Decrypting data

S

Steve Long

Hello,
I'm wondering if somebody can help me figure out how to decrypt data. I seem
to be able to encrypt data but have not been able to decrypt the very data
that I've encrypted. I know it's because I'm just not understanding the
whole process but I'm obviously just not getting it. Here's the encryption
function I'm using:

Private Sub Encryptpwd()
Dim cdk As PasswordDeriveBytes = New PasswordDeriveBytes("passwd",
Nothing)
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key() As Byte = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)

Console.WriteLine(key.Length * 8)
' Set up an RC2 object to encrypt with the derived key
Dim rc2 As RC2CryptoServiceProvider = New RC2CryptoServiceProvider
rc2.Key = key
Dim b() As Byte = {21, 22, 23, 24, 25, 26, 27, 28}
rc2.IV = b
Dim plaintext() As Byte = Encoding.UTF8.GetBytes("iiMap")
Dim ms As New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, rc2.CreateEncryptor(), _
CryptoStreamMode.Write)
cs.Write(plaintext, 0, plaintext.Length)
cs.Close()
Dim result() As Byte = ms.ToArray()
Dim asc As New ASCIIEncoding
txtResult.Text = asc.GetString(result)
ms.Close()
End Sub

When I try to decrypt the text that's in txtResult.Text, my result is always
and empty string:

Private Sub Decryptpwd()
Dim cdk As PasswordDeriveBytes = New
PasswordDeriveBytes("pa[]ss!!wo//dd", Nothing)
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key() As Byte = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)

Console.WriteLine(key.Length * 8)
' Set up an RC2 object to encrypt with the derived key
Dim rc2 As RC2CryptoServiceProvider = New RC2CryptoServiceProvider
rc2.Key = key
Dim b() As Byte = {21, 22, 23, 24, 25, 26, 27, 28}
rc2.IV = b
Dim plaintext() As Byte = Encoding.UTF8.GetBytes(txtResult.Text)

Dim ms As New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, rc2.CreateDecryptor(), _
CryptoStreamMode.Write)
cs.Write(plaintext, 0, plaintext.Length)
Dim result(plaintext.Length - 1) As Byte
ms.Read(result, 0, result.Length - 1)
Dim asc As New ASCIIEncoding
txtDecrypt.Text = asc.GetString(result)
cs.Close()
ms.Close()
End Sub

I would very much appreciate someone setting me straight on this issue.
Thanks in advance
Steve
P.S. I posted this in the security group but didn't get an answer to it. I'm
hoping I have better luck here.
 
J

Jay B. Harlow [MVP - Outlook]

Steve,
Numerous little problems:

1) different passwords
2) different encodings. I would recommend using Encoding.Default instead of
a mix of UTF8 & ASCII. At the very least use an 8 bit encoding such as UTF8.
I would avoid ASCII as its a 7 bit encoding & you will loose the high order
bits!
3) missing CryptoStream.FlushFinalBlock after the "last" write to the
CryptoStream.
4) not resetting the position of the output stream to the beginning of the
stream before attempting to read what was written.

Try something like:

Private Sub EncryptPassword()
Dim cdk As PasswordDeriveBytes = New
PasswordDeriveBytes("pa[]ss!!wo//dd", Nothing)
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key() As Byte = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)

Console.WriteLine(key.Length * 8)
' Set up an RC2 object to encrypt with the derived key
Dim rc2 As RC2CryptoServiceProvider = New RC2CryptoServiceProvider
rc2.Key = key
Dim b() As Byte = {21, 22, 23, 24, 25, 26, 27, 28}
rc2.IV = b
Dim plaintext() As Byte = Encoding.Default.GetBytes("iiMap")
Dim ms As New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, rc2.CreateEncryptor(),
_
CryptoStreamMode.Write)
cs.Write(plaintext, 0, plaintext.Length)
cs.FlushFinalBlock()
cs.Close()
Dim result() As Byte = ms.ToArray()
txtResult.Text = Encoding.Default.GetString(result)
ms.Close()
End Sub

Private Sub DecryptPassword()
Dim cdk As PasswordDeriveBytes = New
PasswordDeriveBytes("pa[]ss!!wo//dd", Nothing)
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key() As Byte = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)

