gpt4 book ai didi

ios - SWIFT AES 加密和解密 - 得到不同的结果

转载 作者:可可西里 更新时间:2023-11-01 01:56:11 26 4
gpt4 key购买 nike

我正在快速实现 AES 加密。java和C#的加密解密工作正常。

很快,我得到的结果与实际结果不同。在调试时,我注意到 Java 默认使用 sign int。所以我以相同的方式实现,这样我就可以验证 derivedKey 在两个应用程序(Java 和 Swift)中是否相同。但是在创建 keyData 和 ivData 时,它会丢失已签名的数据。不确定这是否会造成问题。

我已经尝试了 AES Encryption .net to swift 中解释的以下代码

func decrypt(encryptedText: String, keys :String)  -> String{
let encryptedData = encryptedText.data(using: .utf16LittleEndian)
let derivedKey = generateDerivedKey(keyString: keys)
let key = Array(derivedKey[0..<32])
let iv = Array(derivedKey[32..<48])
let keyData = Data(bytes: key, count: key.count)
let ivData = Data(bytes: iv, count: iv.count)
let decryptedData = testDeCrypt(data: encryptedData!, keyData: keyData, ivData: ivData, operation: kCCDecrypt)

return String(bytes: decryptedData, encoding: .unicode)!
}

func generateDerivedKey(keyString :String) -> [Int8] {
let salt: [UInt8] = [0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76]
var key = [UInt8](repeating: 0, count: 48)
CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), keyString, keyString.utf8.count, salt, salt.count, CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA1), 1000, &key, 48)
let derivedKey : [Int8] = key.map {Int8(bitPattern: $0)}

return derivedKey
}

func testDeCrypt(data: Data, keyData: Data, ivData: Data, operation: Int) -> Data {
assert(keyData.count == Int(kCCKeySizeAES128) || keyData.count == Int(kCCKeySizeAES192) || keyData.count == Int(kCCKeySizeAES256))
var decryptedData = Data(count: data.count)
var num_bytes_decrypted: size_t = 0
let operation = CCOperation(operation)
let algoritm = CCAlgorithm(kCCAlgorithmAES)
let options = CCOptions(kCCOptionPKCS7Padding)
let decryptedDataCount = decryptedData.count
let cryptoStatus = keyData.withUnsafeBytes {keyDataBytes in
ivData.withUnsafeBytes {ivDataBytes in
data.withUnsafeBytes {dataBytes in
decryptedData.withUnsafeMutableBytes {decryptedDataBytes in
CCCrypt(operation, algoritm, options, keyDataBytes, keyData.count, ivDataBytes, dataBytes, data.count, decryptedDataBytes, decryptedDataCount, &num_bytes_decrypted)
}
}
}
}
if cryptoStatus == CCCryptorStatus(kCCSuccess) {
decryptedData.count = num_bytes_decrypted
return decryptedData
} else {
return Data()
}
}

Java代码

public static String aesDecrypt(String text, String key) {
byte[] decValue = null;
try {
byte[] salt = new byte[] { 0x49, 0x76, 0x61, 0x6E, 0x20, 0x4D,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
SecretKeyFactory factory = SecretKeyFactory
.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray(), salt,
1000, 384);

Key secretKey = factory.generateSecret(pbeKeySpec);
byte[] keys = new byte[32];
byte[] iv = new byte[16];
System.arraycopy(secretKey.getEncoded(), 0, keys, 0, 32);
System.arraycopy(secretKey.getEncoded(), 32, iv, 0, 16);

SecretKeySpec secretSpec = new SecretKeySpec(keys, "AES");

AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

try {
cipher.init(Cipher.DECRYPT_MODE, secretSpec, ivSpec);
} catch (InvalidKeyException e) {

} catch (InvalidAlgorithmParameterException e) {
}

org.apache.commons.codec.binary.Base64 decoder = new org.apache.commons.codec.binary.Base64();
byte[] decodedValue = decoder.decode(text.getBytes());

decValue = cipher.doFinal(decodedValue);

} catch (Exception e) {
}

if (decValue != null) {
return new String(decValue, Charset.forName("UTF_16LE"));
} else {
return null;
}
}

测试数据键:“ThisIsATestPassword444Encryption”文本:“TestStringToEncrypt”

Java 输出编码密文:[97, 47, 77, 79, 118, 111, 79, 70, 47, 87, 90, 67, 81, 98, 51, 74, 83, 88, 97, 68, 84, 105, 72 , 71, 67, 121, 122, 86, 81, 116, 106, 104, 117, 78, 108, 118, 49, 48, 65, 77, 69, 53, 114, 43, 120, 104, 89, 120 , 50, 98, 80, 66, 50, 77, 87, 80, 103, 110, 117, 118, 118, 97, 78, 106]

加密文本:a/MOvoOF/WZCQb3JSXaDTiHGCyzVQtjhuNlv10AME5r+xhYx2bPB2MWPgnuvvaNj

解密 text.getbytes : [97, 47, 77, 79, 118, 111, 79, 70, 47, 87, 90, 67, 81, 98, 51, 74, 83, 88, 97, 68, 84 , 105, 72, 71, 67, 121, 122, 86, 81, 116, 106, 104, 117, 78, 108, 118, 49, 48, 65, 77, 69, 53, 114, 43, 120, 104 , 89, 120, 50, 98, 80, 66, 50, 77, 87, 80, 103, 110, 117, 118, 118, 97, 78, 106]

