Click to See Complete Forum and Search --> : MD5 Encoding problem


zacharya
February 17th, 2005, 01:48 AM
I have a problem using md5 encryption method
I've look few samples that I have to convert my string data to byte[] in order to
encrypt it using computehash() then convert it back to string using the encoding object.
I already did all those steps but in the end when I want to display the encrypted string, I got few characters missing (replaced by cube characted), I supposed the problem is the encoding part.
So I changed it using UTF8 encoding....but still it doesn't displayed right...

here is my code, I'm using c#

encryption.aspx

<%@ Page language="c#" Codebehind="encryption.aspx.cs" AutoEventWireup="false" Inherits="rz2.EncryptionForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="EncryptionForm" method="post" runat="server">
<asp:Label id="lbl1" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 56px" runat="server"
Width="104px" Height="16px">Plain String :</asp:Label>
<asp:TextBox id="plainStrTxt" style="Z-INDEX: 102; LEFT: 192px; POSITION: absolute; TOP: 56px"
runat="server" Width="304px"></asp:TextBox>
<asp:Label id="lbl2" style="Z-INDEX: 103; LEFT: 48px; POSITION: absolute; TOP: 104px" runat="server"
Width="120px" Height="16px" Visible="False">Encrypted String :</asp:Label>
<asp:Label id="encryptedStrLbl" style="Z-INDEX: 104; LEFT: 200px; POSITION: absolute; TOP: 104px"
runat="server" Width="296px" Height="24px" Visible="False"></asp:Label>
<asp:Button id="submitBtn" style="Z-INDEX: 105; LEFT: 200px; POSITION: absolute; TOP: 152px"
runat="server" Width="112px" Text="Encrypt String"></asp:Button></form>
</body>
</HTML>




encryption.aspx.cs

...

private void submitBtn_Click(object sender, System.EventArgs e)
{
if( plainStrTxt.Text.Trim().Equals("") )
return;


byte[] plain = System.Text.Encoding.Unicode.GetBytes(plainStrTxt.Text.Trim());

MD5 md5 = new MD5CryptoServiceProvider();

byte[] res = md5.ComputeHash(plain);

encryptedStrLbl.Text = System.Text.Encoding.Unicode.GetString(res);

lbl2.Visible = true;
encryptedStrLbl.Visible = true;
}

...



what should I do to get the proper and readable encrypted string?

thanks before