C# Rijndael 算法(如何创建我们自己的密钥)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1666588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:50:13  来源:igfitidea点击:

Rijndael algorithm (How to create our own Key )

c#windows-mobilecompact-framework

提问by Geetha

All the samples for Rijndael algorithm are defining the key from the Rijndael class itself, can't we provide the Key of our own. Any hint on this will help me a lot.

Rijndael 算法的所有示例都是从 Rijndael 类本身定义密钥,我们不能提供自己的密钥。对此的任何提示都会对我有很大帮助。

The sample application we are creating is for windows mobile and it doesn't support PasswordDeriveBytes

我们正在创建的示例应用程序适用于 windows mobile,它不支持 PasswordDeriveBytes

Thanks in advance Geetha

提前致谢

Update on this topic: As per the code sample provided below, we have tried it and it seems to be working but there is a small hiccup in this. when we decrypt the data there is a 8 bit padding up on the right side of the value for the example, we are encrypting a Unique key for transaction and it looks like this :

关于此主题的更新:根据下面提供的代码示例,我们已经尝试过,它似乎可以正常工作,但是这里有一个小问题。当我们解密数据时,示例值的右侧有一个 8 位填充,我们正在加密交易的唯一密钥,它看起来像这样:

Before encryption: MI03112009044625000000000000008024754008

加密前:MI03112009044625000000000000008024754008

After Decryption: MI03112009044625000000000000008024754008揞????空?資

解密后:MI03112009044625000000000000000000000008024754008揞??????空?资

can anyone help on this right padding happening in the original value.

任何人都可以帮助处理原始值中发生的正确填充。

thanks Geetha

谢谢 Geetha

采纳答案by Jonas Elfstr?m

You can try something like this, based on the RijndaelManaged Class MSDN articlethat I also recommend you to read.

您可以根据我也推荐您阅读的RijndaelManaged Class MSDN 文章尝试类似的操作。

var plainText = "This will be encrypted.";
var aesAlg = new RijndaelManaged();
aesAlg.Key = new byte[32] { 118, 123, 23, 17, 161, 152, 35, 68, 126, 213, 16, 115, 68, 217, 58, 108, 56, 218, 5, 78, 28, 128, 113, 208, 61, 56, 10, 87, 187, 162, 233, 38 };
aesAlg.IV = new byte[16] { 33, 241, 14, 16, 103, 18, 14, 248, 4, 54, 18, 5, 60, 76, 16, 191};
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

msEncrypt = new MemoryStream();
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
        swEncrypt.Write(plainText);
    }
}

return msEncrypt.ToArray();

回答by Oscar Kilhed

The key property of the Rijndael instance takes a byte[] as the key. Make sure you set it to an array with a valid size for the algorithm.

Rijndael 实例的 key 属性以 byte[] 作为键。确保将其设置为具有算法有效大小的数组。

Link to msdn: http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.key.aspx

链接到 msdn:http: //msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.key.aspx

回答by Graviton

What do you mean by cannot provide the key of our own? Here's an exampleon how you do it.

不能提供我们自己的密钥是什么意思?这是一个关于你如何做的例子

public static string Encrypt(string Text, byte[] key, byte[] VectorBytes){
    try{
        byte[] TextBytes = Encoding.UTF8.GetBytes(Text);        
        RijndaelManaged rijKey = new RijndaelManaged();
        rijKey.Mode = CipherMode.CBC; 
        ICryptoTransform encryptor = rijKey.CreateEncryptor(key,VectorBytes); 
        MemoryStream memoryStream = new MemoryStream(); 
        cryptoStream.Write(TextBytes, 0, TextBytes.Length); 
        cryptoStream.FlushFinalBlock(); 
        byte[] cipherTextBytes = memoryStream.ToArray();
        memoryStream.Close();
        cryptoStream.Close(); 
        string cipherText = Convert.ToBase64String(cipherTextBytes); 
        return cipherText;
    } 
    catch (Exception e){
        MessageBox.Show("Falsches Passwort "+ e.Message.ToString());
        string t = "";
        return t;
    }
}