解码解密文本:[107, -13, 14, -66, -125, -123, -3, 102, 66, 65, -67, -55, 73, 118, -125, 78, 33, -58、11、44、-43、66、-40、-31、-72、-39、111、-41、64、12、19、-102、-2、-58、22、49、-39 , -77, -63, -40, -59, -113, -126, 123, -81, -67, -93, 99]

快速输出:加密文本:a/MOvoOF/WZCQb3JSXaDTiHGCyzVQtjhuNlv10AME5r+xhYx2bPB2MWPgnuvvaNj

解密文本 : ー瑒왿᪰牧ྐྵ䐫侺ꋴ锏閑ﷃ鿴㦦જ㤉ꄕ临高禄鼍ᐏ┓ീ学䥏㿖칵斗솽ᢼ介鞴ꋴ鴷쿠蒻⯨䍊䂷篥럟⤫俷违둘๔Ꞵ‵

Swift 和 java 加密匹配。

非常感谢任何帮助。

最佳答案

Swift 代码中最糟糕的两个部分是:

#1

let encryptedData = encryptedText.data(using: .utf16LittleEndian)

和:

#2

return String(bytes: decryptedData, encoding: .unicode)!

#1

在您的 Java 代码中,您正在将 text 解码为 Base-64,但在您的 Swift 代码中,您只是获得 .utf16LittleEndian 的字节表示,这与 Base-64 无关。

你可能需要这样的东西:

guard let encryptedData = Data(base64Encoded: encryptedText) else {
print("Data is not a valid base-64")
return nil
}

(您的decrypt(encryptedText:keys:) 应该返回String? 而不是String,因为解密可能会失败。)


#2

在您的 Java 代码中,您使用 new String(decValue, Charset.forName("UTF_16LE")) 将解密的字节转换为字符串。 UTF_16LE 代表 UTF-16 Little Endian。 Swift 的 String.Encoding 中的等价物是 utf16LittleEndian

该行应该如下所示:

return String(bytes: decryptedData, encoding: .utf16LittleEndian)

当您使用 [UInt8] 作为其返回类型时,您的 generateDerivedKey(keyString:) 可以被简化。 (你最好使用 UInt8 来表示 Swift 中的中间字节类型。)

所有这些结合起来,你的 Swift 代码应该是:

func decrypt(encryptedText: String, keys: String)  -> String? { //### `String?` rather than `String`
//### Decode `encryptedText` as Base-64
guard let encryptedData = Data(base64Encoded: encryptedText) else {
print("Data is not a valid Base-64")
return nil
}
let derivedKey = generateDerivedKey(keyString: keys)
//### A little bit shorter, when `derivedKey` is of type `[UInt8]`
let keyData = Data(bytes: derivedKey[0..<32])
let ivData = Data(bytes: derivedKey[32..<48])
if let decryptedData = testDeCrypt(data: encryptedData, keyData: keyData, ivData: ivData, operation: kCCDecrypt) {
//### Use `utf16LittleEndian`
return String(bytes: decryptedData, encoding: .utf16LittleEndian)
} else {
//### return nil, when `testDeCrypt` fails
return nil
}
}

func generateDerivedKey(keyString: String) -> [UInt8] { //### `[UInt8]`
let salt: [UInt8] = [0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76]
var key = [UInt8](repeating: 0, count: 48)
CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), keyString, keyString.utf8.count, salt, salt.count, CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA1), 1000, &key, 48)

//### return the Array of `UInt8` directly
return key
}

func testDeCrypt(data: Data, keyData: Data, ivData: Data, operation: Int) -> Data? { //### make it Optional
assert(keyData.count == Int(kCCKeySizeAES128) || keyData.count == Int(kCCKeySizeAES192) || keyData.count == Int(kCCKeySizeAES256))
var decryptedData = Data(count: data.count)
var numBytesDecrypted: size_t = 0
let operation = CCOperation(operation)
let algoritm = CCAlgorithm(kCCAlgorithmAES)
let options = CCOptions(kCCOptionPKCS7Padding)
let decryptedDataCount = decryptedData.count
let cryptoStatus = keyData.withUnsafeBytes {keyDataBytes in
ivData.withUnsafeBytes {ivDataBytes in
data.withUnsafeBytes {dataBytes in
decryptedData.withUnsafeMutableBytes {decryptedDataBytes in
CCCrypt(operation, algoritm, options, keyDataBytes, keyData.count, ivDataBytes, dataBytes, data.count, decryptedDataBytes, decryptedDataCount, &numBytesDecrypted)
}
}
}
}
if cryptoStatus == CCCryptorStatus(kCCSuccess) {
decryptedData.count = numBytesDecrypted
return decryptedData
} else {
return nil //### returning `nil` instead of `Data()`
}
}

使用上面的新 Swift 代码,我可以生成与您的 Java 代码相同的结果:

let test = "a/MOvoOF/WZCQb3JSXaDTiHGCyzVQtjhuNlv10AME5r+xhYx2bPB2MWPgnuvvaNj"
let keys = "ThisIsATestPassword444Encryption"

if let result = decrypt(encryptedText: test, keys: keys) {
print(result) //->TestStringToEncrypt
} else {
print("*Cannot decrypt*")
}

(我需要更新我的旧 Java 环境以比较 Java 和 Swift 之间的中间结果,但这是另一回事...)

关于ios - SWIFT AES 加密和解密 - 得到不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53195595/

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