ios - 加密字符串错误
<p><p>你好需要加密这个字符串</p>
<pre><code>NSString *iv = @"12345678910111211";
NSString *key = @"12345678910111211";
NSString *dataString2 = @"eJDRqD_1ME0:APA91bFRfuy6Xk0GMVHtFLKjw5eZnqoGQ7wdTYjXdLGPqOVGuApv_eaZQzHQmqhqTKN70mspUt6BpZFFnCGn4j2y0Ew-eS1SnghLQZNxNAbh9LSgCjviWGe2wwCCW132jqW5E_aaxL5g";
NSData *dataIn2 = ;
NSData *result2 = ; // result = 16bytes
NSData *hexaData2 = result2;
NSString *DataHexadecimalString2 = ;
NSLog(@"Encrypted hexa= %@", DataHexadecimalString2);
</code></pre>
<p>我得到的输出是:</p>
<blockquote>
<p>6bd95973e91de1330e3195098104116b0f888533bfeb0f20edcbcdf66a9e5d79676b8b33b62c470454003dc5013d92efb191b1b07e320b1cff59874007191d72be18e8b2784dcfc8c2960b59879b9c14c42421105ac356d5bccc7ee0f70122f8c2a47743984ba453a02b82b7ddd770fd5284483d3581c818076f9c87569345ab558c2e286ceb1388d6444042ecb10d0ccb294488ed51c86de20b85b076bb2d85</p>
</blockquote>
<p>预期的输出是这样的:</p>
<blockquote>
<p>6bd95973e91de1330e3195098104116b0f888533bfeb0f20edcbcdf66a9e5d79676b8b33b62c470454003dc5013d92efb191b1b07e320b1cff59874007191d72be18e8b2784dcfc8c2960b59879b9c14c42421105ac356d5bccc7ee0f70122f8c2a47743984ba453a02b82b7ddd770fd5284483d3581c818076f9c87569345ab558c2e286ceb1388d6444042ecb10d0c46530088a27a522ba365d6942a83ac41</p>
</blockquote>
<p>不知道我做错了什么。谁能帮帮我?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这是一个很好的例子说明为什么<strong>不</strong>使用<code>mcrypt</code>。</p>
<p>输入字符串为 153 字节,需要填充到 160 字节,要么指定填充选项,要么会出错。在这种情况下,加密函数似乎只占用了内存中输入之后的任何字节。</p>
<p>一种解决方案是指定 PKCS#7(née PKCS#5)填充。</p>
<p>添加信息:<code>mcrypt</code> 并且正在使用空填充,只需向加密数据添加足够的空字节以使其达到 block 大小的精确倍数(AESblock 大小为 16 字节)。</p>
<p>你需要做两件事:</p>
<p>1) 添加尾随 0x00 字节,在本例中添加 8:</p>
<pre><code>NSString *dataString2 = @"eJDRqD_1ME0:APA91bFRfuy6Xk0GMVHtFLKjw5eZnqoGQ7wdTYjXdLGPqOVGuApv_eaZQzHQmqhqTKN70mspUt6BpZFFnCGn4j2y0Ew-eS1SnghLQZNxNAbh9LSgCjviWGe2wwCCW132jqW5E_aaxL5g\x00\x00\x00\x00\x00\x00\x00\x00";
</code></pre>
<p>2) 在 BBAES 中删除 PKCS#7 填充</p>
<pre><code>CCCryptorStatus status = CCCryptorCreate(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding, , , , &cryptor);
</code></pre>
<p>到</p>
<pre><code>CCCryptorStatus status = CCCryptorCreate(operation, kCCAlgorithmAES128, 0, , , , &cryptor);
</code></pre>
<p>仅使用 Common Crypto(警告,示例中没有错误检查):</p>
<pre><code>NSData *ivData= [@"0q1z2a3a4p5a6789" dataUsingEncoding:NSUTF8StringEncoding];
NSData *keyData = [@"9876a5p4a3a2z1q0" dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [@"eJDRqD_1ME0:APA91bFRfuy6Xk0GMVHtFLKjw5eZnqoGQ7wdTYjXdLGPqOVGuApv_eaZQzHQmqhqTKN70mspUt6BpZFFnCGn4j2y0Ew-eS1SnghLQZNxNAbh9LSgCjviWGe2wwCCW132jqW5E_aaxL5g\x00\x00\x00\x00\x00\x00\x00\x00" dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *plainData = ;
size_t movedBytes = 0;
CCCryptorStatus ccStatus;
ccStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
0, // Bad idea not to use PKCS#7 padding (kCCOptionPKCS7Padding)
keyData.bytes, kCCKeySizeAES128,
ivData.bytes,
encryptedData.bytes, encryptedData.length,
plainData.mutableBytes, plainData.length,
&movedBytes);
plainData.length = movedBytes;
NSLog(@"Data: \n%@",plainData);
</code></pre>
<p>您当然可以使用代码循环添加空字节,但最好不要使用 mcrypt 并使用 PKCS#7(née PKCS#5)填充。</p></p>
<p style="font-size: 20px;">关于ios - 加密字符串错误,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/42006736/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/42006736/
</a>
</p>
页:
[1]