2012-04-28 44 views
0

ゼロパディング付きのCBCモードを使用して、AES 128暗号化を使用して文字列を暗号化しようとしています。悲しいことに、私はこれを行う方法がわからない、多くの試みが失敗したので。私はC#でコードを持っていて、誰かが私の暗号化の働きを助けることができるかどうか疑問に思っていました。 コード:[私は多分、私が見落として、とにかくずっとcccrypto知らない?]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);` 

IVEは、共通の暗号を見て、しかし、私はCBCモードのオプションを見ることができません 感謝。

答えて

2

メソッドパラメータ宣言ので

@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. 

IV CommonCrypto.m

- (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm 
           key: (id) key 
       initializationVector: (id) iv 
          options: (CCOptions) options 
           error: (CCCryptorStatus *) error 
に、あなたは ivパラメータ

nil値を渡すことができ
0

次のリンクを参照してください。

AES1 AES2 AES3

あなたはそこからソースコードライブラリをダウンロードして、あなたはあなたのためにそれらのいずれかが有用見つけた場合、あなたのプロジェクトでそれを使用することができます。

2

Common Cryptoは適切な場所です。具体的には、CCCryptorCreateの3番目のパラメータ、つまりCCOptionsを0に設定します。つまり、デフォルトはCBCです。 CommonCrypto.hは、実際にはマニュアルページよりも優れているため、オープンしてください。ここでそれを行う方法の例です:

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

私は(すべてゼロを意味する)IVのためにNULLに渡され、あなたが正しくキーとキーサイズのセットアップを得たと想定しました。私のテストでは、32バイト(256ビット)のキーを使用しました。

関連する問題