Encrypt/decrypt string with des cbc mode obj-c

安稳与你 提交于 2019-12-25 05:03:21

问题


hi guys i'm pretty new in obj-c world and i would like know how can I encrypt a string in des? I already tried search but could not find any sample code that could help me the only thing I realized is that there is a class commonCrypt to do what I want but I do not know how to use it

my code

NSString* key = @"abc43HU0";
NSString *token = @"hellohello";


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 = NULL;

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 *) [key UTF8String];

ccStatus = CCCrypt(kCCEncrypt,
                   kCCAlgorithmDES,
                   kCCModeCBC,
                   vkey, //"123456789012345678901234", //key
                   kCCKeySizeDES,
                   vinitVec,// 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;

crypt_result.text = myData;

回答1:


this is the working code:

NSString* key = @"abc43HU0";
NSString *token = @"hellohello";

const void *vplainText;
size_t plainTextBufferSize = [token length];
vplainText = (const void *) [token UTF8String];    
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;

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

Byte iv [] = {0x65, 0x110, 0x68, 0x26, 0x69, 0x178, 0x200, 0x219};

const void *vkey = (const void *) [key UTF8String];

ccStatus = CCCrypt(kCCEncrypt,
                   kCCAlgorithmDES,
                   kCCOptionPKCS7Padding,
                   vkey, 
                   kCCKeySizeDES,
                   iv,
                   vplainText,
                   plainTextBufferSize,
                   (void *)bufferPtr,
                   bufferPtrSize,
                   &movedBytes);

NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
NSString* result = [base64 base64EncodeData:myData];//my own method to encoding with base64



回答2:


Try kCCOptionPKCS7Padding | kCCModeCBC for ccOptions parameter.



来源:https://stackoverflow.com/questions/11375909/encrypt-decrypt-string-with-des-cbc-mode-obj-c

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