如何在 ECB 模式下使用 DES 加密 Objective C 中的 NSString?
我正在尝试在 iPhone 上的 Objective C 中加密 NSString。至少当我编码“us=foo;pw=bar;pwAlg=false;”时,我不想得到像“TmsbDaNG64lI8wC6NLhXOGvfu2IjLGuEwc0CzoSHnrs=”这样的字符串通过使用这个键:“testtest”。
我现在的问题是,CCCrypt 总是返回“4300 - 参数错误”,我不知道为什么。
这是我的代码(5小时谷歌和尝试错误的结果):
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];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ECB 模式下的 DES 加密使用 8 字节块大小,并对每个块单独进行编码。问题是您的输入字符串没有平均分为 8 个字节,并且加密器不知道如何处理最终的非 8 字节块。
修复方法是允许加密器通过将 kCCOptionPKCS7Padding 添加到 CCCrypt 的选项来填充最终块。例如(来自 NSData 加密类别的片段):
看看这个 帖子 有关填充算法的更多详细信息。希望这有帮助。
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):
Take a look at this post for more details regarding padding algorithms. Hope this helps.