嗨,大家好我在obj-c世界很新,我想知道如何加密des中的字符串?我已经尝试了搜索,但找不到任何可以帮助我的示例代码我唯一意识到有一个类commonCrypt来做我想要的但我不知道如何使用它
我的代码
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;
答案 0 :(得分:2)
这是工作代码:
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
答案 1 :(得分:0)
尝试使用kCCOptionPKCS7Padding | kCCModeCBC
获取ccOptions参数。