Encrypting/decrypting strings

Posts 16 of 6 · Page 1 of 1
Encrypting/decrypting strings
OK, I'm not going into detail about it, but I'll give you the code, and how to use a simple encryption/decryption method.

then upgrade it to a MD5 hash method.

first of all, put these above your namespace.
(if you don't know where that is, sorry, stop reading, and do something else)

[highlight=vb.net]
Imports System.Text
Imports System.IO
Imports System.Security.Cryptography
Imports System.Collections.Specialized
[/highlight]


first method, reverse encrypt/decrypt.

[highlight=vb.net]
Public Function SimpleCrypt(ByVal Text As String) As String
' Encrypts/decrypts the passed string using
' a simple ASCII value-swapping algorithm
Dim strTempChar As String, i As Integer
strTempChar = ""
For i = 1 To Len(Text)
If Asc(Mid$(Text, i, 1)) < 128 Then
strTempChar = CType(Asc(Mid$(Text, i, 1)) + 128, String)
ElseIf Asc(Mid$(Text, i, 1)) > 128 Then
strTempChar = CType(Asc(Mid$(Text, i, 1)) - 128, String)
End If
Mid$(Text, i, 1) = Chr(CType(strTempChar, Integer))
Next i
Return Text
End Function
[/highlight]



simply, use it like:
[highlight=vb.net]
dim str as string = simplecrypt("string here")
[/highlight]

and to decrypt that message, use:
[highlight=vb.net]
msgbox(simplecrypt(str))
[/highlight]



now, if you want to use a heavy encryption, there are a few you can use,
but i tend to stick with a MD5 encryption.
people use what they want to, and this is just my personal one...well, modified of coarse for security reasons for my apps...

[highlight=vb.net]
Public Class DEScrypt
Private Shared DES As New TripleDESCryptoServiceProvider
Private Shared MD5 As New MD5CryptoServiceProvider

Public Shared Function MD5hash(ByVal value As String) As Byte()
Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value ))
End Function
Public Shared Function encrypt(ByVal strEncrypt As String, ByVal key As String) As String
DES.Key = DEScrypt.MD5hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(strEncrypt)
Return Convert.ToBase64String(DES.CreateEncryptor().Trans formFinalBlock(Buffer, 0, Buffer.Length))
End Function
Public Shared Function decrypt(ByVal strdecrypt As String, ByVal key As String) As String
Try
DES.Key = DEScrypt.MD5hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = Convert.FromBase64String(strdecrypt)
Return ASCIIEncoding.ASCII.GetString(DES.CreateDecryptor( ).TransformFinalBlock(Buffer, 0, Buffer.Length))
Catch ex As Exception
MsgBox("error, wrong key")
Return ""
End Try
End Function
End Class
[/highlight]


now, to use this one, its much the same, except there is a decrypt function and an encrypt function.

where it says key, that is the encryption/decryption key. it must be the same as the one you used to encrypt if your going to decrypt the encrypted string. or, it will error.

encrypt :
[highlight=vb.net]
dim encoded as string = encrypt("string here", "key")

[/highlight]

and decrypt:
[highlight=vb.net]
dim unencoded as string = decrypt(encoded, "key")
[/highlight]


that's about it, so...until I'm bored again, see ya's ^.^
LOL

Quote Originally Posted by *****179
first of all, put these in your namespace area.
(if you don't know where that is, sorry, stop reading, and do something else)
I stopped reading because even that line was wrong. You do NOT put import statements within a namespace.

Why are you using VB6 functions for this..?
ok...better?...
What xD namespace area? lol, i learned VB and i never, never heard about namespace are, namespacearea is in C++ but not in VB ,.. its the Import Area but the rest is okay
Quote Originally Posted by Code[VB] View Post
What xD namespace area? lol, i learned VB and i never, never heard about namespace are, namespacearea is in C++ but not in VB ,.. its the Import Area but the rest is okay
VB does contains namespaces.
thank u man ...i apprecitate this ...and also the other pplz wich corrected ..THANK YOU
Posts 16 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?