如何在目标C与DES在ECB模式加密一个NSString? [英] How to encrypt an NSString in Objective C with DES in ECB-Mode?

查看:189
本文介绍了如何在目标C与DES在ECB模式加密一个NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPhone上的目标C加密一个NSString。至少我wan't获得像一个字符串TmsbDaNG64lI8wC6NLhXOGvfu2IjLGuEwc0CzoSHnrs =当我带codeUS = foo的; PW =酒吧; pwAlg = FALSE;使用此键:TESTTEST

我现在就是问题,那CCCrypt总是返回。4300 - 参数错误,我没有更多的想法,为什么

这是我的code(5小时谷歌和try'n'error结果):

 的NSString *记号= @US = foo的; PW =酒吧; pwAlg = FALSE;;
的NSString *键= @TESTTEST常量无效* vplainText;
为size_t plainTextBufferSize;plainTextBufferSize = [令牌长度]
vplainText =(常量无效*)标记UTF8字符串]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的((无效*)bufferPtr,为0x0,bufferPtrSize);
// memset的((无效*)四,为0x0,(为size_t)的sizeof(IV));
* NSString的initVec = @INIT VEC
常量无效* v键=(常量无效*)键UTF8字符串]
常量无效* vinitVec =(常量无效*)[initVec UTF8字符串]ccStatus = CCCrypt(kCCEncrypt,
                   kCCAlgorithmDES,
                   kCCOptionECBMode,
                   v键,//123456789012345678901234,//关键
                   kCCKeySizeDES,
                   NULL,// vinitVec,//初始化VEC,//第四,
                   vplainText,//您的姓名,//明文,
                   plainTextBufferSize,
                   (无效*)bufferPtr,
                   bufferPtrSize,
                   movedBytes);* NSString的结果;
NSData的* MYDATA的= [NSData的dataWithBytes:(常量无效*)bufferPtr长度:(NSUInteger)movedBytes]
结果= [myData的base64Encoding]


解决方案

在ECB模式的DES加密使用一个8个字节的块大小和连接codeS每块独立。问题是,你输入的字符串不划分为8个字节平等和cryptor不知道如何处理最后一个非8字节的块做的。

解决方法是通过增加kCCOptionPKCS7Padding的选项CCCrypt让cryptor垫最后的障碍。如(片段从NSData的加密类):

  CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                      kCCAlgorithmDES,
                                      kCCOptionPKCS7Padding | kCCOptionECBMode,
                                      keyPtr,
                                      kCCKeySizeDES,
                                      空值,
                                      [自我字节]
                                      DATALENGTH,
                                      缓冲,
                                      BUFFERSIZE
                                      &安培; numBytesEncrypted);

看看这个,了解关于填充算法的详细信息。希望这有助于。

I am trying to encrypt an NSString in Objective C on the iPhone. At least I wan't to get a string like "TmsbDaNG64lI8wC6NLhXOGvfu2IjLGuEwc0CzoSHnrs=" when I encode "us=foo;pw=bar;pwAlg=false;" by using this key: "testtest".

My problem for now is, that CCCrypt always returns "4300 - Parameter error" and I have no more idea why.

This is my code (the result of 5 hours google and try'n'error):

NSString *token = @"us=foo;pw=bar;pwAlg=false;";
NSString *key = @"testtest";

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(kCCEncrypt,
                   kCCAlgorithmDES,
                   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];

解决方案

DES encryption in ECB mode uses an 8 byte block size, and encodes each blocks individually. The problem is that your input string doesn't divide into 8 bytes equally and the cryptor doesn't know what to do with the final non 8 byte block.

The fix is to allow the cryptor to pad the final block by adding kCCOptionPKCS7Padding to the options to CCCrypt. eg (snippet from an NSData encryption category):

CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, 
                                      kCCAlgorithmDES, 
                                      kCCOptionPKCS7Padding | kCCOptionECBMode,
                                      keyPtr, 
                                      kCCKeySizeDES,
                                      NULL, 
                                      [self bytes], 
                                      dataLength,
                                      buffer, 
                                      bufferSize
                                      &numBytesEncrypted);

Take a look at this post for more details regarding padding algorithms. Hope this helps.

这篇关于如何在目标C与DES在ECB模式加密一个NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