AES encryption in iOS and Android, and decryption in C#.NET

AES encryption in iOS and Android, and decryption in C#.NET

AES (Advanced Encryption Standard) is a widely used encryption algorithm that can be implemented in various programming languages, including iOS, Android, and C#.NET. Here is an example of how to perform AES encryption in iOS and Android, and decryption in C#.NET.

iOS (Swift):

import CommonCrypto func encryptAES(key: Data, iv: Data, plaintext: Data) throws -> Data { let ciphertextLength = plaintext.count + kCCBlockSizeAES128 var ciphertext = Data(count: ciphertextLength) var bytesEncrypted: size_t = 0 let status = key.withUnsafeBytes { keyUnsafeRawBufferPointer in iv.withUnsafeBytes { ivUnsafeRawBufferPointer in plaintext.withUnsafeBytes { plaintextUnsafeRawBufferPointer in ciphertext.withUnsafeMutableBytes { ciphertextUnsafeMutableRawBufferPointer in CCCrypt( CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionPKCS7Padding), keyUnsafeRawBufferPointer.baseAddress, key.count, ivUnsafeRawBufferPointer.baseAddress, plaintextUnsafeRawBufferPointer.baseAddress, plaintext.count, ciphertextUnsafeMutableRawBufferPointer.baseAddress, ciphertextLength, &bytesEncrypted) } } } } guard status == kCCSuccess else { throw NSError(domain: "com.example.aes", code: Int(status)) } return ciphertext.prefix(bytesEncrypted) } 

Android (Java):

import android.util.Base64; import java.nio.charset.StandardCharsets; import java.security.AlgorithmParameters; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AES { public static String encrypt(String key, String iv, String plaintext) throws Exception { byte[] keyBytes = Base64.decode(key, Base64.DEFAULT); byte[] ivBytes = Base64.decode(iv, Base64.DEFAULT); byte[] plaintextBytes = plaintext.getBytes(StandardCharsets.UTF_8); Key secretKey = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(ivBytes)); byte[] ciphertextBytes = cipher.doFinal(plaintextBytes); return Base64.encodeToString(ciphertextBytes, Base64.DEFAULT); } } 

C# (.NET):

using System.Security.Cryptography; using System.Text; public static string DecryptAes(string key, string iv, string ciphertext) { byte[] keyBytes = Convert.FromBase64String(key); byte[] ivBytes = Convert.FromBase64String(iv); byte[] ciphertextBytes = Convert.FromBase64String(ciphertext); using Aes aes = Aes.Create(); aes.Key = keyBytes; aes.IV = ivBytes; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; using ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); byte[] plaintextBytes = decryptor.TransformFinalBlock(ciphertextBytes, 0, ciphertextBytes.Length); return Encoding.UTF8.GetString(plaintextBytes); } 

In this example, we have created a function to encrypt plaintext using AES in iOS and Android, and a function to decrypt the resulting ciphertext in C#.NET. Both the iOS and Android implementations use the CommonCrypto library to perform AES encryption, while the C# implementation uses the Aes class provided by the .NET framework. The key

Examples

  1. "AES encryption in Swift for iOS"

    • Code Implementation (Swift):
      let dataToEncrypt = "YourData".data(using: .utf8)! let key = "YourSecretKeyYourSecretKeyYourSecretKey".data(using: .utf8)! let iv = "YourInitializationVector".data(using: .utf8)! var encryptedData: Data? do { encryptedData = try AES.GCM.seal(dataToEncrypt, using: SymmetricKey(data: key), nonce: AES.GCM.Nonce(data: iv)) } catch { print("Encryption error: \(error)") } 
    • Description: Uses the CryptoSwift library to perform AES encryption in Swift.
  2. "AES encryption with base64 encoding in Swift for iOS"

    • Code Implementation (Swift):
      let encryptedBase64 = encryptedData?.base64EncodedString() 
    • Description: Converts the encrypted data to a base64-encoded string for transmission.

Android (Java/Kotlin):

  1. "AES encryption in Java for Android"

    • Code Implementation (Java):
      String dataToEncrypt = "YourData"; String key = "YourSecretKeyYourSecretKeyYourSecretKey"; String iv = "YourInitializationVector"; byte[] encryptedData = AesEncryption.encrypt(dataToEncrypt, key, iv); 
    • Description: Uses a custom AesEncryption class to perform AES encryption in Java for Android.
  2. "AES encryption with base64 encoding in Java for Android"

    • Code Implementation (Java):
      String encryptedBase64 = Base64.getEncoder().encodeToString(encryptedData); 
    • Description: Converts the encrypted byte array to a base64-encoded string for transmission.

C#.NET:

  1. "AES decryption in C#"

    • Code Implementation (C#):
      byte[] decryptedData = AesDecryption.Decrypt(encryptedData, key, iv); string decryptedString = Encoding.UTF8.GetString(decryptedData); 
    • Description: Uses a custom AesDecryption class to decrypt the AES-encrypted data in C#.
  2. "AES decryption from base64 string in C#"

    • Code Implementation (C#):
      byte[] encryptedBytes = Convert.FromBase64String(encryptedBase64); byte[] decryptedData = AesDecryption.Decrypt(encryptedBytes, key, iv); string decryptedString = Encoding.UTF8.GetString(decryptedData); 
    • Description: Decodes the base64-encoded string and performs AES decryption in C#.
  3. "AES key and IV generation in C#"

    • Code Implementation (C#):
      byte[] key = AesEncryption.GenerateRandomKey(); byte[] iv = AesEncryption.GenerateRandomIV(); 
    • Description: Generates a random key and initialization vector (IV) for AES encryption in C#.
  4. "AES encryption and decryption with password in C#"

    • Code Implementation (C#):
      byte[] salt = AesEncryption.GenerateSalt(); byte[] key = AesEncryption.DeriveKeyFromPassword("YourPassword", salt); byte[] encryptedData = AesEncryption.Encrypt(dataToEncrypt, key, iv); byte[] decryptedData = AesDecryption.Decrypt(encryptedData, key, iv); 
    • Description: Derives a key from a password and uses it for both encryption and decryption in C#.
  5. "AES encryption with CBC mode in C#"

    • Code Implementation (C#):
      byte[] encryptedData = AesEncryption.EncryptCBC(dataToEncrypt, key, iv); 
    • Description: Performs AES encryption using CBC (Cipher Block Chaining) mode in C#.
  6. "AES encryption with PKCS7 padding in C#"

    • Code Implementation (C#):
      byte[] encryptedData = AesEncryption.EncryptWithPKCS7Padding(dataToEncrypt, key, iv); 
    • Description: Uses PKCS7 padding for AES encryption in C#.

More Tags

crashlytics android-camera actionbarsherlock hidden-field dos broadcast git-log remote-connection teamcity javac

More C# Questions

More Chemical thermodynamics Calculators

More Cat Calculators

More Other animals Calculators

More Physical chemistry Calculators