Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文本文件的加密和解密_C#_Winforms_Encryption_Cryptography - Fatal编程技术网

C# 文本文件的加密和解密

C# 文本文件的加密和解密,c#,winforms,encryption,cryptography,C#,Winforms,Encryption,Cryptography,好的,我有一个小程序,我在C#winforms中构建了它,它基本上使用了安全密码学,我试图找出我在代码中犯了什么错误。它可以很好地加密文本文件。但是,当我将加密文件粘贴到文本框中并点击Decrypt时,它不会解密文件,而是再次加密 我想知道我是否把代码弄错了 namespace Encrypted { class Encryptor { public static string IV = "1a1a1a1a1a1a1a1a"; public sta

好的,我有一个小程序,我在C#winforms中构建了它,它基本上使用了安全密码学,我试图找出我在代码中犯了什么错误。它可以很好地加密文本文件。但是,当我将加密文件粘贴到文本框中并点击Decrypt时,它不会解密文件,而是再次加密

我想知道我是否把代码弄错了

namespace Encrypted
{
    class Encryptor
    {
        public static string IV = "1a1a1a1a1a1a1a1a";
        public static string Key = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a";

        public static string Encrypt (string decrypted)
        {
            byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted);
            AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
            endec.BlockSize = 128;
            endec.KeySize = 256;
            endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
            endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            endec.Padding = PaddingMode.PKCS7;
            endec.Mode = CipherMode.CBC;
            ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV);
            byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
            icrypt.Dispose();
            return Convert.ToBase64String(enc);
        }

        public static string Decrypted(string encrypted)
        {
            byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(encrypted);
            AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
            endec.BlockSize = 128;
            endec.KeySize = 256;
            endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
            endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            endec.Padding = PaddingMode.PKCS7;
            endec.Mode = CipherMode.CBC;
            ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV);
            byte[] enc = icrypt.TransformFinalBlock(textbytes, 0,     textbytes.Length);
            icrypt.Dispose();
            return ASCIIEncoding.ASCII.GetString(enc);
        }
    }
 }
这是上面的课程,下面是表格1:

namespace Encrypted
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            string dir = textBox1.Text;
            Directory.CreateDirectory("data\\" + dir);

            var sw = new StreamWriter("data\\" + dir + "data.ls");

            string enctxt = Encryptor.Encrypt(textBox1.Text);

            sw.WriteLine(enctxt);

            sw.Close();
        }

        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            string dir = textBox2.Text;
            Directory.CreateDirectory("data\\" + dir);

            var sw = new StreamWriter("data\\" + dir + "data.ls");

            string enctxt = Encryptor.Decrypted(textBox2.Text);

            sw.WriteLine(enctxt);

            sw.Close();
        }
    }

在“解密”按钮中,我是否需要使用StreamReader或StreamWriter。一旦加密的文件在data.ls中,我复制它并将其粘贴到文本框中进行解密。但是,is不会解密,它只会重新加密文件。有什么我做错了吗?

这是因为您正在使用
Decrypted()
通过创建
加密器来加密输入文件。它应该创建一个
解密器来执行解密操作。这一个单词的改变将逆转这一过程。AESCryptServiceProvider的类将使用指定的密钥和初始化向量(IV)创建对称AES解密器对象

即,更改
Decrypted()
方法中的以下行

  ICryptoTransform icrypt = endec.CreateDecryptor(endec.Key, endec.IV);

这里是一个参考示例,您可以看到一个工作示例,这不仅仅是不使用CreateDecryptor的错误。您还必须解码base64中的密文字符串,这是加密返回的内容

