Hi I am building a iOS application that communicates with a restful server. This requires my java AES code and Swift AES code to produce the same results currently I am using CryptoSwift, using the tutorial here (AES Encrypt and Decrypt). However this gives me different results compared to my java code.
Here is my java code:
private static final byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' }; public static String encrypt(String Data) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance("AES"); c.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = c.doFinal(Data.getBytes()); String encryptedValue = new BASE64Encoder().encode(encVal); return encryptedValue; } public static String decrypt(String encryptedData) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance("AES"); c.init(Cipher.DECRYPT_MODE, key); byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); byte[] decValue = c.doFinal(decordedValue); String decryptedValue = new String(decValue); return decryptedValue; } public static Key generateKey() throws Exception { Key key = new SecretKeySpec(keyValue, "AES"); return key; } public static void main(String[] args) { try { System.out.println(AES.encrypt("test")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } This code gives me xGdooY+6V8q1cze7mR9TjQ==
Here is my code in swift
import Foundation import CryptoSwift extension String { func aesEncrypt(key: String, iv: String) throws -> String{ let data = self.dataUsingEncoding(NSUTF8StringEncoding) let enc = try AES(key: key, iv: iv, blockMode:.CBC).encrypt(data!.arrayOfBytes()) let encData = NSData(bytes: enc, length: Int(enc.count)) let base64String: String = encData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)); let result = String(base64String) return result } func aesDecrypt(key: String, iv: String) throws -> String { let data = NSData(base64EncodedString: self, options: NSDataBase64DecodingOptions(rawValue: 0)) let dec = try AES(key: key, iv: iv, blockMode:.CBC).decrypt(data!.arrayOfBytes()) let decData = NSData(bytes: dec, length: Int(dec.count)) let result = NSString(data: decData, encoding: NSUTF8StringEncoding) return String(result!) } } let key = "TheBestSecretKey" // length == 32 let iv = "gqLOHUioQ0QjhuvI" // length == 16 let s = "test" let enc = try! s.aesEncrypt(key, iv: iv) let dec = try! enc.aesDecrypt(key, iv: iv) print(s) print("enc:\(enc)") print("dec:\(dec)") print("\(s == dec)") This code gives me LQu3c4HaOQf7W0CfnMMy1w==
As you may see they do work independently but I need them to be compatible