I have a problem with transforming Java Code to Swift for AES Encryption with CBC. Here is the Java Code:
public static String decryptOAuthClientSecret(String cipher) {
String result = "";
try {
Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] sec = MessageDigest.getInstance("MD5").digest(SECRET.getBytes());
SecretKeySpec skeySpec = new SecretKeySpec(sec, "AES");
AlgorithmParameterSpec spec = new IvParameterSpec(sec);
aes.init(Cipher.DECRYPT_MODE, skeySpec, spec);
result = new String(aes.doFinal(Base64Utils.decodeFromString(new String(cipher.getBytes()))));
} catch (Exception e) {
}
return result;
}
My Swift Code:
import CryptoSwift
func decryptAES(cipher: String) {
let spec = SECRET.md5()
let iv = String(spec.prefix(16))
do {
let aes = try AES(key: spec.bytes, blockMode: CBC(iv: iv.bytes), padding: .pkcs5)
let result = try aes.decrypt(Data(base64Encoded: cipher)!.bytes)
print(result.toHexString())
print(String(bytes: result, encoding: .ascii))
} catch(let error) {
print(error)
}
}
Test:
SECRET: asdfasdfasdfasdf
CIPHER: 3+pTpRej59qOjbLz9oP5fN05lHbrT7Gybk9wPA58pXrXAYp2jsO4HchHczvmT4RBvA9pR6Ve/PCe3WEYxSaOqg==
RESULT: 2d2dac4e768eb53498ffd0ebfeef2840f81eb09e2d0f06f8
ACTUAL HEX RESULT: 7e56cfb3bf4f6be7b13aa398e0f2478194f15059d3b795815569b469247eca894b1d44ca2786e1952523ba8cc6618d1da4660a359f767047efd4ec538c6df2fd
ACTUAL ASCII RESULT: ~Vϳ¿Okç±:£àòGñPYÓ·Ui´i$~ÊK\u{1D}DÊ\'á%#ºÆa\u{1D}¤f\n5vpGïÔìSmòý
And here the JavaTest:
@Test
public void testDecryptOAuthClientSecret() {
String decryptedSecret = CryptoUtils.decryptOAuthClientSecret("3+pTpRej59qOjbLz9oP5fN05lHbrT7Gybk9wPA58pXrXAYp2jsO4HchHczvmT4RBvA9pR6Ve/PCe3WEYxSaOqg==");
assertEquals("2d2dac4e768eb53498ffd0ebfeef2840f81eb09e2d0f06f8", decryptedSecret);
}
Does anybody know what im doing wrong?
EDIT:
For the complete clarification here is the encryption function in Java and the clearText:
public static String encryptOAuthClientSecret(String plain) {
String result = "";
try {
Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] sec = MessageDigest.getInstance("MD5").digest(SECRET.getBytes());
SecretKeySpec skeySpec = new SecretKeySpec(sec, "AES");
AlgorithmParameterSpec spec = new IvParameterSpec(sec);
aes.init(Cipher.ENCRYPT_MODE, skeySpec, spec);
result = Base64Utils.encodeToString(aes.doFinal(plain.getBytes()));
} catch (Exception e) {
}
return result;
}
And the test case:
@Test
public void testEncryptOAuthClientSecret() {
String encryptedSecret = CryptoUtils.encryptOAuthClientSecret("2d2dac4e768eb53498ffd0ebfeef2840f81eb09e2d0f06f8");
assertEquals("3+pTpRej59qOjbLz9oP5fN05lHbrT7Gybk9wPA58pXrXAYp2jsO4HchHczvmT4RBvA9pR6Ve/PCe3WEYxSaOqg==", encryptedSecret);
}
Use base64 String instead of hexString.
extension String {
func encrypt(key : String ,value : String) -> String {
var result = ""
do {
let key: [UInt8] = Array(key.utf8) as [UInt8]
let value : [UInt8] = Array(value.utf8) as [UInt8]
let aes = try! AES(key: key, blockMode: CBC(iv: value) as BlockMode, padding: .pkcs7)
let encrypted = try aes.encrypt(Array(self.utf8))
result = encrypted.toBase64() ?? ""
} catch {
print("Error: \(error)")
}
return result
}
// decrypt the string
func decrypt(key : String ,value : String) -> String {
var result = ""
do {
let encrypted = self
let key: [UInt8] = Array(key.utf8) as [UInt8]
let value : [UInt8] = Array(value.utf8) as [UInt8]
let aes = try! AES(key: key, blockMode: CBC(iv: value) as BlockMode, padding: .pkcs7)
let decrypted = try aes.decrypt(Array(base64: encrypted))
result = String(data: Data(decrypted), encoding: .utf8) ?? ""
} catch {
print("Error: \(error)")
}
return result
}
}
For testing you can you can use. Key bits 256. key - "ABC2587@3878avd5DEF3698@4989bwe6" value - "1234567890123456"
Hope it's helps to you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.