本文介绍了iphone加密不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经实现了这个加密/解密,执行没有错误,但是我无法解密数据,不知道数据是否加密, pre> - (NSData *)AES256EncryptWithKey :( NSString *)键{

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 / *初始化向量(可选)* /,
[数据字节],dataLength, * input * /
[data bytes],bufferSize,/ * output * /
& numBytesEncrypted);
if(cryptStatus == kCCSuccess){
return [NSData dataWithBytesNoCopy:data length:numBytesEncrypted];
}}

解密是:


$ b $ (NSData *)key andForData:(NSData *)objDataObject {
char keyPtr [kCCKeySizeAES256 + 1];($)$ {code>
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:@hellandForData:objData];

NSLog(@解密字符串为:%@ AND LENGTH IS%d,[val description],[val length]);

解密似乎并没有发生,加密 - 我不确定,请帮助我

解决方案

我已经自己学习了,



可以通过ccats的完全实现是:





感谢所有帮助的人。


I have this encryption/decryption implemented and executes with no error, but i am unable to decrypt the data and im not sure whether the data is encrypted, kindly help.

- (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];
}}

The decryption is :

-(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];
}

}

i call the above methods like this:

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]);

Decryption doesnt seem to happen at all and the encryption - i am not sure about it, kindly help me here.

解决方案

I have learnt it by myself,

a complete implementation which can bye-pass ccats is:

http://pastie.org/966473

Thanks to all who helped.

这篇关于iphone加密不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 21:05