gpt4 book ai didi

java - AESCipher(安卓)与 CCCrypt(ios)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:16 35 4
gpt4 key购买 nike

我使用 2 个库来加密密码,但使用相同的密码,我得到不同的值:。

这是我在android中使用的代码:

String dataEncrypted = new String();
try {
Cipher aesCipher = Cipher.getInstance("AES");
byte[] raw = hexToBytes(key);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
aesCipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] byteDataToEncrypt = data.getBytes();
byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
dataEncrypted = new BASE64Encoder().encode(byteCipherText);
return dataEncrypted;
} catch (Exception ex) {
//log.d(ex.getMessage());
}

我在 ios 中使用的这段代码:

const void *vplainText;
size_t plainTextBufferSize;
NSData *plainTextData = [data dataUsingEncoding: NSUTF8StringEncoding];
plainTextBufferSize = [plainTextData length];
vplainText = [plainTextData bytes];
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
bufferPtrSize =(plainTextBufferSize + kCCBlockSizeAES128);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0001, bufferPtrSize);

const void *vkey = (const void *)[key UTF8String];


CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
vkey,
kCCKeySizeAES128,
NULL,
vplainText,
plainTextBufferSize, /* input */
bufferPtr,
kCCKeySizeAES128, /* output */
&movedBytes);

NSString result;
if (cryptStatus== kCCSuccess)
{
result=[Base64 encode:(const void *)bufferPtr length:(NSUInteger)movedBytes];
free(bufferPtr);

}else{
result =@"False";
free(bufferPtr);
}

如何匹配ios和android的2个版本。请帮帮我!

最佳答案

Android 代码使用 AES256 加密字符串,然后对结果进行 base64 编码。 iOS 代码看起来只是加密,因此要使它们匹配,您必须对其进行 base64 编码。尝试使用以下内容:

- (NSString *)AES256EncryptWithKey:(NSString *)key
{
NSData *plainData = [self dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [plainData AES256EncryptWithKey:key];

//Use any decent base64 category support
NSString *encryptedString = [encryptedData base64Encoding];

return encryptedString;
}

. .对于 base64 编码,请在 NSString 上使用任何合适的类别,或者如果您还没有,请尝试 here .

加密方式(AES256是Android默认的AES):

   //based on: AES Encrypt/Decrypt, Created by Jim Dovey and 'Jean'
//See http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html
- (NSData*)AES256EncryptWithKey:(NSString*)key
{
uint8_t iv[kCCBlockSizeAES128];
SecRandomCopyBytes(0, sizeof(iv), iv);

// '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, kCCKeySizeAES128,
iv /* initialization vector (optional) */, [self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess)
{
NSMutableData* result = [NSMutableData dataWithBytes:iv length:kCCBlockSizeAES128];
[result appendBytes:buffer length:numBytesEncrypted];

free(buffer); //free the buffer

//the returned NSData takes ownership of the buffer and will free it on deallocation
return result;
}

free(buffer); //free the buffer
return nil;
}

我在 CocoaSecurity 上也取得了很好的成功框架,但是为了匹配Android的AES加密,我不得不退回到上面的方法。 (不再记得为什么)。

关于java - AESCipher(安卓)与 CCCrypt(ios),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23861877/

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