我已实现此加密/解密并执行且没有错误,但我无法解密数据,我不确定数据是否已加密,请帮助。
- (NSData *)AES256EncryptWithKey:(NSString *)key {
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [strData length];
data = [[NSData alloc] init];
data = [strData dataUsingEncoding:NSUTF8StringEncoding];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *pribuffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[data bytes], dataLength, /* input */
[data bytes], bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:data length:numBytesEncrypted];
}}
解密是:
-(NSData *)AES256DecryptWithKey:(NSString *)key andForData:(NSData *)objDataObject {
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [objDataObject length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL,
[objDataObject bytes], dataLength,
[objDataObject bytes], bufferSize,
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:objDataObject length:numBytesDecrypted];
}
}
我将上述方法称为:
CryptoClass *obj = [CryptoClass new];
NSData *objData = [obj AES256EncryptWithKey:@"hell"];
NSData *val = [obj AES256DecryptWithKey:@"hell" andForData:objData ];
NSLog(@"decrypted string is : %@ AND LENGTH IS %d",[val description],[val length]);
解密似乎根本没有发生和加密 - 我不确定,请在这里帮助我。