J
Jose Perez
Dear All,
I have the following problem. I am passing an encrypted value on the
querystring from one aspx to another. Originally I was outputing
characters such as "+, /,..." (which caused problems), so I converted
this byte array to Hex. When I come to read the string back into a
byte array to decrypt it I always get an extra value, so myByte(40)
becomes a length of 41 and I get an error relating to the length of
the byte array. If I try and force it to be myByte(39) (so the length
is 40) I get an error saying bad data!! Code below...
I don't know whether this is the correct solution but I want to
"delete" the last byte of my (hex to byte) array.
Any help is greatly appreciated.
Thanks in advance,
Jose
<code>
(Const sPassword As String)
Public Shared Function Encrypt(ByVal sToEncrypt As String) As String
Dim sEncrypted As String
Dim des As TripleDESCryptoServiceProvider
Dim hashMD5 As MD5CryptoServiceProvider
Dim pwdHash As Byte()
Dim buff As Byte()
hashMD5 = New MD5CryptoServiceProvider
pwdHash =
hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sPassword))
'Set variable to nothing.
hashMD5 = Nothing
des = New TripleDESCryptoServiceProvider
des.Key = pwdHash
des.Mode = CipherMode.ECB
buff = ASCIIEncoding.ASCII.GetBytes(sToEncrypt)
buff = des.CreateEncryptor().TransformFinalBlock(buff, 0,
buff.Length)
'Encode each byte in the binary into two hex digits.
Dim sHex As New StringBuilder(2 * buff.Length)
Dim i As Integer
For i = 0 To buff.Length - 1 Step 1
sHex.AppendFormat("{0:X2} ", buff(i))
Next
'Remove all whitespaces and return.
Return sHex.ToString().Replace(" ", String.Empty)
End Function
Public Shared Function Decrypt(ByVal sToDecrypt As String) As
String
Dim des As TripleDESCryptoServiceProvider
Dim hashMD5 As MD5CryptoServiceProvider
Dim pwdHash As Byte()
Dim buff As Byte()
Dim sDecrypted As String
hashMD5 = New MD5CryptoServiceProvider
pwdHash =
hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sPassword))
'Set variable to nothing.
hashMD5 = Nothing
des = New TripleDESCryptoServiceProvider
des.Key = pwdHash
des.Mode = CipherMode.ECB
'Change "each" Hex into byte format.
Dim i As Integer
Dim iByte As Integer = sToDecrypt.Length / 2
Dim ByteVar(iByte) As Byte
For i = 0 To iByte - 1
ByteVar(i) = Convert.ToByte(sToDecrypt.Substring(i *
2, 2), 16)
Next
buff = ByteVar '####HERE'S THE PROBLEM####
'Decrypt DES 3 encrypted byte buffer and return ASCII
string.
sDecrypted =
ASCIIEncoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buff,
0, buff.Length))
'Cleanup
des = Nothing
Return sDecrypted
End Function
</code>
I have the following problem. I am passing an encrypted value on the
querystring from one aspx to another. Originally I was outputing
characters such as "+, /,..." (which caused problems), so I converted
this byte array to Hex. When I come to read the string back into a
byte array to decrypt it I always get an extra value, so myByte(40)
becomes a length of 41 and I get an error relating to the length of
the byte array. If I try and force it to be myByte(39) (so the length
is 40) I get an error saying bad data!! Code below...
I don't know whether this is the correct solution but I want to
"delete" the last byte of my (hex to byte) array.
Any help is greatly appreciated.
Thanks in advance,
Jose
<code>
(Const sPassword As String)
Public Shared Function Encrypt(ByVal sToEncrypt As String) As String
Dim sEncrypted As String
Dim des As TripleDESCryptoServiceProvider
Dim hashMD5 As MD5CryptoServiceProvider
Dim pwdHash As Byte()
Dim buff As Byte()
hashMD5 = New MD5CryptoServiceProvider
pwdHash =
hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sPassword))
'Set variable to nothing.
hashMD5 = Nothing
des = New TripleDESCryptoServiceProvider
des.Key = pwdHash
des.Mode = CipherMode.ECB
buff = ASCIIEncoding.ASCII.GetBytes(sToEncrypt)
buff = des.CreateEncryptor().TransformFinalBlock(buff, 0,
buff.Length)
'Encode each byte in the binary into two hex digits.
Dim sHex As New StringBuilder(2 * buff.Length)
Dim i As Integer
For i = 0 To buff.Length - 1 Step 1
sHex.AppendFormat("{0:X2} ", buff(i))
Next
'Remove all whitespaces and return.
Return sHex.ToString().Replace(" ", String.Empty)
End Function
Public Shared Function Decrypt(ByVal sToDecrypt As String) As
String
Dim des As TripleDESCryptoServiceProvider
Dim hashMD5 As MD5CryptoServiceProvider
Dim pwdHash As Byte()
Dim buff As Byte()
Dim sDecrypted As String
hashMD5 = New MD5CryptoServiceProvider
pwdHash =
hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sPassword))
'Set variable to nothing.
hashMD5 = Nothing
des = New TripleDESCryptoServiceProvider
des.Key = pwdHash
des.Mode = CipherMode.ECB
'Change "each" Hex into byte format.
Dim i As Integer
Dim iByte As Integer = sToDecrypt.Length / 2
Dim ByteVar(iByte) As Byte
For i = 0 To iByte - 1
ByteVar(i) = Convert.ToByte(sToDecrypt.Substring(i *
2, 2), 16)
Next
buff = ByteVar '####HERE'S THE PROBLEM####
'Decrypt DES 3 encrypted byte buffer and return ASCII
string.
sDecrypted =
ASCIIEncoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buff,
0, buff.Length))
'Cleanup
des = Nothing
Return sDecrypted
End Function
</code>