I am try crypt and decrypt the same text. I am use a symetric algorithm (Rijndael), but I don´t understand what happend whit my code. This is the test.
1. I write a text in a textbox (txtSrc).
2. I read the text, crypt it and write it in a label (lblCph1).
3. I read the text in the label and again crypt it y write the result in other label (lblCph2).
In the point 2, I expect the text of the label with strange simbols (cipher it), and in the pint 3 I expect the text of the label in plain text, but it is in strange simbols too. I don´t undertand, I think what if I crypt a text with the same key a pair of time, it will be decrypted. ¿Someone can explain to me?
I have write a example code.
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Security.Cryptography;<br />
using System.IO;<br />
<br />
namespace PruebaSimetrica<br />
{<br />
public partial class Form1 : Form<br />
{<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
private void Form1_Load(object sender, EventArgs e)<br />
{<br />
<br />
}<br />
<br />
private void btnCph1_Click(object sender, EventArgs e)<br />
{<br />
byte[] RellenoKey = new byte[] { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };<br />
byte[] RellenoIV = new byte[] { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };<br />
Rijndael RijndaelAlg = Rijndael.Create();<br />
RijndaelAlg.Key = RellenoKey;<br />
RijndaelAlg.IV = RellenoIV;<br />
byte[] bytePlainText = Encoding.Default.GetBytes(txtSrc.Text);<br />
byte[] byteCryptText;<br />
<br />
bytePlainText = Encoding.Default.GetBytes(txtSrc.Text);<br />
<br />
MemoryStream mStream = new MemoryStream();<br />
CryptoStream cStream = new CryptoStream(mStream, RijndaelAlg.CreateEncryptor(RijndaelAlg.Key, RijndaelAlg.IV), CryptoStreamMode.Write);<br />
<br />
cStream.Write(bytePlainText, 0, bytePlainText.Length);<br />
cStream.FlushFinalBlock();<br />
<br />
byteCryptText = mStream.ToArray();<br />
<br />
lblcph1.Text = System.Text.Encoding.Default.GetString(byteCryptText);<br />
<br />
cStream.Close();<br />
mStream.Close();<br />
}<br />
<br />
private void btnCph2_Click(object sender, EventArgs e)<br />
{<br />
byte[] RellenoKey = new byte[] { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };<br />
byte[] RellenoIV = new byte[] { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };<br />
Rijndael RijndaelAlg = Rijndael.Create();<br />
RijndaelAlg.Key = RellenoKey;<br />
RijndaelAlg.IV = RellenoIV;<br />
byte[] bytePlainText ;<br />
byte[] byteCryptText=Encoding.Default.GetBytes(lblcph1.Text);<br />
<br />
byteCryptText = Encoding.Default.GetBytes(lblcph1.Text);<br />
<br />
MemoryStream mStream = new MemoryStream();<br />
<br />
CryptoStream cStream = new CryptoStream(mStream, RijndaelAlg.CreateEncryptor(RijndaelAlg.Key, RijndaelAlg.IV), CryptoStreamMode.Write);<br />
<br />
cStream.Write(byteCryptText, 0, byteCryptText.Length);<br />
cStream.FlushFinalBlock();<br />
<br />
bytePlainText = mStream.ToArray();<br />
<br />
lblCph2.Text = System.Text.Encoding.Default.GetString(bytePlainText);<br />
<br />
cStream.Close();<br />
mStream.Close();<br />
}<br />
<br />
}<br />
}
Thanks,
Fernando
|