Jump to content

Vb Encryption Help


EMB
 Share

Recommended Posts

How would I go about encrypting a string with a password in Visual Basic (.NET)? The majority of stuff I have seen online encrypt files and do all sorts of fancy shit, which I do not need. Don't ask me to use C or something, because I need to use VB.

Any help is appreciated, wether it be a link or working code.

Link to comment
Share on other sites

...

I know the encryption standards, I just don't know how to implement them into some code or a function.

It's a good way to learn :)

That wikipedia article describe how AES works step by step. Though AES is only a symmetric key encryption algorithm. You probably want to combined it with a basic password based encryption system where you use the MD5 or SHA1 sum generated from a random number (salt) and a password to either produce the key for the AES algorithm or to encrypt the AES key with another algorithm.

Link to comment
Share on other sites

I would advice you to start witht RC4. It's still used, like in WEP, and best of all Wiki comes with pseudocode ;-)

http://en.wikipedia.org/wiki/RC4

It's nearly VB-Code. If that doesn't help you post again I think somewhere in an old backup I still have my implementation of RC4, DES, TripleDES and IDEA from back when I was in school and used to code in Visual Basic 6.0.

But I think with the pseudo code from Wiki you will succeed.

Link to comment
Share on other sites

Have a look at planet source code. Back in the day when I used to work with VB6 a lot, this is the site that I learned most of it from: http://www.planet-source-code.com/vb/default.asp?lngWId=10

They have tons of working examples and easy to follow comments on most of the submitted projects. Freely downloadable code to work from and see how to go about implementing your own project. Not just for Visual Basic, but they have a lot of other languages as well, such as PHP, ASP, Java, JavaScript, etc..

Link to comment
Share on other sites

It's nearly VB-Code.

C is nearly VB? If you say so.

Anyway, I may do something like this, but adding a secret number. Not the most secure in the world but hey, I don't intend it to be found in the first place.

Link to comment
Share on other sites

This is a Module I found online and have been playing around with (I will cite the author later If I can dig out the bookmark I originally got it from)

Dim TDES As New TripleDES()
s_Password = TDES.Decrypt(s_EncryptedString)

Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Public Class TripleDES
    Private key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
    Private iv() As Byte = {65, 110, 68, 26, 69, 178, 200, 219}

    Public Function Encrypt(ByVal plainText As String) As Byte()
        ' Declare a UTF8Encoding object so we may use the GetByte
        ' method to transform the plainText into a Byte array.
        Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
        Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)

        ' Create a new TripleDES service provider
        Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()

        ' The ICryptTransform interface uses the TripleDES
        ' crypt provider along with encryption key and init vector
        ' information
        Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)

        ' All cryptographic functions need a stream to output the
        ' encrypted information. Here we declare a memory stream
        ' for this purpose.
        Dim encryptedStream As MemoryStream = New MemoryStream()
        Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)

        ' Write the encrypted information to the stream. Flush the information
        ' when done to ensure everything is out of the buffer.
        cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
        cryptStream.FlushFinalBlock()
        encryptedStream.Position = 0

        ' Read the stream back into a Byte array and return it to the calling
        ' method.
        Dim result(encryptedStream.Length - 1) As Byte
        encryptedStream.Read(result, 0, encryptedStream.Length)
        cryptStream.Close()

        Return result
    End Function

    Public Function Decrypt(ByVal inputInBytes() As Byte) As String
        ' UTFEncoding is used to transform the decrypted Byte Array
        ' information back into a string.
        Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
        Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()

        ' As before we must provide the encryption/decryption key along with
        ' the init vector.
        Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)

        ' Provide a memory stream to decrypt information into
        Dim decryptedStream As MemoryStream = New MemoryStream()
        Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
        cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
        cryptStream.FlushFinalBlock()
        decryptedStream.Position = 0

        ' Read the memory stream and convert it back into a string
        Dim result(decryptedStream.Length - 1) As Byte
        decryptedStream.Read(result, 0, decryptedStream.Length)
        cryptStream.Close()
        Dim myutf As UTF8Encoding = New UTF8Encoding()

        Return myutf.GetString(result)
    End Function

    Public Function StrToByteArray(ByVal str As String) As Byte()
        Dim encoding As New System.Text.UTF8Encoding()
        Return encoding.GetBytes(str)
    End Function 'StrToByteArra

    Public Function ByteArrayToString(ByVal ba_ByteArray As Byte()) As String
        Dim dBytes As Byte() = ba_ByteArray
        Dim s_String As String
        Dim o_Encoding As New System.Text.UTF8Encoding()
        s_String = o_Encoding.GetString(dBytes)

        Return s_String
    End Function

End Class

Link to comment
Share on other sites

Private Sub Command1_click()

Dim S As String

S = RndCrypt(Text1.Text, "pass")

Text2.Text = S

End Sub

Public Function RndCrypt(ByVal Str As String, ByVal Password As String) As String

Dim SK As Long, K As Long

Rnd -1

Randomize Len(Password)

For K = 1 To Len(Password)

SK = SK + (((K Mod 256) Xor Asc(Mid$(Password, K, 1))) Xor Fix(256 * Rnd))

Next K

Rnd -1

Randomize SK

For K = 1 To Len(Str)

Mid$(Str, K, 1) = Chr(Fix(256 * Rnd) Xor Asc(Mid$(Str, K, 1)))

Next K

RndCrypt = Str

End Function

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...