ios 3DES加密/解密CBC

时间:2014-12-04 14:45:37

标签: ios encryption 3des cbc-mode

我需要一些3DES解密方面的帮助。我已经创建了一些解密代码,但是我无法让它按原样运行。这是我第一次使用加密技术,而且我不知道我在这里做错了什么。

我从服务器收到解密密钥(这是密钥的示例值:0F7BC98767FF9A01F2B2AD1CD644AD33 - 它是随机生成字节的十六进制表示。)

这是我用来解密邮件的方法:

-(NSString*)doCipher:(NSString*)message key:(NSString*)key operation:(CCOperation)encryptOrDecrypt {

const void *messageData;
size_t messageBufferSize;

if (encryptOrDecrypt == kCCDecrypt){
    NSData *messageEncryptData= [NSData dataWithBase64EncodedString:message];
    messageBufferSize= [messageEncryptData length];
    messageData= [messageEncryptData bytes];
}
else{
    messageBufferSize= message.length;
    messageData = [[[message dataUsingEncoding: NSUTF8StringEncoding]mutableCopy] bytes];
}

CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;

bufferPtrSize = (messageBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);

uint8_t iv[kCCBlockSize3DES];
memset((void *) iv, 0x0, (size_t) sizeof(iv));



NSData *keyData = [[key dataUsingEncoding:NSUTF8StringEncoding]mutableCopy];

ccStatus = CCCrypt(encryptOrDecrypt,
                   kCCAlgorithm3DES,
                   kCCOptionPKCS7Padding & kCCModeCBC,
                   (const void *)[keyData bytes],
                   kCCKeySize3DES,
                   iv,
                   messageData,
                   messageBufferSize,
                   (void *)bufferPtr,
                   bufferPtrSize,
                   &movedBytes);

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";

NSLog(@"bufferPtr: %s", bufferPtr);

NSString *s = [[NSString alloc] initWithFormat:@"%s", bufferPtr];
return s;
}

- 编辑--- 这是我从方法中收到的输出:

bufferPtr:Ã'ÕÏÁU

---编辑2 ----- 我将密钥长度固定为24字节长,但现在最终结果为空字符串

1 个答案:

答案 0 :(得分:2)

我发现了我的问题。这是我读钥匙的方式。

我创建了这个方法,用于将十六进制字符串转换为NSData:

+ (NSData *)dataFromHexString: (NSString *) hex {
const char *chars = [hex UTF8String];
int i = 0, len = hex.length;

NSMutableData *data = [NSMutableData dataWithCapacity:len / 2];
char byteChars[3] = {'\0','\0','\0'};
unsigned long wholeByte;

while (i < len) {
    byteChars[0] = chars[i++];
    byteChars[1] = chars[i++];
    wholeByte = strtoul(byteChars, NULL, 16);
    [data appendBytes:&wholeByte length:1];
}

return data;
}

然后这就是我设法解密该死的东西:

NSData *keyData = [Utilities dataFromHexString:key];

ccStatus = CCCrypt(encryptOrDecrypt,
                   kCCAlgorithm3DES,
                   kCCOptionPKCS7Padding,
                   [keyData bytes],
                   [keyData length],
                   iv,
                   messageData,
                   messageBufferSize,
                   (void *)bufferPtr,
                   bufferPtrSize,
                   &movedBytes);

抱歉浪费你的时间:)