【问题标题】:Convert RijndaelManaged code to AesManaged code将 RijndaelManaged 代码转换为 AesManaged 代码
【发布时间】:2014-08-29 08:50:54
【问题描述】:

我得到了这段代码:

    public static string DoPrefixCipherEncrypt(string strIn, byte[] btKey)
    {
        if (strIn.Length < 1)
            return strIn;

        // Convert the input string to a byte array 
        byte[] btToEncrypt = System.Text.Encoding.Unicode.GetBytes(strIn);
        RijndaelManaged cryptoRijndael = new RijndaelManaged();
        cryptoRijndael.Mode =
        CipherMode.ECB;//Doesn't require Initialization Vector 
        cryptoRijndael.Padding =
        PaddingMode.PKCS7;


        // Create a key (No IV needed because we are using ECB mode) 
        ASCIIEncoding textConverter = new ASCIIEncoding();

        // Get an encryptor 
        ICryptoTransform ictEncryptor = cryptoRijndael.CreateEncryptor(btKey, null);


        // Encrypt the data... 
        MemoryStream msEncrypt = new MemoryStream();
        CryptoStream csEncrypt = new CryptoStream(msEncrypt, ictEncryptor, CryptoStreamMode.Write);


        // Write all data to the crypto stream to encrypt it 
        csEncrypt.Write(btToEncrypt, 0, btToEncrypt.Length);
        csEncrypt.Close();


        //flush, close, dispose 
        // Get the encrypted array of bytes 
        byte[] btEncrypted = msEncrypt.ToArray();


        // Convert the resulting encrypted byte array to string for return 
        return (Convert.ToBase64String(btEncrypted));
    }

    private static List<int> GetRandomSubstitutionArray(string number)
    {
        // Pad number as needed to achieve longer key length and seed more randomly.
        // NOTE I didn't want to make the code here available and it would take too longer to clean, so I'll tell you what I did. I basically took every number seed that was passed in and prefixed it and  postfixed it with some values to make it 16 characters long and to get a more unique result. For example:
        // if (number.Length = 15)
        //    number = "Y" + number;
        // if (number.Length = 14)
        //    number = "7" + number + "z";
        // etc - hey I already said this is a hack ;)

        // We pass in the current number as the password to an AES encryption of each of the
        // digits 0 - 9. This returns us a set of values that we can then sort and get a 
        // random order for the digits based on the current state of the number.
        Dictionary<string, int> prefixCipherResults = new Dictionary<string, int>();
        for (int ndx = 0; ndx < 10; ndx++)
            prefixCipherResults.Add(DoPrefixCipherEncrypt(ndx.ToString(), Encoding.UTF8.GetBytes(number)), ndx);

        // Order the results and loop through to build your int array.
        List<int> group = new List<int>();
        foreach (string key in prefixCipherResults.Keys.OrderBy(k => k))
            group.Add(prefixCipherResults[key]);

        return group;
    }

从此链接Encrypt a number to another number of the same length

我需要将“DoPrefixCypherEncrypt”转换/调整为 AesManaged 而不是 RijdaelManaged。

谢谢大家

更新: 感谢您的所有回答:

我最终找到了另一种使用 WP 8.1 中可用类的方法。

代替:

        public static string DoPrefixCipherEncrypt(string strIn, byte[] btKey)
    {
        if (strIn.Length < 1)
            return strIn;

        // Convert the input string to a byte array 
        byte[] btToEncrypt = System.Text.Encoding.Unicode.GetBytes(strIn);

        AesManaged cryptoRijndael = new AesManaged();
        cryptoRijndael.Mode = CipherMode.ECB; cryptoRijndael.Padding = PaddingMode.PKCS7; //Mode Doesn't require Initialization Vector 

        // Create a key (No IV needed because we are using ECB mode) 
        ASCIIEncoding textConverter = new ASCIIEncoding();
        // Get an encryptor 
        ICryptoTransform ictEncryptor = cryptoRijndael.CreateEncryptor(btKey, null);
        // Encrypt the data... 
        MemoryStream msEncrypt = new MemoryStream();
        CryptoStream csEncrypt = new CryptoStream(msEncrypt, ictEncryptor, CryptoStreamMode.Write);

        // Write all data to the crypto stream to encrypt it 
        csEncrypt.Write(btToEncrypt, 0, btToEncrypt.Length); csEncrypt.Close(); //flush, close, dispose 
        // Get the encrypted array of bytes 
        byte[] btEncrypted = msEncrypt.ToArray();

        // Convert the resulting encrypted byte array to string for return 
        return (Convert.ToBase64String(btEncrypted));
    }

与非 WinRT .NET 兼容。

我可以使用:

    public static string DoPrefixCipherEncrypt(string strIn, byte[] btKey)
    {
        if (strIn.Length < 1)
            return strIn;

        IBuffer plainBuffer = CryptographicBuffer.ConvertStringToBinary(strIn, BinaryStringEncoding.Utf16LE);
        IBuffer keyMaterial = CryptographicBuffer.CreateFromByteArray(btKey);

        SymmetricKeyAlgorithmProvider symProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
        // create symmetric key from derived password key
        CryptographicKey symmKey = symProvider.CreateSymmetricKey(keyMaterial);

        var buffEncrypted = CryptographicEngine.Encrypt(symmKey, plainBuffer, null);
        var strEncrypted = CryptographicBuffer.EncodeToBase64String(buffEncrypted);

        return strEncrypted;
    }

【问题讨论】:

  • 你的问题是什么?
  • 我不明白这个问题 - 你知道 Rijndael 算法和 AES 实际上是一回事吗?
  • 我想在 Windows/Phone 8.1 中使用这种加密不使用 System.Security.Cryptography 所以我的问题是我需要一个可以用于 Windows.Security.Cryptography 类或任何其他适用的代码一。谢谢
  • 你为什么不干呢?

标签: c# .net cryptography aes rijndaelmanaged


【解决方案1】:

只需将 RijndaelManaged 的​​实例化和声明替换为 AesManaged:

AesManaged cryptoRijndael = new AesManaged();

我试过了,效果很好。我建议也重命名 cryptoRijndael 变量名 - 但这不会改变代码的任何功能。

MSDN上有一个例子如何使用AesManaged,语句如下:

AES 算法本质上是 Rijndael 对称算法 固定的块大小和迭代次数。此类功能相同 作为 RijndaelManaged 类的方式,但将块限制为 128 位和 不允许反馈模式。

所以只需将 RijndaelManaged 的​​引用替换为 AesManaged。只要您不在所描述的限制范围内使用它就可以了。

严格来说,AES is a subset of Rijndael 所以 Rijndael Managed 应该已经涵盖了您需要的任何东西。

来自Wikipedia

AES 基于两个比利时人开发的 Rijndael 密码[5] 密码学家 Joan Daemen 和 Vincent Rijmen 提交了一份 在 AES 选择过程中向 NIST 提出建议。

【讨论】:

  • 谢谢。 Windows/Phone 8.1 没有 Rijndael ,但我知道它有 AesManaged
  • 对不起,WinRT 中没有 AesManaged 类。我最终找到了另一种方法
  • 注意:这个答案也适用于 Windows 的 .NET Framework,而不仅仅是 Windows Mobile 8.x
猜你喜欢
  • 2017-09-23
  • 2012-04-25
  • 2018-11-05
  • 2013-10-24
  • 2020-07-26
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
相关资源
最近更新 更多