3des encryption and decryption in iOS

[亡魂溺海] 提交于 2019-12-19 10:25:46

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/21454774/3des-encryption-and-decryption-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!