473,320 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Decrypting data

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.
Nov 21 '05 #1
2 1706
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" <St**********@NoSpam.com> wrote in message
news:eo**************@TK2MSFTNGP14.phx.gbl...
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.

Nov 21 '05 #2
Thanks Jay, I'll give it a whirl

Appreciate the help.
Steve

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OZ**************@TK2MSFTNGP10.phx.gbl...
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" <St**********@NoSpam.com> wrote in message
news:eo**************@TK2MSFTNGP14.phx.gbl...
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.


Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: M Wells | last post by:
Hi All, I'm trying to implement encryption on certain data fields in my MySQL database and I'm experiencing ongoing problems. I seem to be able to encrypt the data without issues, but can't...
3
by: JW | last post by:
I can encrypt the contents of a memory stream with no problems, however when decrypting( I'm using the same key an IV ) I get an exception stating bad data. I'm at a lost. I'm actually creating...
0
by: cmrchs | last post by:
Hi, Using the RSA-crypto algorithm in .NET , when actually encrypting/decrypting, all the code samples I ran into always used something like, to encrypt : // create keypair and store in...
1
by: Yair Nissan | last post by:
Hi, I'm trying to find a way to encrypt data (strings) from my client application (c++ 6.0) and transfer it to my server's .Net web services application. The web services must be able to decrypt...
5
by: Joe | last post by:
I'm trying to figure out how to remove the characters padded to the end of my string without setting the Padding = PaddingMode.None. My original string passed in is 'passwordTest' and the...
0
by: cmrchs | last post by:
Hi, Using the RSA-crypto algorithm in .NET , when actually encrypting/decrypting, all the code samples I ran into always used something like, to encrypt : ' create keypair and store in...
3
by: dfa_geko | last post by:
Hi All, I had a question about encrypting and decrypting XML files using asymmetric keys. I copied some sample code from MSDN, here are the samples: ...
5
by: Harb247 | last post by:
Hi Guys, First Post. Need some help decrypting a String / DB The company i manage recently had an argument with their lead programmer for their data base. However he has decided to have nothing...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
2
by: prasath03 | last post by:
Hi, I wrote a java program for Encrypt and Decrypt the given string, when i execute the program it show me an error....but the string has Encrypted, if i want to Decrypt the string it show me an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.