c# - 将 RijndaelManaged 代码转换为 AesManaged 代码

标签 c# .net cryptography aes rijndaelmanaged

我得到了这段代码:

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

最佳答案

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

AesManaged cryptoRijndael = new AesManaged();

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

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

The AES algorithm is essentially the Rijndael symmetric algorithm with a fixed block size and iteration count. This class functions the same way as the RijndaelManaged class but limits blocks to 128 bits and does not allow feedback modes.

因此只需将 RijndaelManaged 的​​引用替换为 AesManaged。只要您不在描述的限制之外使用它,您应该没问题。

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

来自 Wikipedia :

AES is based on the Rijndael cipher[5] developed by two Belgian cryptographers, Joan Daemen and Vincent Rijmen, who submitted a proposal to NIST during the AES selection process.

关于c# - 将 RijndaelManaged 代码转换为 AesManaged 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25564827/

相关文章:

c# - 获取静态字段的值

c# - 我用 EF 6 实现外键的错误是什么?

c# - C#中的AES CTR解密

c# - 使用代码库加载程序集

C#。相同的代码仅适用于 1 个标签...为什么?

.net - 如何缓存css,图像和js?

c# - ServiceStack IReturn 和元数据

c# - 为什么 smtpclient 发送的电子邮件没有出现在已发送的项目中

c++ - C++ 中的 "Decrypt"MD5

cryptography - 将 X.509 证书从十六进制格式转换为 .cer 格式