ios Android和Objective C之间的AES 128兼容性

zvms9eto  于 2022-11-19  发布在  iOS
关注(0)|答案(2)|浏览(112)

我有两个应用程序:Android和iOS(Objective-C)。我正在尝试实现一个加密系统,这样我就可以在这两个应用程序上加密,并在服务器应用程序中解密。问题是,我使用的是AES 128-ECB,但我从Android获得的base64密钥与我的objectivec密钥不匹配。我不知道我错过了什么。
以下是一些片段:内部监督系统

- (NSData*) EncryptAES: (NSString *) key{
char keyPtr[kCCKeySizeAES128+1];
bzero( keyPtr, sizeof(keyPtr) );

[key getCString: keyPtr maxLength: sizeof(keyPtr) encoding:NSUTF8StringEncoding];
size_t numBytesEncrypted = 0;

NSUInteger dataLength = [self length];

size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
const unsigned char iv[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

CCCryptorStatus result = CCCrypt( kCCEncrypt,
                                 kCCAlgorithmAES128,
                                 kCCOptionPKCS7Padding,
                                 keyPtr,
                                 kCCKeySizeAES128,
                                 iv,
                                 [self bytes], [self length],
                                 buffer, bufferSize,
                                 &numBytesEncrypted );

if( result == kCCSuccess )
    return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
else {
    NSLog(@"Failed AES");
}
return nil;
}

然后道:

NSString *pass = @"WORD_TO_ENCRYPT";
NSString *key = @"STRING_KEY";

//Encryption - APPROACH 1
NSData *data = [pass dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [data EncryptAES:key];
NSString* encryptedBase64 = [self Base64Encode:encryptedData];

NSLog(@"%@", encryptedBase64);

下面是我的Java函数:

String plainTextKey = "STRING_KEY";
    String plainText = "WORD_TO_ENCRYPT";
    // Encrypt where jo is input, and query is output and ENCRPYTION_KEy is key
    //String inputtt = "some clear text data";
    byte[] input = new byte[0];
    String skyKey;

    input = plainText.getBytes("utf-8");
    MessageDigest md;
    md = MessageDigest.getInstance("MD5");
    byte[] thedigest = md.digest(plainTextKey.getBytes("UTF-8"));
    SecretKeySpec skc = new SecretKeySpec(thedigest, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skc);
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);
    String encode = Base64.encode(cipherText);
    System.out.println(encode);

我在撞墙,却不知道我错过了什么。提前感谢你的帮助!
PS:我没有特别的理由使用AES 128-ECB。如果多系统兼容性更简单,我可以使用任何其他算法。

ffscu2ro

ffscu2ro1#

在Android端,当您初始化Cipher示例时,必须提供匹配的IV。

byte[] iv = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
cipher.init(Cipher.ENCRYPT_MODE, skc, new IvParameterSpec(iv));
mlmc2os5

mlmc2os52#

避免使用硬编码的IV作为加密机制,至少在Android上是这样。建议使用随机数生成器,但如果您无法提供随机值,至少可以从密钥中构建IV。
另一方面,如果一切都是从密码派生出来的,那么就不会有很好的安全性;"你需要在每一条信息中加入一些随机性“
确保存储加密过程中使用的IV,以便在解密时正确应用。

byte[] iv = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
cipher.init(Cipher.ENCRYPT_MODE, skc, new IvParameterSpec(iv));

相关问题