在Objective C,.net和Android中生成相同的加密字符串AES/CBC/PKCS7Padding [英] Generate same encrypted string AES/CBC/PKCS7Padding in Objective C, .net and Android
问题描述
我想在iOS和.net的android中生成相同的加密字符串.我可以在android和.net中生成相同的字符串,但对于Objective C可以生成不同的字符串.
I want to generate the same encrypted string in iOS, android in .net. I can generate same string android and in .net but different for Objective C.
Android代码:
Android code:
public static String encrypt(String key, String value) {
try {
SecretKey secretKey = new SecretKeySpec(key.getBytes(), "AES");
AlgorithmParameterSpec iv = new IvParameterSpec(key.getBytes());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return new String(Base64.encode(cipher.doFinal(value.getBytes("UTF-8")), Base64.NO_WRAP));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
后端代码
public Encryption(input: string): any {
return CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(input), this.Cryptokey,
{
keySize: 128 / 8,
iv: this.iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
}和目标C代码中
- (NSData *)AES256EncryptWithKey:(NSString *)key
{
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer); //free the buffer;
return nil;
}
,我遵循了这些网址加密AES/CBC/PKCS7Padding
使用PKCS7Padding的AES CBC加密在Java和Objective-C中有不同的结果
根据我对目标C的理解,我首先需要在NSData中转换密码,然后将其传递给AES加密方法.此方法将返回给我加密的数据,现在我需要将其转换为基本64字符串编码.有人可以建议我如何为iOS,Android和.net生成相同的结果吗?
As per my understanding for Objective C first I need to convert the password in NSData then pass it to the AES encryption method. This method will return to me encrypted Data now I need to convert this in base 64 string encoding. Can anyone suggest to me what should I do to generate the same result for iOS, Android, and .net?
例如如果我想加密字符串"xceedancce",在所有三个平台上,我的关键是7061737323313233
For example if I want to encrypt string "xceedancce" in all 3 platforms and my key is 7061737323313233
然后在Android和.net中,结果为"uXDlYA4e8Z8HWd9rvNdXaw =="相同,但在iOS中
then in Android and .net the result is "uXDlYA4e8Z8HWd9rvNdXaw==" same but in iOS
是"l4zDDnwOVJ0dz2fl7HdKIA =="谁能建议我在目标C中该怎么做?
it is "l4zDDnwOVJ0dz2fl7HdKIA==" Can anyone suggest what should I do in Objective C?
谢谢.
推荐答案
谢谢,罗布.我同时使用了RNCryptor库(Objective C和C#),它解决了我的问题.
Thank you, Rob. I used the RNCryptor library for both (Objective C and C#) and it solved my problem.
这是我的目标C代码:
-(NSString *)encryptUsingRNCryptor:(NSString *)strPassword{
NSData *data = [strPassword dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSData *encryptedData = [RNEncryptor encryptData:data
withSettings:kRNCryptorAES256Settings
password:kKeyForEncryption
error:&error];
NSString *encryptString = [encryptedData base64EncodedStringWithOptions:0];
NSLog(@"enccrypted data ----->>>## %@ ## %@", encryptedData, encryptString);
// Decryption
NSData *decryptedData = [RNDecryptor decryptData:encryptedData
withPassword:kKeyForEncryption
error:&error];
NSString *decodedString = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
NSLog(@" Decryted data ----%@ --------and string %@", decryptedData, decodedString);
return [encryptedData base64EncodedStringWithOptions:0];
}
对于C#解密,我们使用了RNCryptor-cs库.
and for C# decryption we used RNCryptor-cs library.
再一次感谢Rob:)
这篇关于在Objective C,.net和Android中生成相同的加密字符串AES/CBC/PKCS7Padding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!