我正在尝试使用CBC模式使用零填充加密AES 128加密的字符串。可悲的是,我不知道该怎么做,因为许多尝试都没有成功。我在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多少反正,也许我忽略了?] 感谢;
答案 0 :(得分: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位)密钥。
答案 1 :(得分: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
参数
答案 2 :(得分:0)