gpt4 book ai didi

java - 将 Java AES 解密代码转换为 CryptoSwift

转载 作者:行者123 更新时间:2023-11-29 05:40:45 25 4
gpt4 key购买 nike

我在将 Java 代码转换为 Swift 以使用 CBC 进行 AES 加密时遇到问题。这是 Java 代码:

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;
}

我的 Swift 代码:

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)
}
}

测试:

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òý

这里是 JavaTest:

@Test
public void testDecryptOAuthClientSecret() {
String decryptedSecret = CryptoUtils.decryptOAuthClientSecret("3+pTpRej59qOjbLz9oP5fN05lHbrT7Gybk9wPA58pXrXAYp2jsO4HchHczvmT4RBvA9pR6Ve/PCe3WEYxSaOqg==");
assertEquals("2d2dac4e768eb53498ffd0ebfeef2840f81eb09e2d0f06f8", decryptedSecret);
}

有人知道我做错了什么吗?

编辑:

为了完整说明,这里是 Java 中的加密函数和明文:

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;
}

测试用例:

@Test
public void testEncryptOAuthClientSecret() {
String encryptedSecret = CryptoUtils.encryptOAuthClientSecret("2d2dac4e768eb53498ffd0ebfeef2840f81eb09e2d0f06f8");
assertEquals("3+pTpRej59qOjbLz9oP5fN05lHbrT7Gybk9wPA58pXrXAYp2jsO4HchHczvmT4RBvA9pR6Ve/PCe3WEYxSaOqg==", encryptedSecret);
}

最佳答案

使用 base64 字符串而不是十六进制字符串。

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"

希望对你有帮助。

关于java - 将 Java AES 解密代码转换为 CryptoSwift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56581358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com