我想使用身份验证标签创建AES加密和解密GCM模式。这是我的iOS版本的源代码:
CCCryptorStatus ccStatus = kCCSuccess;
NSData *iv = [hexIV dataUsingEncoding:NSUTF8StringEncoding];
NSData *symmetricKey = [hexIV dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *dataOut = [NSMutableData dataWithLength:self.length];
NSMutableData *tag = [NSMutableData dataWithLength:kCCBlockSizeAES128];
size_t tagLength = kCCBlockSizeAES128;
ccStatus = CCCryptorGCM(kCCEncrypt,
kCCAlgorithmAES,
symmetricKey.bytes,
kCCKeySizeAES256,
iv.bytes,
iv.length,
aad.bytes,
aad.length,
self.bytes,
self.length,
dataOut.mutableBytes,
tag.bytes,
&tagLength);
if (ccStatus == kCCSuccess) {
return [NSDictionary dictionaryWithObjectsAndKeys:dataOut,@"cyphertext",tag,@"tag",iv,@"iv",nil];
} else {
return nil;
}
我想将此代码更改为Android版本,下面是Android的源代码:
byte[] aKey = hexStringToByteArray(hexKey);
byte[] aIV = hexStringToByteArray(hexIV);
Key key = new SecretKeySpec(aKey, "AES");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(16 * Byte.SIZE, aIV));
cipher.updateAAD(aad);
byte[] encrypted = cipher.doFinal(aKey);
如何在我的源代码中添加身份验证标签?
当我在iOS
中加密AES GCM时let iv = AES.GCM.Nonce()
let sealedBox = try! AES.GCM.seal(rawJsonData,
using: yourKey,
nonce: yourIv)
let cipher = sealedBox.ciphertext + sealedBox.tag
记住,在Android中,您有:iv cipher tag
您可以轻松地在iOS中复制此iOS,以遵循相同的结构,
let incomingCipher = (payload?.fromBase64())!
let IV = initialVector?.fromBase64()
let combine = IV! + incomingCipher
let myNewSealedBox = try AES.GCM.SealedBox(combined: combine)
其中incomingcipher(android(包含密码 tag