0

AES 128 暗号化、CBC モード、ゼロ パディングを使用して文字列を暗号化しようとしています。悲しいことに、多くの試みが失敗したため、これを行う方法がわかりません。私は C# のコードを持っていますが、誰かが暗号化を機能させるのを手伝ってくれるかどうか疑問に思っていました。コード:

`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 をあまり知らないので、見落としているのでしょうか?] ありがとうございます。

4

3 に答える 3

2

CommonCryptoは見るのに適した場所です。具体的には、CCCryptorCreateの3番目のパラメーターであるCCOptionsを0に設定する必要があります。つまり、デフォルトはCBCです。CommonCrypto.hを開く必要があります。これは、manページよりも実際には優れたドキュメントです。これを行う方法の例を次に示します。

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

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

于 2012-04-28T18:53:52.273 に答える
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. 

したがって、パラメータnilに値を渡すことができますiv

于 2012-04-28T19:04:44.620 に答える
0

以下のリンクをご覧ください。

AES1 AES2 AES3

そこからソース コード ライブラリをダウンロードして、役に立つものがあればプロジェクトで使用できます。

于 2012-04-28T18:50:12.693 に答える