xcode Objective-C 解密 AES 128 cbc 十六进制字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9682118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C decrypt AES 128 cbc hex string
提问by pasqui86
I am developing an application for iPhone on Snow Leopard with Xcode 3.1 that receives from a restful web service an encrypted text in hexadecimal format with the algorithm AES 128-bit (CBC). The algorithm uses an initialization vector + secret key. How do I decrypt this text? Thanks to everyone for the tips that I will succeed in giving.
我正在使用 Xcode 3.1 在 Snow Leopard 上开发 iPhone 应用程序,该应用程序从宁静的 Web 服务接收十六进制格式的加密文本,采用 AES 128 位 (CBC) 算法。该算法使用初始化向量 + 密钥。如何解密此文本?感谢大家的提示,我将成功地给予。
EDIT: I get response from REST Server in hex AND crypted format,I try with this code but i receive always bad param error. Can you help me to find the error? Is it possible that i firstly convert the string response into binary format?
编辑:我从 REST 服务器收到十六进制和加密格式的响应,我尝试使用此代码,但我总是收到错误的参数错误。你能帮我找出错误吗?是否有可能我首先将字符串响应转换为二进制格式?
NSString *response = [request responseString];
NSData *encryptedData = [response dataUsingEncoding: NSASCIIStringEncoding];
NSString *key = @"32charlength";
NSString *iv = @"16charlength";
NSData *unencryptedData = NULL;
size_t unencryptedLength = [unencryptedData length];
CCCryptorStatus ccStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, 0, key, kCCKeySizeAES128, iv, [encryptedData bytes], [encryptedData length], unencryptedData, [encryptedData length], &unencryptedLength);
NSString *output = [[NSString alloc] initWithBytes:unencryptedData length:unencryptedLength encoding:NSUTF8StringEncoding];
if (ccStatus == kCCSuccess) risultato.text = @"SUCCESS";
else if (ccStatus == kCCParamError) risultato.text = @"BAD PARAM";
else if (ccStatus == kCCBufferTooSmall) risultato.text = @"BUFFER TOO SMALL";
else if (ccStatus == kCCMemoryFailure) risultato.text = @"MEMORY FAILURE";
else if (ccStatus == kCCAlignmentError) risultato.text = @"ALIGNMENT";
else if (ccStatus == kCCDecodeError) risultato.text = @"DECODE ERROR";
else if (ccStatus == kCCUnimplemented) risultato.text = @"UNIMPLEMENTED";
EDIT2: this function return BAD PARAM because haven't the right buffer size where allocate decrypted data. I edit the function in this working way:
EDIT2:这个函数返回 BAD PARAM 因为没有正确的缓冲区大小来分配解密数据。我以这种工作方式编辑函数:
NSData *encryptedData = [response dataUsingEncoding: NSASCIIStringEncoding];
const void *key = @"32charlength;
uint8_t *iv = @"16charlength";
char buffer[4*4096];
memset(buffer, 'CCCryptorStatus
CCCrypt(CCOperation op, CCAlgorithm alg, CCOptions options, const void *key, size_t keyLength,
const void *iv, const void *dataIn, size_t dataInLength, void *dataOut, size_t dataOutAvailable,
size_t *dataOutMoved);
', sizeof(buffer));
size_t size = sizeof(buffer);
CCCryptorStatus ccStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
0,
key,
kCCKeySizeAES128,
iv,
[encryptedData bytes],
[encryptedData length],
buffer,
sizeof(buffer),
&size);
This function working for me.. thanks so much.
这个功能对我有用..非常感谢。
EDIT 23 MARCH------
编辑 3 月 23 日------
Now the system work form me with 16 byte key size. Now i have a question, what i can do to implement 32 byte key size ? thanks so much.
现在系统以 16 字节的密钥大小工作。现在我有一个问题,我可以做些什么来实现 32 字节的密钥大小?非常感谢。
采纳答案by Tark
This is possible using the CCCryptor
functions included in the <CommonCrypto/CommonCryptor.h>
header. Check man CCCryptor
for the gory details, in your case it sounds like you can use a single call to CCCrypt()
to decode the received data:
这可以使用标题中CCCryptor
包含的函数来实现<CommonCrypto/CommonCryptor.h>
。检查man CCCryptor
血腥细节,在您的情况下,听起来您可以使用单个调用CCCrypt()
来解码接收到的数据:
char * key = "shouldbe16chars.";
NSUInteger dataLength = [encryptedData length];
uint8_t unencryptedData[dataLength + kCCKeySizeAES128];
size_t unencryptedLength;
CCCrypt(kCCDecrypt, kCCAlgorithmAES128, 0, key, kCCKeySizeAES128, NULL, [encryptedData bytes], dataLength, unencryptedData, dataLength, &unencryptedLength);
NSString *output = [[NSString alloc] initWithBytes:unencryptedData length:unencryptedLength encoding:NSUTF8StringEncoding];
Assuming you have the data to be decrypted in NSData *encryptedData
you could try something like:
假设您有要解密的数据,NSData *encryptedData
您可以尝试以下操作:
This is untested, make sure you check the return value of CCCrypt
for errors. Check the header file for details, it is well documented.
这是未经测试的,请确保检查CCCrypt
错误的返回值。检查头文件以获取详细信息,它有很好的文档记录。