使用rijndael解密时出现问题

编程入门 行业动态 更新时间:2024-10-25 22:25:56
本文介绍了使用rijndael解密时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Hii .....我制作了一个用于加密和解密文件的程序,有人能指导我这段代码有什么问题,因为它没有给我任何错误或异常,但没有正确的解密方法place.Button1_click用于加密 和button2_click用于解密.

Hii.....i made a program for encryption and decryption of files usign rijndael.can anyone guide me what's the problem with this code because it is not giving me any error or exception yet no correct decryption takes place.Button1_click is for encryption and button2_click is for decryption.

byte [] tmpSource; byte [] tmpHash;

byte[] tmpSource; byte[] tmpHash;

受保护的无效Button1_click(对象发送者,EventArgs e) { 如果(FileUpload.HasFile) { tmpSource = ASCIIEncoding.ASCII.GetBytes(this.TextBox1.Text); tmpHash =新的MD5CryptoServiceProvider().ComputeHash(tmpSource); 字符串rturn = ByteArrayToString(tmpHash); byte []键= Encoding.ASCII.GetBytes(rturn); FileStream outputStream = new FileStream("c://encryp.txt",FileMode.OpenOrCreate,FileAccess.ReadWrite); byte [] data = FileUpload.FileBytes; SymmetricAlgorithm myAlgo = new RijndaelManaged(); myAlgo.BlockSize = 128; myAlgo.GenerateIV(); 字节[] iv = myAlgo.IV​​; ICryptoTransform加密器; cryptoor = myAlgo.CreateEncryptor(key,iv); CryptoStream cryptoStream =新的CryptoStream(outputStream,加密器,CryptoStreamMode.Write); cryptoStream.Write(data,0,data.Length); cryptoStream.FlushFinalBlock(); cryptoStream.Close(); byte [] mydata = System.IO.File.ReadAllBytes("C://encryp.txt"); System.IO.FileStream fs =新的FileStream("c://" + FileUpload .FileName,FileMode.Create); fs.Write(key,0,key.Length); fs.Write(iv,0,iv.Length); fs.Write(mydata,0,mydata.Length); fs.Flush(); fs.Close(); this.Title ="done"; System.IO.File.Delete("c://encryp.txt"); } } 静态字符串ByteArrayToString(byte [] arrInput) { int i; StringBuilder sOutput =新的StringBuilder(arrInput.Length); for(i = 0; i< arrInput.Length; i ++) { sOutput.Append(arrInput [i] .ToString("X2"))); } 返回sOutput.ToString(); }

protected void Button1_click(object sender, EventArgs e) { if (FileUpload.HasFile) { tmpSource = ASCIIEncoding.ASCII.GetBytes(this.TextBox1.Text); tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource); string rturn = ByteArrayToString(tmpHash); byte[] key = Encoding.ASCII.GetBytes(rturn); FileStream outputStream = new FileStream("c://encryp.txt" , FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] data = FileUpload.FileBytes; SymmetricAlgorithm myAlgo = new RijndaelManaged(); myAlgo.BlockSize = 128; myAlgo.GenerateIV(); byte[] iv = myAlgo.IV; ICryptoTransform encryptor; encryptor = myAlgo.CreateEncryptor(key, iv); CryptoStream encryptStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write); encryptStream.Write(data, 0, data.Length); encryptStream.FlushFinalBlock(); encryptStream.Close(); byte[] mydata = System.IO.File.ReadAllBytes("C://encryp.txt"); System.IO.FileStream fs = new FileStream("c://"+FileUpload .FileName , FileMode.Create); fs.Write(key, 0, key.Length); fs.Write(iv, 0, iv.Length); fs.Write(mydata, 0, mydata.Length); fs.Flush(); fs.Close(); this.Title = "done"; System.IO.File.Delete( "c://encryp.txt"); } } static string ByteArrayToString(byte[] arrInput) { int i; StringBuilder sOutput = new StringBuilder(arrInput.Length); for (i = 0; i < arrInput.Length; i++) { sOutput.Append(arrInput[i].ToString("X2")); } return sOutput.ToString(); }

