将RijndaelManaged代码转换为AesManaged代码



我得到了这段代码:

    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;
    }

从该链接将一个号码加密为另一个相同长度的号码

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

谢谢各位

更新:感谢您的回答:

我最终找到了另一种方法,使用WP8.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;
    }

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

AesManaged cryptoRijndael = new AesManaged();

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

MSDN上有一个如何使用AesManaged的示例,其中包含以下语句:

AES算法本质上是Rijndael对称算法固定的块大小和迭代计数。这个类的功能相同与RijndaelManaged类一样,但将块限制为128位不允许使用反馈模式。

因此,只需将RijndaelManaged的引用替换为AesManaged即可。只要你没有在描述的限制之外使用它,你就应该没事。

严格来说,AES是Rijndael的一个子集,因此Rijndael-Managed应该已经涵盖了您需要的任何内容。

来自维基百科:

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

相关内容

  • 没有找到相关文章

最新更新