3DES加密工作正常,并且使用相同的逻辑获得了错误的解密值。
请找到以下逻辑来调用加密/解密方法
NSString *cipherEnString = [self doCipher:@"Test" enc:kCCEncrypt];
NSString *cipherDecString = [self doCipher:cipherEnString enc:kCCDecrypt];
下面添加了加密/解密的代码。
- (NSString*) doCipher:(NSString*)plainText enc:(CCOperation)encryptOrDecrypt{
const void *vplainText;
size_t plainTextBufferSize;
if (encryptOrDecrypt == kCCDecrypt)
{
NSString *encStr = [self encodeStringTo64:plainText];
NSData *EncryptData = [encStr dataUsingEncoding:NSUTF8StringEncoding];
plainTextBufferSize = [EncryptData length];
vplainText = [EncryptData bytes];
}
else
{
plainTextBufferSize = [plainText length];
vplainText = (const void *) [plainText UTF8String];
}
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
// uint8_t ivkCCBlockSize3DES;
bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);
unsigned char secretKey[24]= {0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11,0x12, 0x11, 0x0D, 0x0B, 0x07, 0x02, 0x04,0x08,0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11};
unsigned char IV3[8]={0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11};
uint8_t iv[kCCBlockSize3DES];
memset((void *) iv, 0x0, (size_t) sizeof(iv));
ccStatus = CCCrypt(encryptOrDecrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding ,
secretKey, //"123456789012345678901234", //key
kCCKeySize3DES,
IV3 , //iv,
vplainText, //plainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
/*else*/ if (ccStatus == kCCParamError) return @"PARAM ERROR";
else if (ccStatus == kCCBufferTooSmall) return @"BUFFER TOO SMALL";
else if (ccStatus == kCCMemoryFailure) return @"MEMORY FAILURE";
else if (ccStatus == kCCAlignmentError) return @"ALIGNMENT";
else if (ccStatus == kCCDecodeError) return @"DECODE ERROR";
else if (ccStatus == kCCUnimplemented) return @"UNIMPLEMENTED";
NSString *result;
if (encryptOrDecrypt == kCCDecrypt)
{
result = [ [NSString alloc] initWithData: [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes] encoding:NSASCIIStringEncoding];
NSString *s = [[NSString alloc] initWithBytes:bufferPtr
length:movedBytes
encoding:NSUTF8StringEncoding];
NSLog(@"%@",s);
}
else
{
NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
NSString *base64String = [myData base64EncodedStringWithOptions:0];
return base64String;
}
return result;
}
- (NSString*)encodeStringTo64:(NSString*)fromString
{
NSData *plainData = [fromString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String;
if ([plainData respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
base64String = [plainData base64EncodedStringWithOptions:kNilOptions]; // iOS 7+
}
else {
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
NSLog(@"%@", base64String);// pre iOS7
}
return base64String;
}
答案 0 :(得分:0)
您正在努力将base64加密字符串转换为加密数据:
电流:
NSString *encStr = [self encodeStringTo64:plainText];
NSData *encryptData = [encStr dataUsingEncoding:NSUTF8StringEncoding];
正确:
NSData *encryptData = [[NSData alloc] initWithBase64EncodedString:plainText options:0];
此外,您不需要以下方法:
- (NSString *)encodeStringTo64:(NSString*)fromString
添加几个要加密/解密的数据的十六进制转储会显示此问题。
最好创建一个基本加密方法,该方法接受并返回NSData
并添加执行调用加密方法的任何编码/解码的方法。减少协作的混合使代码更简单,更容易理解和调试。
请参阅此SO answer,了解如何消除malloc
,而只是使用NSMutableData
。