I’ve an issue with AES-128 encryption. The encrypted string in iOS is different as compared to Android.
Below is android code :
public class Encryption {
private static final String ALGORITHM = "AES";
private static final String UNICODE_FORMAT = "UTF8";
public static String encryptValue(String valueToEnc) {
try {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = new Base64().encode(encValue);
String urlEncodeddata = URLEncoder.encode(encryptedValue, "UTF-8");
return urlEncodeddata;
} catch (Exception e) {
}
return valueToEnc;
}
private static Key generateKey() throws Exception {
byte[] keyAsBytes;
keyAsBytes = "MySixteenCharKey".getBytes(UNICODE_FORMAT);
Key key = new SecretKeySpec(keyAsBytes, ALGORITHM);
return key;
}
}
Create string extension
and use library CryptoSwift
// String+Addition.swift
import CryptoSwift
extension String {
func aesEncrypt(key: String) throws -> String {
var result = ""
do {
let key: [UInt8] = Array(key.utf8) as [UInt8]
let aes = try! AES(key: key, blockMode: ECB() , padding:.pkcs5) // AES128 .ECB pkcs7
let encrypted = try aes.encrypt(Array(self.utf8))
result = encrypted.toBase64()!
print("AES Encryption Result: \(result)")
} catch {
print("Error: \(error)")
}
return result
}
}
and to use
@IBAction func onBtnClicked(_ sender: Any) {
let value = "My value to be encrypted"
let key = "MySixteenCharKey"
print(key!)
let encryptedValue = try! value.aesEncrypt(key: key!)
print(encryptedValue)
}
For citation of this particular android code: 1) The 16 char length key implies AES-128 2) The code is without iVector, This implies ECB mode 3) Using padding as pkcs5 or pkcs7 did not made any difference in my case
Create an extension of String and use this below function for encrypting and decrypting.
extension String {
//MARK: - Encrypt AES Method
/// This method will encrypt and return Cipher string
func aesEncrypt(key: String = "fqJfdzGDvfwbedsKSUGty3VZ9taXxMVw", iv: String = "1234567890123456") -> String {
if let data = self.data(using: String.Encoding.utf8) {
do {
let enc = try AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt(data.bytes)
let encData = Data(bytes: enc, count: Int(enc.count))
let base64String: String = encData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0));
return base64String
}
catch let error {
print(error.localizedDescription)
return ""
}
}
return ""
}
//MARK: - Decrypt AES Method
/// This method will decrypt the Cipher string
func aesDecrypt(key: String = "fqJfdzGDvfwbedsKSUGty3VZ9taXxMVw", iv: String = "1234567890123456") -> String {
if let data = Data(base64Encoded: self, options: Data.Base64DecodingOptions.init(rawValue: 0)) {
do {
let dec = try AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).decrypt(data.bytes)
let decData = Data(bytes: dec, count: Int(dec.count))
let result = String(data: decData, encoding: .utf8)
return result ?? ""
}
catch let error {
print(error.localizedDescription)
return ""
}
}
return ""
}
}
Please use below library for AES encryption
and decryption
. It is available for iOS, Android and Web as well.
For iOS
OS / Swift 3, 4
Add a bridging header. Apple documentation
#import "CryptLib.h"
let plainText = "this is my plain text"
let key = "your key"
let cryptLib = CryptLib()
let cipherText = cryptLib.encryptPlainTextRandomIV(withPlainText: plainText, key: key)
print("cipherText \(cipherText! as String)")
let decryptedString = cryptLib.decryptCipherTextRandomIV(withCipherText: cipherText, key: key)
print("decryptedString \(decryptedString! as String)")
Android
val plainText = "this is my plain text"
val key = "your key"
val cryptLib = CryptLib()
val cipherText = cryptLib.encryptPlainTextWithRandomIV(plainText, key)
println("cipherText $cipherText")
val decryptedString = cryptLib.decryptCipherTextWithRandomIV(cipherText, key)
println("decryptedString $decryptedString")
avascript / NodeJS / Web
Download the library
npm install @skavinvarnan/cryptlib --save
const plainText = "this is my plain text";
const key = "your key";
const cryptLib = require('@skavinvarnan/cryptlib');
const cipherText = cryptLib.encryptPlainTextWithRandomIV(plainText, key);
console.log('cipherText %s', cipherText);
const decryptedString = cryptLib.decryptCipherTextWithRandomIV(cipherText, key);
console.log('decryptedString %s', decryptedString);
You will get same encryption
and decryption
result for all plateform ioS/Android/Web.
Try this:
func encryptValue(stringToEncrypt:String) -> String{
var encryptedString: String = ""
let value = "MySixteenCharKey"
let input: Array<UInt8> = Array(stringToEncrypt.utf8)
let key: Array<UInt8> = Array("MySixteenCharKey".utf8)
do {
let encrypted = try AES(key: key, blockMode: .ECB, padding: PKCS7()).encrypt(input)
let base64 = encrypted.toBase64()
encryptedString = base64
} catch {
print(error)
}
return encryptedString
}
来源:https://stackoverflow.com/questions/43322310/aes128-encryption-in-swift