Console.WriteLine(key.Length * 8)
' Set up an RC2 object to encrypt with the derived key
Dim rc2 As RC2CryptoServiceProvider = New RC2CryptoServiceProvider
rc2.Key = key
Dim b() As Byte = {21, 22, 23, 24, 25, 26, 27, 28}
rc2.IV = b
Dim plaintext() As Byte = Encoding.Default.GetBytes(txtResult.Text)

Dim ms As New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, rc2.CreateDecryptor(),
_
CryptoStreamMode.Write)
cs.Write(plaintext, 0, plaintext.Length)
cs.FlushFinalBlock()
ms.Position = 0L
Dim result(plaintext.Length - 1) As Byte
ms.Read(result, 0, result.Length - 1)
txtDecrypt.Text = Encoding.Default.GetString(result)
cs.Close()
ms.Close()
End Sub

Hope this helps
Jay



Steve Long said:
Hello,
I'm wondering if somebody can help me figure out how to decrypt data. I
seem
to be able to encrypt data but have not been able to decrypt the very data
that I've encrypted. I know it's because I'm just not understanding the
whole process but I'm obviously just not getting it. Here's the encryption
function I'm using:

Private Sub Encryptpwd()
Dim cdk As PasswordDeriveBytes = New PasswordDeriveBytes("passwd",
Nothing)
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key() As Byte = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)

Console.WriteLine(key.Length * 8)
' Set up an RC2 object to encrypt with the derived key
Dim rc2 As RC2CryptoServiceProvider = New RC2CryptoServiceProvider
rc2.Key = key
Dim b() As Byte = {21, 22, 23, 24, 25, 26, 27, 28}
rc2.IV = b
Dim plaintext() As Byte = Encoding.UTF8.GetBytes("iiMap")
Dim ms As New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, rc2.CreateEncryptor(),
_
CryptoStreamMode.Write)
cs.Write(plaintext, 0, plaintext.Length)
cs.Close()
Dim result() As Byte = ms.ToArray()
Dim asc As New ASCIIEncoding
txtResult.Text = asc.GetString(result)
ms.Close()
End Sub

When I try to decrypt the text that's in txtResult.Text, my result is
always
and empty string:

Private Sub Decryptpwd()
Dim cdk As PasswordDeriveBytes = New
PasswordDeriveBytes("pa[]ss!!wo//dd", Nothing)
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key() As Byte = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)

Console.WriteLine(key.Length * 8)
' Set up an RC2 object to encrypt with the derived key
Dim rc2 As RC2CryptoServiceProvider = New RC2CryptoServiceProvider
rc2.Key = key
Dim b() As Byte = {21, 22, 23, 24, 25, 26, 27, 28}
rc2.IV = b
Dim plaintext() As Byte = Encoding.UTF8.GetBytes(txtResult.Text)

Dim ms As New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, rc2.CreateDecryptor(),
_
CryptoStreamMode.Write)
cs.Write(plaintext, 0, plaintext.Length)
Dim result(plaintext.Length - 1) As Byte
ms.Read(result, 0, result.Length - 1)
Dim asc As New ASCIIEncoding
txtDecrypt.Text = asc.GetString(result)
cs.Close()
ms.Close()
End Sub

I would very much appreciate someone setting me straight on this issue.
Thanks in advance
Steve
P.S. I posted this in the security group but didn't get an answer to it.
I'm
hoping I have better luck here.
 
S

Steve Long

Thanks Jay, I'll give it a whirl

Appreciate the help.
Steve

Jay B. Harlow said:
Steve,
Numerous little problems:

1) different passwords
2) different encodings. I would recommend using Encoding.Default instead of
a mix of UTF8 & ASCII. At the very least use an 8 bit encoding such as UTF8.
I would avoid ASCII as its a 7 bit encoding & you will loose the high order
bits!
3) missing CryptoStream.FlushFinalBlock after the "last" write to the
CryptoStream.
4) not resetting the position of the output stream to the beginning of the
stream before attempting to read what was written.

Try something like:

Private Sub EncryptPassword()
Dim cdk As PasswordDeriveBytes = New
PasswordDeriveBytes("pa[]ss!!wo//dd", Nothing)
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key() As Byte = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)

