我在Cryptostream损坏数据C#时遇到问题

时间:2018-09-24 23:31:46

标签: c# cryptography corruption rijndaelmanaged

我正在尝试为自己创建一个具有专门类的dll的dll,而在开发它并测试该方法(意味着它不是一个dll而是一个方法)时,它工作正常,没有破坏任何东西,但是现在有些奇怪原因是该方法对我的数据有些奇怪(例如txt文件),这里是原始txt:

line1 testing testing testing
line2 testing testing testing
line3 testing testing testing
line4 testing testing testing

这是加密和解密后的结果:

wš0åmŦI%1ˆìµsting testing
line2 testing testing testing
line3 testing testing testing
line4 testing testing testing

这是两个方法:

public static void EncryptFile(string inputFile, string outputFile)
        {
            try
            {
                StackFrame frame = new StackFrame(1);
                var method = frame.GetMethod();
                var type = method.DeclaringType;
                //var name = method.Name;
                //var assembly = typeof(Crypto).Assembly;
                //var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
                var id = type.GUID.ToString();
                StringBuilder sb = new StringBuilder(id);
                var Code = sb.Replace("-", "").ToString();
                UnicodeEncoding UE = new UnicodeEncoding();
                ASCIIEncoding AsciieEncoding = new ASCIIEncoding();
                byte[] key = AsciieEncoding.GetBytes(Code);

                string cryptFile = outputFile;
                FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

                RijndaelManaged RMCrypto = new RijndaelManaged();
                RMCrypto.GenerateIV();
                byte[] IV = RMCrypto.IV;
                RMCrypto.Padding = PaddingMode.ISO10126;
                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateEncryptor(key, IV),
                    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();
                RegistryKey Regkey =   Registry.LocalMachine.OpenSubKey("Software", true);
                Regkey.CreateSubKey("HBH");
                Regkey = Regkey.OpenSubKey("HBH", true);
                Regkey.CreateSubKey("Data");
                Regkey = Regkey.OpenSubKey("Data", true);
                long longIV = BitConverter.ToInt64(IV, 0);
                Regkey.SetValue("Data", longIV);
                Console.WriteLine("Encryption Succeeded !");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Encryption failed! Error");
                Console.WriteLine("Do you Want to see the error message ? (y/n)");
                string Response = Console.ReadLine();
                if (Response == "y" || Response == "Y")
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    Application.Exit();
                }
                else
                {
                    Application.Exit();
                }
            }
        }

这是解密方法:

public static void DecryptFile(string inputFile, string outputFile)
        {
            try
            {
                StackFrame frame = new StackFrame(1);
                var method = frame.GetMethod();
                var type = method.DeclaringType;
                var id = type.GUID.ToString();
                //var assembly = typeof(Crypto).Assembly;
                //var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
                //var id = attribute.Value;
                StringBuilder sb = new StringBuilder(id);
                var Code = sb.Replace("-", "").ToString();
                ASCIIEncoding AsciiEncoding = new ASCIIEncoding();
                byte[] key = AsciiEncoding.GetBytes(Code);
                RegistryKey Regkey = Registry.LocalMachine.OpenSubKey("Software", true);
                Regkey.OpenSubKey("HBH");
                Regkey = Regkey.OpenSubKey("HBH", true);
                Regkey.OpenSubKey("Data");
                Regkey = Regkey.OpenSubKey("Data", true);
                byte[] IV = Regkey.GetValue("Data") as byte[];
                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

                RijndaelManaged RMCrypto = new RijndaelManaged();
                RMCrypto.Padding = PaddingMode.ISO10126;
                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateDecryptor(key, IV),
                    CryptoStreamMode.Read);

                FileStream fsOut = new FileStream(outputFile, FileMode.Create);
                cs.Flush();
                int data;
                while ((data = cs.ReadByte()) != -1)
                    fsOut.WriteByte((byte)data);

                fsOut.Close();
                cs.Close();
                fsCrypt.Close();
                Console.WriteLine("Decryption Succeeded !");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Decryption failed! Error");
                Console.WriteLine("Do you Want to see the error message ? (y/n)");
                string Response = Console.ReadLine();
                if (Response == "y" || Response == "Y")
                {
                    Console.WriteLine(ex.Message +"at line :" + ex.StackTrace);
                    Console.ReadLine();
                    Application.Exit();
                }
                else
                {
                    Application.Exit();
                }
            }
        }

0 个答案:

没有答案