Re: .NET Encryption/Decryption Problem -- Pls help
From: Asaf Goldman (asafgo@hotmail.com)
Date: 08/26/02
- Next message: Kevin Spencer: "Re: memory cache"
- Previous message: John Saunders: "Re: Equivalent to Permission Checker 'HasAccess' method?"
- In reply to: FOB: ".NET Encryption/Decryption Problem -- Pls help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
From: "Asaf Goldman" <asafgo@hotmail.com> Date: Mon, 26 Aug 2002 10:49:38 +0200
Write
-------------------------------------------------------
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Security.Cryptography" %>
<Script Runat="Server">
CONST DESKey As String = "ABCDEFGH"
CONST DESIV As String = "HGFEDCBA"
Function Convert2ByteArray( strInput As String ) As Byte()
Dim intCounter As Integer
Dim arrChar As Char()
arrChar = strInput.ToCharArray()
Dim arrByte( arrChar.Length - 1 ) As Byte
For intCounter = 0 To arrByte.Length - 1
arrByte( intCounter ) = Convert.ToByte( arrChar( intCounter ) )
Next
Return arrByte
End Function
Sub Button_Click( s As Object, e As EventArgs )
Dim arrDESKey As Byte()
Dim arrDESIV AS Byte()
Dim arrInput As Byte()
Dim objFileStream As FileStream
Dim objDES As DESCryptoServiceProvider
Dim objEncryptor As ICryptoTransform
Dim objCryptoStream As CryptoStream
arrDESKey = Convert2ByteArray( DESKey )
arrDESIV = Convert2ByteArray( DESIV )
arrInput = Convert2ByteArray( txtInput.Text )
objDES = New DESCryptoServiceProvider()
objEncryptor = objDES.CreateEncryptor( arrDESKey, arrDESIV )
objFileStream = New FileStream( _
MapPath( "secret.txt" ), _
FileMode.Create, _
FileAccess.Write )
objCryptoStream = New CryptoStream( _
objFileStream, _
objEncryptor, _
CryptoStreamMode.Write )
objCryptoStream.Write( arrInput, 0, arrInput.Length )
objCryptoStream.Close()
End Sub
</Script>
<html>
<head><title>SymmetricWrite.aspx</title></head>
<body>
<form Runat="Server">
<h2>DES Encryption</h2>
<b>Enter text to encrypt:</b>
<br>
<asp:TextBox
id="txtInput"
TextMode="Multiline"
Columns="50"
Rows="10"
Runat="Server" />
<p>
<asp:Button
Text="Encrypt!"
OnClick="Button_Click"
Runat="Server" />
</form>
</body>
</html>
Read------------------------------------------------------------------------
--------
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Security.Cryptography" %>
<Script Runat="Server">
CONST DESKey As String = "ABCDEFGH"
CONST DESIV As String = "HGFEDCBA"
Sub Page_load
DESDecrypt
End Sub
Function Convert2ByteArray( strInput As String ) As Byte()
Dim intCounter As Integer
Dim arrChar As Char()
arrChar = strInput.ToCharArray()
Dim arrByte( arrChar.Length - 1 ) As Byte
For intCounter = 0 To arrChar.Length - 1
arrByte( intCounter ) = Convert.ToByte( arrChar( intCounter ) )
Next
Return arrByte
End Function
Sub DESDecrypt
Dim arrDESKey As Byte()
Dim arrDESIV As Byte()
Dim objFileStream As FileStream
Dim objDES As DESCryptoServiceProvider
Dim objDecryptor As ICryptoTransform
Dim objCryptoStream As CryptoStream
arrDESKey = Convert2ByteArray( DESKey )
arrDESIV = Convert2ByteArray( DESIV )
objDES = New DESCryptoServiceProvider
objDecryptor = objDES.CreateDecryptor( arrDESKey, arrDESIV )
objFileStream = New FileStream( _
MapPath( "secret.txt" ), _
FileMode.Open, _
FileAccess.Read )
objCryptoStream = New CryptoStream( _
objFileStream, _
objDecryptor, _
CryptoStreamMode.Read )
lblMessage.Text = New StreamReader( objCryptoStream ).ReadToEnd()
objFileStream.Close
End Sub
</Script>
<html>
<head><title>SymmetricRead.aspx</title></head>
<body>
<h2>DES Decryption</h2>
<b>Decrypted Text:</b>
<p>
<asp:Label
ID="lblMessage"
Runat="Server" />
</body>
</html>
"FOB" <fob_in_states@yahoo.com> wrote in message
news:OCPus8ITCHA.4240@tkmsftngp08...
> Hello geeks,
> I am working on some server side infrastructure where I need to encrypt
some
> data and send it to the client so that the client could send that data
back
> to me for identification purposes. I am trying to use .NET cryptography
> classes but I am running into some problem. Probably problem is with
> DESCryptoServiceProvider or I am not able to accurately convert from
string
> to byte array and vice vera.
>
> At the end, I dont receive the actual string that I encrypted, rather the
> string is truncated, means loss of data. If I encrypt the string
> "01234567890123456789", I receive only "01234567" !!!!
>
> I would greatly appreciate if someone could point out the problem.
>
> Following is the code that I am using:
>
> private void button1_Click(object sender, System.EventArgs e)
> {
> Byte[] arrKey ;
> arrKey = GetByteArray(txtKeyPassword.Text) ;
>
> Byte[] arrIV ;
> arrIV = GetByteArray(txtIVPassword.Text) ;
>
> // Data to encrypt
> string strData = "01234567890123456789" ;
>
> // Convert string data to byte array
> System.Text.UTF8Encoding objEncoding = new System.Text.UTF8Encoding() ;
> Byte[] arrUTF8 = objEncoding.GetBytes(strData) ;
>
> // Encrypt byte array
> DESCryptoServiceProvider objDES = new DESCryptoServiceProvider() ;
> MemoryStream objMemoryStream = new MemoryStream() ;
> CryptoStream objCryptoStream = new CryptoStream( objMemoryStream,
> objDES.CreateEncryptor(arrKey, arrIV),
> CryptoStreamMode.Write) ;
> objCryptoStream.Write(arrUTF8, 0, arrUTF8.Length) ;
> objCryptoStream.Flush() ;
>
> Byte[] arrEncryptedData = objMemoryStream.ToArray() ;
> string strEncryptedData = Convert.ToBase64String(arrEncryptedData) ;
>
> objCryptoStream.Close() ;
> objMemoryStream.Close() ;
>
> // Decrypt Data.
> // First conver the user token from Base64 to byte array
> // then decrypt the data.
> Byte[] arrEncryptedData2 = Convert.FromBase64String(strEncryptedData) ;
> objMemoryStream = new MemoryStream() ;
> objCryptoStream = new CryptoStream( objMemoryStream,
> objDES.CreateDecryptor(arrKey, arrIV),
> CryptoStreamMode.Write) ;
> objCryptoStream.Write(arrEncryptedData2, 0, arrEncryptedData2.Length) ;
> objCryptoStream.Flush() ;
>
> // Get the byte array.
> Byte[] arrDecryptedData = objMemoryStream.ToArray() ;
>
> // Convert the array to string
> string strDecryptedData = objEncoding.GetString(arrDecryptedData) ;
> }
>
> private Byte[] GetByteArray(string r_strData)
> {
> Byte[] arrByte = new Byte[r_strData.Length] ;
> Encoding objEncoding = System.Text.Encoding.UTF8 ;
> arrByte = objEncoding.GetBytes(r_strData) ;
> return arrByte ;
> }
>
> Thanks,
> FOB
>
>
>
- Next message: Kevin Spencer: "Re: memory cache"
- Previous message: John Saunders: "Re: Equivalent to Permission Checker 'HasAccess' method?"
- In reply to: FOB: ".NET Encryption/Decryption Problem -- Pls help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]