2012-04-28 111 views
0

我正在尝试使用CBC模式和零填充来加密AES 128加密的字符串。可悲的是,我不知道该怎么做,因为很多尝试都失败了。我在C#中有代码,并想知道是否有人能帮我让我的加密工作。 代码:Objective C AES CBC加密与零填充

`using System; 
using System.Security.Cryptography; 
using System.Text; 
using System.IO; 

byte[] request = UTF8Encoding.UTF8.GetBytes("{string which needs encrypting}"); 

byte[] key = UTF8Encoding.UTF8.GetBytes("{key}"); 
byte[] iv = UTF8Encoding.UTF8.GetBytes("{iv}"); 

AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); 

aes.Key = key; 
aes.IV = iv; 


aes.Mode = CipherMode.CBC; 

aes.Padding = PaddingMode.Zeros; 

ICryptoTransform cTransform = aes.CreateEncryptor(); 

byte[] result = cTransform.TransformFinalBlock(request, 0, request.Length); 

aes.Clear() 

string encryptedRequest = Convert.ToBase64String(result, 0, result.Length);` 

香港专业教育学院看上去公共的密码,但我不能看到一个选项,CBC模式[我不知道cccrypto反正多,也许我忽略了?] 感谢;

回答

2

的方法,CommonCrypto.m

- (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm 
           key: (id) key 
       initializationVector: (id) iv 
          options: (CCOptions) options 
           error: (CCCryptorStatus *) error 

IV参数声明

@param  iv    Initialization vector, optional. Used for 
          Cipher Block Chaining (CBC) mode. If present, 
          must be the same length as the selected 
          algorithm's block size. If CBC mode is 
          selected (by the absence of any mode bits in 
          the options flags) and no IV is present, a 
          NULL (all zeroes) IV will be used. This is 
          ignored if ECB mode is used or if a stream 
          cipher algorithm is selected. 

所以,你可以通过niliv参数

0

看看下面的链接。

AES1 AES2 AES3

你可以从那里下载源代码库,并用它在你的项目中,如果你发现你其中的任何有用的。

2

Common Crypto是正确的地方看看。具体而言,您要使用CCCryptorCreate的第三个参数,即CCOptions设置为0.也就是说,默认值是CBC。您应该打开CommonCrypto.h,因为它实际上比手册页更好的文档。下面是如何做到这一点的例子:

CCCryptorRef cryptorRef; 
CCCryptorStatus rc; 
rc = CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES128, 0, key, keySize, NULL, &cryptorRef); 

我NULL被传为IV(这意味着所有的零),并假设你正确地拿到了钥匙和密钥大小设置。在我的测试中,我使用了一个32字节(256位)的密钥。