Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encrypt / Decrypt

Status
Not open for further replies.

DH

Programmer
Dec 8, 2000
168
I am new to Encrypting and Decrypting text and would like to learn more.

I am working with the following code from ASP.NET Unleased. The code encrypts text and saves the text to a file. I can then decrypt the file when needed. To learn more I would like to:

1.) Display the encrypted text on the screen in the lbldecryptedtext.Text label

2.) Save the encrypted text to a database instead of a file.

Here is the code:

Const DESKey As String = "ABCDEFGH"
Const DESIV As String = "HGFEDCBA"

Function Convert2ByteArray(ByVal 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
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
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)
lblencyptedtext.Text = ????????????
objCryptoStream.Close()
End Sub
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)
lbldecryptedtext.Text = New StreamReader(objCryptoStream).ReadToEnd()
objFileStream.Close()
End Sub
Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click
DESDecrypt()
End Sub

Any suggestions on how to...

1.) Display the encrypted text on the screen in the lbldecryptedtext.Text label

2.) Save the encrypted text to a database instead of a file.

Thank you,

DH
 
1) Try stepping through the code and you'll see the encryption process taking place (arrDESIV is the byte array that holds each byte). Therefore something like the following should work:
Code:
        For Each y As Byte In arrDESIV
            lblencyptedtext.Text &= y
        Next

2) Rather than set "lblencyptedtext.Text" like I do in the above example, simply wtie it to a String and then insert the String into the db (I assume you know how to do an insert query to a db?).


----------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
I use this (it's in C#, sorry, but it should be straightforward to convert to VB.NET, as most of it are framework calls).
Code:
public static byte[] Encrypt(string source)
{
	RijndaelManaged myRijndael = new RijndaelManaged();
	UnicodeEncoding ue = new UnicodeEncoding(false, false);
	ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, iv);
			
	MemoryStream ms = new MemoryStream();
	CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);

	byte[] toEncrypt = ue.GetBytes(source);

	cs.Write(toEncrypt, 0, toEncrypt.Length);
	cs.FlushFinalBlock();

	return ms.ToArray();
}

public static string Decrypt(byte[] source)
{
	RijndaelManaged myRijndael = new RijndaelManaged();
	UnicodeEncoding ue = new UnicodeEncoding(false, false);
	ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, iv);

	MemoryStream ms = new MemoryStream(source);
	CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);

	byte[] decryptedBytes = new byte[source.Length];

	cs.Read(decryptedBytes, 0, decryptedBytes.Length);

	char[] nullChars = {'\x0000'};

	return ue.GetString(decryptedBytes).TrimEnd(nullChars);
}
the variable key is the key, and iv is the initialization vector, defined like this:
Code:
private static byte[] key = {
	0x0e, 0x75, 0x11, 0x97, 0xa7, 0x3, 0x56, 0x42, 
	0x47, 0xa0, 0xde, 0x2c, 0x4e, 0x45, 0x88, 0xd7, 
	0x8a, 0xbb, 0x82, 0xb3, 0xce, 0x2, 0xa8, 0xe7, 
	0x59, 0xba, 0x87, 0x9d, 0xb2, 0x2e, 0x60, 0x75
	};
private static byte[] iv = {
	0xf4, 0x4f, 0xd6, 0xd8, 0x9c, 0x1c, 0x4e, 0xd7, 
	0x6b, 0x74, 0xfa, 0x05, 0xc5, 0x49, 0xf1, 0xb5
	};
There are almost certainly better places/ways to hide the key within the source code, but this is sufficient for me to use. I created the key and iv with this utility method:
Code:
public static void create_key_iv()
{
	RijndaelManaged myRijndael = new RijndaelManaged();

	myRijndael.GenerateKey();
	myRijndael.GenerateIV();

	//Get the key and IV.
	byte[] key1 = myRijndael.Key;
	byte[] IV1 = myRijndael.IV;

	for (int i = 0; i < key1.GetLength(0); i++)
		Console.WriteLine("0x" + key1[i].ToString("x") + ", ");

	for (int i = 0; i < IV1.GetLength(0); i++)
		Console.WriteLine("0x" + IV1[i].ToString("x") + ", ");
}
Hope this helps.
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top