gpt4 book ai didi

iOS AES 加密 - 加密失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:10 25 4
gpt4 key购买 nike

在我的项目中,我必须实现 AES 128 CBC 加密。我正在使用类别并且基于 NSData。这是我的加密代码:

- (NSData*)AES128Decrypt
{
char ivPtr[kCCKeySizeAES128 + 1];
bzero(ivPtr, sizeof(ivPtr));

// fetch iv data
[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];


// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES128 + 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]; // dataLength = 19

//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 numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, 0,
keyPtr, kCCKeySizeAES128,
ivPtr,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted); // buffer = 0 & numBytesDecrypted = 0

if (cryptStatus == kCCSuccess)
{
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytes:buffer length:numBytesDecrypted] ; // returns 0
}

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

这就是我在 View 类中调用它的方式:

- (void) testActuallyEncrypting :(NSString*) hexString {
NSLog(@"String to Encrypt : %@", hexString); // prints test12

@try {
//Convert NSString to NSData
NSData *data = [self dataFromHexString:hexString]; // [hexString dataUsingEncoding:NSUTF8StringEncoding]; //
// // Prepare the NSDAta obj to store the encrypted pswd
NSData *encryptedData = [NSData dataWithBytes:[data bytes] length:[data length]]; // 6bytes
NSData *decryptedData = [encryptedData AES128Decrypt]; // 0bytes
NSString *decryptedString = [NSString stringWithUTF8String:[decryptedData bytes]]; // NULL Exception
NSLog(@"Decrypted String : %@", decryptedString);

decryptedString = [self addPaddingToString:decryptedString];
decryptedData = [NSData dataWithBytes:[decryptedString UTF8String] length:[[decryptedString dataUsingEncoding:NSUTF8StringEncoding] length]];
encryptedData = [decryptedData AES128Encrypt];
if (encryptedData!=nil)
{
NSString *encryptedHexString = [self hexStringFromData:encryptedData];
NSLog(@"Encrypted HexString : %@",encryptedHexString);

}
}@catch (NSException *ex) {
NSLog(@"Exception : %@", ex);
}
}

我正在传递要加密的 "test12" 字符串。在调用 AES128Decrypt 时,decryptedData 为 0,因此下一行 decryptedString 抛出 Null 异常 - Exception : *** +[ NSString stringWithUTF8String:]: NULL cString.

谁能帮我知道为什么 decryptedData 为空。 AES128Decrypt 方法哪里出错了?

请帮帮我。从过去的两天开始,我一直坚持这一点。在互联网上搜索了很多,但无法获得任何解决方案。非常感谢任何帮助。谢谢。

更新:-在我的类(class)中添加了@Zaph 的方法并正在调用它。

NSLog(@"String to Encrypt : %@", hexString);
NSString *iv = @"fedcba9876543210";
NSString *key = @"0123456789abcdef";

// Convert str to encrypt, iv & key from NSString to NSData
NSData *dataIn = [hexString dataUsingEncoding:NSUTF8StringEncoding];
NSData *ivData = [iv dataUsingEncoding:NSUTF8StringEncoding];
NSData *symKey = [key dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;

NSData *result = [LoginViewController doCipher:dataIn iv:ivData key:symKey context:kCCEncrypt error:&error]; // result = 16bytes

if (result != nil) {
// Convert result to satring
NSString *resultStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(@"Encrypted Str = %@", resultStr ); // Encrypted Str = (null) ????

为什么转换后的字符串为空?请任何帮助。谢谢

最佳答案

没有必要把加密搞得那么复杂,这里有一个基本的加密/解密方法。 iv 和 key 的长度必须正确。值上下文是 kCCEncryptkCCDecrypt

+ (NSData *)doCipher:(NSData *)dataIn
iv:(NSData *)iv
key:(NSData *)symmetricKey
context:(CCOperation)encryptOrDecrypt
error:(NSError **)error
{
CCCryptorStatus ccStatus = kCCSuccess;
size_t cryptBytes = 0;
NSMutableData *dataOut = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];

ccStatus = CCCrypt( encryptOrDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
symmetricKey.bytes,
kCCKeySizeAES128,
iv.bytes,
dataIn.bytes,
dataIn.length,
dataOut.mutableBytes,
dataOut.length,
&cryptBytes);

if (ccStatus == kCCSuccess) {
dataOut.length = cryptBytes;
}
else {
if (error) {
*error = [NSError errorWithDomain:@"kEncryptionError"
code:ccStatus
userInfo:nil];
}
dataOut = nil;
}

return dataOut;
}

关于iOS AES 加密 - 加密失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23637597/

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