OStack程序员社区-中国程序员成长平台

标题: ios - iOS中的3des加解密 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:24
标题: ios - iOS中的3des加解密

我正在研究 3DES 加密和解密。我已成功完成加密。对于解密,我正在使用此代码,但没有得到准确的结果。我的加密字符串是“CHc3TsfJgYs=”, key 是“meristem”。请告诉我我做错了什么?

 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 dataWithBytesconst void *)bufferPtr lengthNSUInteger)movedBytes];
    result = [myData base64Encoding];
    // result = [myDat];
    NSLog(@"dis is data %@",result);



Best Answer-推荐答案


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

关于ios - iOS中的3des加解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21454774/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4