您正在这样做:

  • 获取字符串,从ASCII解码为明文字节

  • 获取明文字节,加密为密文字节

  • 获取密文字节,转换为base64字符串

  • 然后你要这样做:


  • 以字符串为例,从ASCII解码为密文字节字节->加密字节->base64->加密字节->字节->字符串-/p>我搜索了它,并制作了一个与我配合良好的程序:) 所以

    这是一节课

       class Encryptor
    {
        public static string IV = "1a1a1a1a1a1a1a1a";
        public static string Key = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a13";
    
        public static string Encrypt(string decrypted)
        {
            byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted);
            AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
            endec.BlockSize = 128;
            endec.KeySize = 256;
            endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
            endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            endec.Padding = PaddingMode.PKCS7;
            endec.Mode = CipherMode.CBC;
            ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV);
            byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
            icrypt.Dispose();
            return Convert.ToBase64String(enc);
        }
    
        public static string Decrypted(string encrypted)
        {
            byte[] textbytes = Convert.FromBase64String(encrypted);
            AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
            endec.BlockSize = 128;
            endec.KeySize = 256;
            endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
            endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            endec.Padding = PaddingMode.PKCS7;
            endec.Mode = CipherMode.CBC;
            ICryptoTransform icrypt = endec.CreateDecryptor(endec.Key, endec.IV);
            byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
            icrypt.Dispose();
            return System.Text.ASCIIEncoding.ASCII.GetString(enc);
        }
    }
    

    这是表格`

            private void button1_Click(object sender, EventArgs e)
        {
            string dir = textBox1.Text;
            Directory.CreateDirectory("data\\" + dir);
    
            var sw = new StreamWriter("data\\" + dir + "data.ls");
    
            string enctxt = Encryptor.Encrypt(textBox1.Text);
    
            sw.WriteLine(enctxt);
    
            sw.Close();
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            string dir = textBox2.Text;
    
            StreamReader sr = new StreamReader(Application.StartupPath +"\\data\\"+ dir + "data.ls");
            string line = sr.ReadLine();
    
            textBox1.Text = Encryptor.Decrypted(Convert.ToString(line));
        }
    


    致以最良好的祝愿

    所以
    Decrypted
    方法应该调用
    CreateDecryptor
    (而不是
    CreateEncryptor
    ),对吗?当我调用CreateDecryptor时,它抛出一个错误,说块不是完整块。下一个最明显的问题是,您没有对加密字符串使用相同的编码。您从encrypt返回一个base-64字符串,但在
    解密
    中编码时使用的是ASCII。您应该使用
    Convert.FromBase64String
    进行编码,以获取
    textbytes
    @mike。现在,它会抛出一个错误异常:mscorlib.dll中的“System.ArgumentException”。其他信息:路径中的非法字符。您正在使用输入数据创建目录。如果该数据包含无法在路径中使用的字符,那么您就倒霉了。我设计它的方法是询问用户输入数据和文件名。不管好坏如果我做了我建议的两个更改,Encryptor类对我来说可以正常工作。当我这样做时,它会抛出此错误,System.Core.dll中出现“System.Security.Cryptography.CryptographyException”类型的未处理异常。其他信息:输入数据不是一个完整的块。当我使用它时,它不会抛出任何错误这就是我所拥有的,我像你的答案一样改变了它,但它不起作用。ICryptoTransform icrypt=endec.CreateDecryptor(endec.Key,endec.IV)@campnerd:参见此参考,MSDN上的示例在控制台应用程序中显示。所以他们使用CryptoStreamMode.Writer。现在,如果我看一下我的代码,我正在使用流编写器。有区别吗?此外,MSDN使用静态字符串方法显示代码,而我使用的是公共静态字符串方法。这可能是调用错误。我所做的一切都符合MSDN,但当我按照您的样本和说明操作时,我发现了一个错误。但是在程序运行之前没有错误。好的,现在这比另一个人说的更有意义。。所以基本上我的代码是正确的,只是顺序不对。当我回到家,我将看到我做了什么改变。这在加密方面非常有效。然而,我得到了正确的密码解密顺序。我以为是我干的。我猜我开始做这个的时候没有吃足够的甜馅饼..哈哈