受保护的无效Button2_click(对象发送者,EventArgs e) { 如果(FileUpload.HasFile) { 试试 { byte []键=新的字节[32]; byte [] iv =新字节[16]; //step2:创建对称算法对象 SymmetricAlgorithm myalgo = new RijndaelManaged(); byte [] edata = FileUpload.FileBytes; //this.Label2.Text = edata.Length.ToString(); ] // this.Literal1.Text = edata.Length.ToString(); byte [] cryptdata =新的byte [(int)edata.Length]; //this.Label2.Text ="; for(int i = 0; i<(int)edata.Length; i ++) { b如果(i< 32) nbsp; bsp key [i] = edata [i]; b否则(i> = 32& i< 48) nbsp; bsp iv [i-32] = edata [i]; b否则,如果(i> = 48& i<(int)edata.Length) nbsp; bsp cryptdata [i-48] = edata [i]; } tmpSource = ASCIIEncoding.ASCII.GetBytes(this.TextBox1.Text); tmpHash =键; byte [] tmpNewHash; FileStream deoutputStream = new FileStream("c://decryp.txt",FileMode.OpenOrCreate,FileAccess.ReadWrite); tmpNewHash =新的MD5CryptoServiceProvider().ComputeHash(tmpSource); 字符串结果= ByteArrayToString(tmpNewHash); tmpNewHash = ASCIIEncoding.ASCII.GetBytes(result); bool bEqual = false; 如果(tmpNewHash.Length == tmpHash.Length) { b int i = 0; b while((i< tmpNewHash.Length)&&(tmpNewHash [i] == tmpHash [i])) b { nbsp; bsp 我+ = 1; b } b如果(i == tmpNewHash.Length) b { nbsp; bsp bEqual = true; b } } 如果(bEqual) { b //MessageBox.Show(密码匹配等于"); b myalgo.Key =键; b myalgo.IV​​ = iv; b ICryptoTransform dncryptor = myalgo.CreateDecryptor(key,iv); b CryptoStream dncryptStream =新的CryptoStream(deoutputStream,dncryptor,CryptoStreamMode.Write); b dncryptStream.Write(cryptdata,0,cryptdata.Length); b dncryptStream.FlushFinalBlock(); b dncryptStream.Close(); b deoutputStream.Close(); b Literal1.Text = ByteArrayToString(cryptdata); b byte [] mydata = System.IO.File.ReadAllBytes("c://decryp.txt"); b System.IO.FileStream fs =新的FileStream("c://" + FileUpload.FileName,FileMode.Create); b fs.Write(mydata,0,mydata.Length); b fs.Close(); b System.IO.File.Delete("c://decryp.txt"); } 其他 { b Literal1.Text =错误"; } } catch(异常例外) { } }

protected void Button2_click(object sender, EventArgs e) { if (FileUpload.HasFile) { try { byte[] key = new byte[32]; byte[] iv = new byte[16]; //step2: Create the symmetric algorithm object SymmetricAlgorithm myalgo = new RijndaelManaged(); byte[] edata = FileUpload.FileBytes; // this.Label2.Text = edata.Length.ToString(); // this.Literal1.Text = edata.Length.ToString(); byte[] cryptdata = new byte[(int)edata.Length]; // this.Label2.Text = ""; for (int i = 0; i < (int)edata.Length; i++) { if (i < 32) key[i] = edata[i]; else if (i >= 32 && i < 48) iv[i - 32] = edata[i]; else if (i >= 48 && i < (int)edata.Length) cryptdata[i - 48] = edata[i]; } tmpSource = ASCIIEncoding.ASCII.GetBytes(this.TextBox1.Text); tmpHash = key; byte[] tmpNewHash; FileStream deoutputStream = new FileStream("c://decryp.txt" , FileMode.OpenOrCreate , FileAccess.ReadWrite); tmpNewHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource); string result = ByteArrayToString(tmpNewHash); tmpNewHash = ASCIIEncoding.ASCII.GetBytes(result); bool bEqual = false; if (tmpNewHash.Length == tmpHash.Length) { int i = 0; while ((i < tmpNewHash.Length) && (tmpNewHash[i] == tmpHash[i])) { i += 1; } if (i == tmpNewHash.Length) { bEqual = true; } } if (bEqual) { //MessageBox.Show("password is matched equal"); myalgo.Key = key; myalgo.IV = iv; ICryptoTransform dncryptor = myalgo.CreateDecryptor(key, iv); CryptoStream dncryptStream = new CryptoStream(deoutputStream, dncryptor, CryptoStreamMode.Write); dncryptStream.Write(cryptdata, 0, cryptdata.Length); dncryptStream.FlushFinalBlock(); dncryptStream.Close(); deoutputStream.Close(); Literal1.Text = ByteArrayToString(cryptdata); byte[] mydata = System.IO.File.ReadAllBytes("c://decryp.txt"); System.IO.FileStream fs = new FileStream("c://" + FileUpload.FileName, FileMode.Create); fs.Write(mydata, 0, mydata.Length); fs.Close(); System.IO.File.Delete("c://decryp.txt"); } else { Literal1.Text = "error"; } } catch (Exception ex) { } }

推荐答案

我将对其进行研究,稍后再给予答复.

I will research it and give a reply later.

最诚挚的问候,

Guang-Ming Bian- MSFT

> MSDN订户支持 在论坛中

如果您有任何反馈意见有关我们的支持,请联系 msdnmg@microsoft

If you have any feedback on our support, please contact msdnmg@microsoft

更多推荐

使用rijndael解密时出现问题

本文发布于:2023-11-17 17:14:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1610421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:rijndael

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!