【问题标题】:Objective C AES CBC Encryption with Zero PaddingObjective C AES CBC 加密与零填充
【发布时间】:2012-04-28 18:15:50
【问题描述】:

我正在尝试使用 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,也许我忽略了?] 谢谢;

【问题讨论】:

    标签: iphone objective-c ios aes cbc-mode


    【解决方案1】:

    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】:

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

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

      我为 IV 传递了 NULL(这意味着全为零)并假设您正确设置了密钥和密钥大小。在我的测试中,我使用了一个 32 字节(256 位)的密钥。

      【讨论】:

        【解决方案3】:

        查看以下链接。

        AES1 AES2 AES3

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-16
          • 1970-01-01
          • 2013-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-16
          • 1970-01-01
          相关资源
          最近更新 更多