Console.WriteLine(key.Length * 8)
' Set up an RC2 object to encrypt with the derived key
Dim rc2 As RC2CryptoServiceProvider = New RC2CryptoServiceProvider
rc2.Key = key
Dim b() As Byte = {21, 22, 23, 24, 25, 26, 27, 28}
rc2.IV = b
Dim plaintext() As Byte = Encoding.Default.GetBytes("iiMap")
Dim ms As New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, rc2.CreateEncryptor(),
_
CryptoStreamMode.Write)
cs.Write(plaintext, 0, plaintext.Length)
cs.FlushFinalBlock()
cs.Close()
Dim result() As Byte = ms.ToArray()
txtResult.Text = Encoding.Default.GetString(result)
ms.Close()
End Sub

Private Sub DecryptPassword()
Dim cdk As PasswordDeriveBytes = New
PasswordDeriveBytes("pa[]ss!!wo//dd", Nothing)
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key() As Byte = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)

Console.WriteLine(key.Length * 8)
' Set up an RC2 object to encrypt with the derived key
Dim rc2 As RC2CryptoServiceProvider = New RC2CryptoServiceProvider
rc2.Key = key
Dim b() As Byte = {21, 22, 23, 24, 25, 26, 27, 28}
rc2.IV = b
Dim plaintext() As Byte = Encoding.Default.GetBytes(txtResult.Text)

Dim ms As New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, rc2.CreateDecryptor(),
_
CryptoStreamMode.Write)
cs.Write(plaintext, 0, plaintext.Length)
cs.FlushFinalBlock()
ms.Position = 0L
Dim result(plaintext.Length - 1) As Byte
ms.Read(result, 0, result.Length - 1)
txtDecrypt.Text = Encoding.Default.GetString(result)
cs.Close()
ms.Close()
End Sub

Hope this helps
Jay



Steve Long said:
Hello,
I'm wondering if somebody can help me figure out how to decrypt data. I
seem
to be able to encrypt data but have not been able to decrypt the very data
that I've encrypted. I know it's because I'm just not understanding the
whole process but I'm obviously just not getting it. Here's the encryption
function I'm using:

Private Sub Encryptpwd()
Dim cdk As PasswordDeriveBytes = New PasswordDeriveBytes("passwd",
Nothing)
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key() As Byte = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)

Console.WriteLine(key.Length * 8)
' Set up an RC2 object to encrypt with the derived key
Dim rc2 As RC2CryptoServiceProvider = New RC2CryptoServiceProvider
rc2.Key = key
Dim b() As Byte = {21, 22, 23, 24, 25, 26, 27, 28}
rc2.IV = b
Dim plaintext() As Byte = Encoding.UTF8.GetBytes("iiMap")
Dim ms As New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, rc2.CreateEncryptor(),
_
CryptoStreamMode.Write)
cs.Write(plaintext, 0, plaintext.Length)
cs.Close()
Dim result() As Byte = ms.ToArray()
Dim asc As New ASCIIEncoding
txtResult.Text = asc.GetString(result)
ms.Close()
End Sub

When I try to decrypt the text that's in txtResult.Text, my result is
always
and empty string:

Private Sub Decryptpwd()
Dim cdk As PasswordDeriveBytes = New
PasswordDeriveBytes("pa[]ss!!wo//dd", Nothing)
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key() As Byte = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)

Console.WriteLine(key.Length * 8)
' Set up an RC2 object to encrypt with the derived key
Dim rc2 As RC2CryptoServiceProvider = New RC2CryptoServiceProvider
rc2.Key = key
Dim b() As Byte = {21, 22, 23, 24, 25, 26, 27, 28}
rc2.IV = b
Dim plaintext() As Byte = Encoding.UTF8.GetBytes(txtResult.Text)

Dim ms As New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, rc2.CreateDecryptor(),
_
CryptoStreamMode.Write)
cs.Write(plaintext, 0, plaintext.Length)
Dim result(plaintext.Length - 1) As Byte
ms.Read(result, 0, result.Length - 1)
Dim asc As New ASCIIEncoding
txtDecrypt.Text = asc.GetString(result)
cs.Close()
ms.Close()
End Sub

I would very much appreciate someone setting me straight on this issue.
Thanks in advance
Steve
P.S. I posted this in the security group but didn't get an answer to it.
I'm
hoping I have better luck here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top