3des在iOS中的加密和解密

3des在iOS中的加密和解密

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

问题描述

我正在从事3DES加密和解密。我已经成功完成加密。对于解密,我使用这个代码,但没有得到确切的结果。我的加密字符串为 CHc3TsfJgYs = ,键为分生组织。请告诉我我做错了什么?

  NSString * token = @CHc3TsfJgYs =; 
NSString * key = @meristem;

const void * vplainText;
size_t plainTextBufferSize;

plainTextBufferSize = [token length];
vplainText =(const void *)[token UTF8String];

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

bufferPtrSize =(plainTextBufferSize + kCCBlockSize3DES)& 〜(kCCBlockSize3DES - 1);
bufferPtr = malloc(bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr,0x0,bufferPtrSize);
// memset((void *)iv,0x0,(size_t)sizeof(iv));


NSString * initVec = @init Vec;
const void * vkey =(const void *)[key UTF8String];
const void * vinitVec =(const void *)[initVec UTF8String];

ccStatus = CCCrypt(kCCDecrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
vkey,//123456789012345678901234,//键
kCCKeySizeDES,
NULL,// vinitVec,//init Vec,// iv,
vplainText,//Your Name,// plainText,
plainTextBufferSize,
(void * )bufferPtr,
bufferPtrSize,
& movedBytes);

NSString * result;
NSData * myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
result = [myData base64Encoding];
// result = [myDat];
NSLog(@dis is data%@,result);


解决方案
  ccStatus = CCCrypt(kCCDecrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
vkey,//123456789012345678901234,// key
kCCKeySize3DES,
NULL,// vinitVec / /init Vec,// iv,
vplainText,//Your Name,// plainText,
plainTextBufferSize,
(void *)bufferPtr,
bufferPtrSize,
& movedBytes);

您需要更改 kCCKeySizeDES - > kCCKeySize3DES


I am working on 3DES encryption and decryption. I have done encryption successfully. For decryption, I am using this code but not getting exact result. My encryption string is "CHc3TsfJgYs=" and key is "meristem". Please tell me what I am doing wrong?

 NSString *token = @"CHc3TsfJgYs=";
    NSString *key = @"meristem";

    const void *vplainText;
    size_t plainTextBufferSize;

    plainTextBufferSize = [token length];
    vplainText = (const void *) [token UTF8String];

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

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


    NSString *initVec = @"init Vec";
    const void *vkey = (const void *) [key UTF8String];
    const void *vinitVec = (const void *) [initVec UTF8String];

    ccStatus = CCCrypt(kCCDecrypt,
                       kCCAlgorithm3DES,
                       kCCOptionPKCS7Padding | kCCOptionECBMode,
                       vkey, //"123456789012345678901234", //key
                       kCCKeySizeDES,
                       NULL,// vinitVec, //"init Vec", //iv,
                       vplainText, //"Your Name", //plainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);

    NSString *result;
    NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
    result = [myData base64Encoding];
    // result = [myDat];
    NSLog(@"dis is data %@",result);
解决方案
ccStatus = CCCrypt(kCCDecrypt,
                   kCCAlgorithm3DES,
                   kCCOptionPKCS7Padding | kCCOptionECBMode,
                   vkey, //"123456789012345678901234", //key
                   kCCKeySize3DES,
                   NULL,// vinitVec, //"init Vec", //iv,
                   vplainText, //"Your Name", //plainText,
                   plainTextBufferSize,
                   (void *)bufferPtr,
                   bufferPtrSize,
                   &movedBytes);

you need change kCCKeySizeDES -> kCCKeySize3DES

这篇关于3des在iOS中的加密和解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:56