Encrypt/Decrypt string VB.Net
November 27, 2007
Imports System.Security.Cryptography
Encryption Coding Is:
/*
* [strText]: string is that you need to encrypt
*[strEncrKey]: is the key to decrypt the string that has encryption
*/
Private Shared Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Try
Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText)
Dim des As New DESCryptoServiceProvider
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor(bykey, IV), CryptoStreamMode.Write)
cs.Write(InputByteArray, 0, InputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function
Decryption Code Is:
/*
* [strText]: a string that has been encrypt with the above method
*[sDecrKey]: string is the key needed to decrypt
*/
Private Shared Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Dim inputByteArray(strText.Length) As Byte
Try
Dim byKey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
Dim des As New DESCryptoServiceProvider
inputByteArray = Convert.FromBase64String(strText)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function
Call Function:
Return Encrypt(“string is that you need to encrypt“, “abc123″)
Return Decrypt(“string that has been encrypt with the above method
“, “abc123″)
15 Comments Add your own
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed
1.
Ryan | March 24, 2008 at 5:57 am
this is VB.net windows form.. how about in web form?? PLS help me
2.
Kip | April 6, 2008 at 1:53 am
Nice code works fine…
3.
Rahul | August 4, 2008 at 11:30 am
hi,
i’m mew to vb.net can some please tell what does this error is all about and how i can correct it!!
Error - Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. C:\Documents and Settings\Rahul\Desktop\eg\eg\Form1.vb 30 70 eg
4.
Tommy | August 7, 2008 at 1:55 pm
Thanks for sharing these. I have a problem though, I’m using your encrypt class, slightly modified to provide more options with the key but otherwise its the same.
It seems to encrypt and decrypt fine, however, if I encrypt to string, send the string across the network via TCP, and then attempt to decrypt it fails with “Invalid Character…” on the:
‘inputByteArray = Convert.FromBase64String(strExOTxt)’
Any idea why that is? I’ve had a search around to see if anyone has encountered this problem but not found anything. Wondering if I should get to work sending a bytearray across the network instead of the string?
The contents of strExOTxt by the way is: “uMJ+ovaul7o=” Where’s an invalid character in that!? ‘=’ is a buffer, and ‘+’ is allowed, as are the upper/lower case chars!!?
Any help muchly appreciated.
5.
ntcnet | August 7, 2008 at 3:11 pm
If you want to work sending a byte array across the network, you should encrypt another way. i will post if you need.
When you received Encrypted string, i will decrypt this one to bytes.
Work Flow:
Bytes => String Encrypted …Crossing network => String => Bytes
You can use CheckSum algorithm to keep intact your data that sending cross network.
If you have any questions, let me know or comment.
6.
Artyom | November 4, 2008 at 10:45 am
Thank you so much for this code. Who would’ve known that turning memory stream to array makes it displayable in a text box on the form. Took me two days to figure out
7.
Rai Shahid | February 12, 2009 at 12:27 pm
Thanks dear it solve my problem…
8.
Matty | February 25, 2009 at 6:27 pm
Does anyone know why the encrypt method is returning:
“Specified key is not a valid size for this algorithm.”
??
Thanks
9.
jalpaeol | March 2, 2009 at 2:03 am
can you specify both parametersByVal strText As String, ByVal strEncrKey As String)
and (ByVal strText As String, ByVal sDecrKey As String)
have what values can be ?
with example i have error like “bad code”
thanks
10.
ntcnet | March 2, 2009 at 2:25 am
I just commented source code.
Sorry for this inconvenience!
11.
Elena | April 1, 2009 at 2:17 pm
ntcnet, I am having the same problem as Tommy above. I keep getting “Invalid character in Base 65 string” when calling my encryption component from VBScript in attempt to decrypt connection string.
Calling encryption method works fine, but decryption gives me this error.
Could you please help?
12.
ntcnet | April 1, 2009 at 2:23 pm
You should review encryption method and make sure using key string correct. If you have any comments, feel free contact me. thanks
13.
Elena | April 1, 2009 at 2:19 pm
oops, it should have been Base 64 string, not 65
14.
Pranav | April 14, 2009 at 12:47 pm
thanks for providing this for us!
15.
Asaduzzaman Arif | May 4, 2009 at 8:26 am
Thank you
Its great