C#Rijndael加密不接受任何密钥;mykey123”;

本文关键字:mykey123 密钥 任何 加密 不接受 C#Rijndael | 更新日期: 2023-09-27 18:26:52

我在http://www.codeproject.com/Articles/26085/File-Encryption-and-Decryption-in-C.当我使用静态键// string password = @"myKey1234"; // Your Key Here时,它工作得很好。当我传入不同的密钥时,它不起作用string password = @keyPwd;。您可以在我的代码中看到,我正在向函数传递密钥,但它不起作用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSVEncrypts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string inputFile = "";
        string outputFilePath = "";
        string oFilePathName = "";
// EncryptFile
        private void EncryptFile(string inputFile, string outputFile,string keyPwd )
        {
            try
            {
               // string password = @"myKey123"; // Your Key Here
                string password = @keyPwd; 
                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);
                string cryptFile = outputFile;
                FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
                RijndaelManaged RMCrypto = new RijndaelManaged();
                CryptoStream cs = new CryptoStream(fsCrypt,RMCrypto.CreateEncryptor(key, key),CryptoStreamMode.Write);
                FileStream fsIn = new FileStream(inputFile, FileMode.Open);
                int data;
                while ((data = fsIn.ReadByte()) != -1)
                    cs.WriteByte((byte)data);
                fsIn.Close();
                cs.Close();
                fsCrypt.Close();
            }
            catch
            {
                MessageBox.Show("Encryption failed!", "Error");
            }
        }

// Decrypt
        private void DecryptFile(string inputFile, string outputFile, string keyPwd)
        {
            {
                //string password = @"myKey123"; // Your Key Here
                string password = @keyPwd; // Your Key Here
                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);
                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
                RijndaelManaged RMCrypto = new RijndaelManaged();
                CryptoStream cs = new CryptoStream(fsCrypt,
                RMCrypto.CreateDecryptor(key, key),CryptoStreamMode.Read);
                FileStream fsOut = new FileStream(outputFile, FileMode.Create);
                int data;
                while ((data = cs.ReadByte()) != -1)
                fsOut.WriteByte((byte)data);
                fsOut.Close();
                cs.Close();
                fsCrypt.Close();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
           if (inputFile != "")
            {
                oFilePathName = outputFilePath + "''" + textBox1.Text;
                EncryptFile(inputFile, oFilePathName,keytextBox.Text);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (inputFile != "") ;
            {
                oFilePathName = outputFilePath + "''" + textBox1.Text;
              DecryptFile(inputFile, oFilePathName, keytextBox.Text);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog InputOpenFileDialog1 = new OpenFileDialog();
            if (InputOpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string strInfilename = InputOpenFileDialog1.FileName;
                button3.Text = strInfilename;
                inputFile = strInfilename;
                outputFilePath = Path.GetDirectoryName(inputFile);
            }
        }

    }
}

C#Rijndael加密不接受任何密钥;mykey123”;

密钥应该只包含与随机无法区分的位。编码为字节的密码不是密钥。特别是当使用Unicode编码(其应该被命名为UTF16LE)时,许多比特被设置为零。这意味着;键";也不包含足够的熵。

要从密码创建密钥,您应该使用基于密码的密钥派生函数(PBKDF)来派生它。在当前的.NET Crypto API中,最好的方法可能是使用实现PBKDF2的类Rfc2898DeriveBytes。PBKDF2在RFC 2898:PKCS#5:基于密码的加密规范V2.0中定义。如果你想做基于密码的加密,你可能想读一下。

我认为Rijndael实际上是指AES(高级加密标准),AES是Rijndael128位块大小的子集,这正是您所需要的。

AES密钥是128、192或256位,如果您有一个字符串,最好不要使用它,首先通过密钥派生函数(如PBKDF2)运行它。键实际上应该是一个字节数组,而不是字符串。把钥匙做成正确的尺寸,这可能是你的问题。

CreateDecryptor有两个参数,key和iv,不要也为iv使用key,iv被认为是公共的。

从代码中还不清楚,您需要查阅文档,看看默认模式是否为CBC,以及是否启用了PKCS#7(PKCS#5填充可以互换使用)填充。

如果你想要一个安全的加密"开箱即用:使用RNCryptor,有一个C#版本。你还可以获得多语言/平台的互操作性。

正确的加密安全性并不容易,而且很容易犯下破坏安全性的错误。