Skip to content
MPGHThe Dark Arts
/
RegisterLog in
Forum
Community
What's NewLatest posts across the boardTrendingHottest threads right nowSubscribedThreads you follow
Discussion
GeneralIntroductionsEntertainmentDebate FortFlaming & Rage
Board
News & AnnouncementsMPGH TimesSuggestions & HelpGiveaways
More Sections
Art & Graphic DesignProgrammingHackingCryptocurrency
Hacks & Cheats
Games
ValorantCS2 / CS:GOCall of Duty / WarzoneFortniteApex LegendsEscape From Tarkov
+14 moreLeague of LegendsGTA VMinecraftRustROTMGBattlefieldTroveBattleOnCombat ArmsCrossFireBlackshotRuneScapeDayZDead by Daylight
Resources
Game Hacking TutorialsReverse EngineeringGeneral Game HackingAnti-CheatConsole Game Hacking
Tools
Game Hacking ToolsTrainers & CheatsHack/Release NewsNew
Submit a release →Share your cheat, tool, or config with the community.
AINEW
AI Tools
General & DiscussionPrompt EngineeringLLM JailbreaksHotAI Agents & AutomationLocal / Open Models
AI × Gaming
AI Aimbots & VisionML Anti-CheatGame Bots & Automation
Create
AI Coding / Vibe CodingAI Art & MediaAI Voice & TTS
The AI frontier →Where game hacking meets modern machine learning. Jump in.
Marketplace
Buy & Sell
SellingBuyingTradingUser Services
Trust & Safety
Middleman LoungeMarketplace TalkVouch Copy Profiles
Money
Cryptocurrency TalkCurrency ExchangeWork & Job Offers
Start selling →List accounts, services, and goods. Use the middleman to trade safe.
MPGH The Dark Arts

A community for offensive security research, reverse engineering, and AI.

Community

ForumMarketplaceSearch

Account

RegisterLog in

Legal

Privacy PolicyForum RulesHelp & FAQ
© 2026 MPGH · All rights reserved.Built by the community, for the community. For educational purposes onlyContent is shared for security research and education — we don't condone illegal use. You're responsible for complying with applicable laws. Use at your own risk.
Home › Forum › Programming › Visual Basic Programming › Encrypt/Decrypt vb.Net?

Encrypt/Decrypt vb.Net?

Posts 1–15 of 25 · Page 1 of 2
o0OpurezO0o
o0OpurezO0o
Encrypt/Decrypt vb.Net?
Is there a source for a Encrypter/Decrypter (needs password to access)? Cause i need it for my laptop.
#1 · 15y ago
WT
wtfiwantthatname
heres code for encryption. Theres a function for strings and bytes for each encryption type.

Code:
Imports System.Security.Cryptography
Module Encryption
    Public Function AESEncrypt(ByVal Key As String, ByVal Data As String) As String
        Try
            Dim AES As AesCryptoServiceProvider
            With AES
                .Mode = CipherMode.ECB
                .Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
                .IV = .Key
            End With
            Dim AESEncryptor As ICryptoTransform = AES.CreateEncryptor
            Dim DataBuffer As Byte()
            Dim EncryptedData As Byte()
            DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
            EncryptedData = AESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return ByteToString(EncryptedData)
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function


    Public Function AESEncrypt(ByVal Key() As Byte, ByVal Data() As Byte) As Byte()
        Try
            Dim AES As AesCryptoServiceProvider
            With AES
                .Mode = CipherMode.ECB
                .Key = Key
                .IV = .Key
            End With
            Dim AESEncryptor As ICryptoTransform = AES.CreateEncryptor
            Dim DataBuffer As Byte()
            Dim EncryptedData As Byte()
            DataBuffer = Data
            EncryptedData = AESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return EncryptedData
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function


    Public Function AESDecrypt(ByVal Key() As Byte, ByVal Data() As Byte) As Byte()
        Try
            Dim AES As AesCryptoServiceProvider
            With AES
                .Mode = CipherMode.ECB
                .Key = Key
                .IV = .Key
            End With
            Dim AESDecryptor As ICryptoTransform = AES.CreateDecryptor
            Dim DataBuffer As Byte()
            Dim DecryptedData As Byte()
            DataBuffer = Data
            DecryptedData = AESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return DecryptedData
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function


    Public Function AESDecrypt(ByVal Key As String, ByVal Data As String) As String
        Try
            Dim AES As AesCryptoServiceProvider
            With AES
                .Mode = CipherMode.ECB
                .Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
                .IV = .Key
            End With
            Dim AESDecryptor As ICryptoTransform = AES.CreateDecryptor
            Dim DataBuffer As Byte()
            Dim DecryptedData As Byte()
            DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
            DecryptedData = AESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return ByteToString(DecryptedData)
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function


    Public Function RC2Encrypt(ByVal Key As String, ByVal Data As String) As Byte()
        Try
            Dim RC2 As RC2CryptoServiceProvider
            With RC2
                .Mode = CipherMode.ECB
                .UseSalt = True
                .Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
                .IV = .Key
            End With
            Dim RC2Encryptor As ICryptoTransform = RC2.CreateEncryptor
            Dim databuffer As Byte()
            Dim EncryptedData As Byte()
            databuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
            EncryptedData = RC2Encryptor.TransformFinalBlock(databuffer, 0, databuffer.Length)
            Return EncryptedData
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function

    Public Function RC2Encrypt(ByVal Key() As Byte, ByVal Data() As Byte) As Byte()
        Try
            Dim RC2 As RC2CryptoServiceProvider
            With RC2
                .Mode = CipherMode.ECB
                .UseSalt = True
                .Key = Key
                .IV = .Key
            End With
            Dim RC2Encryptor As ICryptoTransform = RC2.CreateEncryptor
            Dim databuffer As Byte()
            Dim EncryptedData As Byte()
            databuffer = Data
            EncryptedData = RC2Encryptor.TransformFinalBlock(databuffer, 0, databuffer.Length)
            Return EncryptedData
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function

    Public Function RC2Decrypt(ByVal key() As Byte, ByVal data() As Byte) As Byte()
        Try
            Dim RC2 As RC2CryptoServiceProvider
            With RC2
                .Mode = CipherMode.ECB
                .UseSalt = True
                .Key = key
                .IV = .Key
            End With
            Dim RC2Decryptor As ICryptoTransform = RC2.CreateDecryptor
            Dim databuffer As Byte()
            Dim Decrypteddata As Byte()
            databuffer = data
            Decrypteddata = RC2Decryptor.TransformFinalBlock(databuffer, 0, databuffer.Length)
            Return Decrypteddata
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function

    Public Function RC2Decrypt(ByVal Key As String, ByVal Data As String) As String
        Try
            Dim RC2 As RC2CryptoServiceProvider
            With RC2
                .Mode = CipherMode.ECB
                .UseSalt = True
                .Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
                .IV = .Key
            End With
            Dim RC2Decryptor As ICryptoTransform = RC2.CreateDecryptor
            Dim DataBuffer As Byte()
            Dim DecryptedData As Byte()
            DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
            DecryptedData = RC2Decryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return ByteToString(DecryptedData)
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function

    Public Function DESEncrypt(ByVal Key As String, ByVal Data As String) As String
        Try
            Dim DES As DESCryptoServiceProvider
            With DES
                .Mode = CipherMode.ECB
                .Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
                .IV = .Key
            End With
            Dim DESEncryptor As ICryptoTransform = DES.CreateEncryptor
            Dim DataBuffer As Byte()
            Dim EncryptedData As Byte()
            DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
            EncryptedData = DESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return ByteToString(EncryptedData)
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function

    Public Function DESEncrypt(ByVal key() As Byte, ByVal data() As Byte) As Byte()
        Try
            Dim DES As DESCryptoServiceProvider
            With DES
                .Mode = CipherMode.ECB
                .Key = key
                .IV = .Key
            End With
            Dim DESEncryptor As ICryptoTransform = DES.CreateEncryptor
            Dim DataBuffer As Byte()
            Dim Encrypteddata As Byte()
            DataBuffer = data
            Encrypteddata = DESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return Encrypteddata
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function



    Public Function DESDecrypt(ByVal Key As String, ByVal Data As String) As String
        Try
            Dim DES As DESCryptoServiceProvider
            With DES
                .Mode = CipherMode.ECB
                .Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
                .IV = .Key
            End With
            Dim DESDecryptor As ICryptoTransform = DES.CreateDecryptor
            Dim DataBuffer As Byte()
            Dim DecryptedData As Byte()
            DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
            DecryptedData = DESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return ByteToString(DecryptedData)
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function

    Public Function DESDecrypt(ByVal Key() As Byte, ByVal data() As Byte) As Byte()
        Try
            Dim DES As DESCryptoServiceProvider
            With DES
                .Mode = CipherMode.ECB
                .Key = Key
                .IV = .Key
            End With
            Dim DESDecryptor As ICryptoTransform = DES.CreateDecryptor
            Dim DataBuffer As Byte()
            Dim DecryptedData As Byte()
            DataBuffer = data
            DecryptedData = DESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return DecryptedData
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function

    Public Function TripleDESEncrypt(ByVal Key As String, ByVal Data As String) As String
        Try
            Dim TripleDes As TripleDESCryptoServiceProvider
            With TripleDes
                .Mode = CipherMode.ECB
                .Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
                .IV = .Key
            End With
            Dim TripleDESEncryptor As ICryptoTransform = TripleDes.CreateEncryptor
            Dim DataBuffer As Byte()
            Dim EncryptedData As Byte()
            DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
            EncryptedData = TripleDESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return ByteToString(EncryptedData)
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function

    Public Function TripleDESEncrypt(ByVal key As Byte(), ByVal data As Byte()) As Byte()
        Try
            Dim TripleDes As TripleDESCryptoServiceProvider
            With TripleDes
                .Mode = CipherMode.ECB
                .Key = key
                .IV = .Key
            End With
            Dim TripleDESEncryptor As ICryptoTransform = TripleDes.CreateEncryptor
            Dim DataBuffer As Byte()
            Dim EncryptedData As Byte()
            DataBuffer = data
            EncryptedData = TripleDESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return EncryptedData
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function


    Public Function TriplesDESDecrypt(ByVal key As Byte(), ByVal Data As Byte()) As Byte()
        Try
            Dim TripleDes As TripleDESCryptoServiceProvider
            With TripleDes
                .Mode = CipherMode.ECB
                .Key = key
                .IV = .Key
            End With
            Dim TripleDESDecryptor As ICryptoTransform = TripleDes.CreateDecryptor
            Dim DataBuffer As Byte()
            Dim DecryptedData As Byte()
            DataBuffer = Data
            DecryptedData = TripleDESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return DecryptedData
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function
    Public Function TripleDESDecrypt(ByVal Key As String, ByVal Data As String) As String
        Try
            Dim TripleDes As TripleDESCryptoServiceProvider
            With TripleDes
                .Mode = CipherMode.ECB
                .Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
                .IV = .Key
            End With
            Dim TripleDESDecryptor As ICryptoTransform = TripleDes.CreateDecryptor
            Dim DataBuffer As Byte()
            Dim DecryptedData As Byte()
            DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
            DecryptedData = TripleDESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
            Return ByteToString(DecryptedData)
        Catch ex As Exception
            MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Function


    Public Function ByteToString(ByVal Bytes As Byte()) As String
        Return Convert.ToString(Bytes)
    End Function
End Module
#2 · 15y ago
o0OpurezO0o
o0OpurezO0o
What? I didnt understand what that is.

And what is the button codec to encrypt?

Edit: I get 32 Warnings:

Code:
Warning	1	Variable 'AES' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	6	18	WindowsApplication1
Warning	2	Function 'AESEncrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	20	5	WindowsApplication1
Warning	3	Variable 'AES' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	26	18	WindowsApplication1
Warning	4	Function 'AESEncrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	40	5	WindowsApplication1
Warning	5	Variable 'AES' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	46	18	WindowsApplication1
Warning	6	Function 'AESDecrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	60	5	WindowsApplication1
Warning	7	Variable 'AES' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	66	18	WindowsApplication1
Warning	8	Function 'AESDecrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	80	5	WindowsApplication1
Warning	9	Variable 'RC2' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	86	18	WindowsApplication1
Warning	10	Function 'RC2Encrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	101	5	WindowsApplication1
Warning	11	Variable 'RC2' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	106	18	WindowsApplication1
Warning	12	Function 'RC2Encrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	121	5	WindowsApplication1
Warning	13	Variable 'RC2' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	126	18	WindowsApplication1
Warning	14	Function 'RC2Decrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	141	5	WindowsApplication1
Warning	15	Variable 'RC2' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	146	18	WindowsApplication1
Warning	16	Function 'RC2Decrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	161	5	WindowsApplication1
Warning	17	Variable 'DES' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	166	18	WindowsApplication1
Warning	18	Function 'DESEncrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	180	5	WindowsApplication1
Warning	19	Variable 'DES' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	185	18	WindowsApplication1
Warning	20	Function 'DESEncrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	199	5	WindowsApplication1
Warning	21	Variable 'DES' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	206	18	WindowsApplication1
Warning	22	Function 'DESDecrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	220	5	WindowsApplication1
Warning	23	Variable 'DES' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	225	18	WindowsApplication1
Warning	24	Function 'DESDecrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	239	5	WindowsApplication1
Warning	25	Variable 'TripleDes' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	244	18	WindowsApplication1
Warning	26	Function 'TripleDESEncrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	258	5	WindowsApplication1
Warning	27	Variable 'TripleDes' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	263	18	WindowsApplication1
Warning	28	Function 'TripleDESEncrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	277	5	WindowsApplication1
Warning	29	Variable 'TripleDes' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	283	18	WindowsApplication1
Warning	30	Function 'TriplesDESDecrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	297	5	WindowsApplication1
Warning	31	Variable 'TripleDes' is used before it has been assigned a value. A null reference exception could result at runtime.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	302	18	WindowsApplication1
Warning	32	Function 'TripleDESDecrypt' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Encryption.vb	316	5	WindowsApplication1
#3 · edited 15y ago · 15y ago
NextGen1
NextGen1
Even though I am anti ignoring warnings. I wrote a whole article on it, those warnings can be ignored. The application will still build with warnings, and will still work.

What WTF neglected to mention is the code he is providing is a rendition of a old module with some minor declaration changes (Microsoft standard btw) in either case, it simply means that each of those functions can return null, but I wouldn't worry about it.

In general, This may help you out.

http://support.microsof*****m/kb/301070

Same source (again with original declarations), just with explanations.

Full source, From MSDN

Code:
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System****ntime.InteropServices
Imports System.Text


Module Module1

   ' Call this function to remove the key from memory after it is used for security.
   <DllImport("kernel32.dll")> _
   Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
   End Sub

   ' Function to generate a 64-bit key.
   Function GenerateKey() As String
      ' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
      Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()

      ' Use the automatically generated key for encryption. 
      Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)

   End Function

   Sub EncryptFile(ByVal sInputFilename As String, _
                  ByVal sOutputFilename As String, _
                  ByVal sKey As String)

      Dim fsInput As New FileStream(sInputFilename, _
                                  FileMode.Open, FileAccess.Read)
      Dim fsEncrypted As New FileStream(sOutputFilename, _
                                  FileMode.Create, FileAccess.Write)

      Dim DES As New DESCryptoServiceProvider()

      'Set secret key for DES algorithm.
      'A 64-bit key and an IV are required for this provider.
      DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)

      'Set the initialization vector.
      DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

      'Create the DES encryptor from this instance.
      Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
      'Create the crypto stream that transforms the file stream by using DES encryption.
      Dim cryptostream As New CryptoStream(fsEncrypted, _
                                          desencrypt, _
                                          CryptoStreamMode.Write)

      'Read the file text to the byte array.
      Dim bytearrayinput(fsInput.Length - 1) As Byte
      fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
      'Write out the DES encrypted file.
      cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
      cryptostream.Close()
   End Sub

   Sub DecryptFile(ByVal sInputFilename As String, _
       ByVal sOutputFilename As String, _
       ByVal sKey As String)

      Dim DES As New DESCryptoServiceProvider()
      'A 64-bit key and an IV are required for this provider.
      'Set the secret key for the DES algorithm.
      DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
      'Set the initialization vector.
      DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

      'Create the file stream to read the encrypted file back.
      Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
      'Create the DES decryptor from the DES instance.
      Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
      'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
      Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
      'Print out the contents of the decrypted file.
      Dim fsDecrypted As New StreamWriter(sOutputFilename)
      fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
      fsDecrypted.Flush()
      fsDecrypted.Close()
   End Sub

   Public Sub Main()
      'Must be 64 bits, 8 bytes.
      Dim sSecretKey As String

      ' Get the key for the file to encrypt.
      ' You can distribute this key to the user who will decrypt the file.
      sSecretKey = GenerateKey()

      ' For additional security, pin the key.
      Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)


      ' Encrypt the file.        
      EncryptFile("%USERPROFILE%\MyData.txt", _
                      "%USERPROFILE%\Encrypted.txt", _
                      sSecretKey)

      ' Decrypt the file.
      DecryptFile("%USERPROFILE%\Encrypted.txt", _
                  "%USERPROFILE%\Decrypted.txt", _
                  sSecretKey)

      ' Remove the key from memory. 
      ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
      gch.Free()
   End Sub

End Module
#4 · edited 15y ago · 15y ago
freedompeace
freedompeace
It's a warning, who cares?
#5 · 15y ago
o0OpurezO0o
o0OpurezO0o
OK, so the two Codes:

Code:
        ' Encrypt the file.        
        EncryptFile("%USERPROFILE%\MyData.txt", _
                        "%USERPROFILE%\Encrypted.txt", _
                        sSecretKey)

        ' Decrypt the file.
        DecryptFile("%USERPROFILE%\Encrypted.txt", _
                    "%USERPROFILE%\Decrypted.txt", _
                    sSecretKey)
i put in the two buttons i want to Encrypt/Decrypt?

BTW thanks NextGen.


anyone? answer please
#6 · edited 15y ago · 15y ago
NextGen1
NextGen1
Quote Originally Posted by o0OpurezO0o View Post
anyone? answer please
This

Code:
 EncryptFile("%USERPROFILE%\MyData.txt", _
                      "%USERPROFILE%\Encrypted.txt", _
                      sSecretKey)

      ' Decrypt the file.
      DecryptFile("%USERPROFILE%\Encrypted.txt", _
                  "%USERPROFILE%\Decrypted.txt", _
                  sSecretKey)
Is a sample, You will need to add one button

Encrypt

and another

Decrypt

Then use the functions

Code:
EncryptFile(ByVal sInputFilename As String, _
                  ByVal sOutputFilename As String, _
                  ByVal sKey As String)
In a example would be

Code:
EncryptFile("C:\test.txt","C:\TestEncrypted.txt, sSecretKey)
This will encrypt the file test.txt and create a new encrypted file (from the original) called TestEncrypted.txt.

Do the same to decrypt.

Make sense?
#7 · 15y ago
o0OpurezO0o
o0OpurezO0o
ERRORS!
Code:
Error	1	Expression expected.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb	7	21	WindowsApplication1
Error	2	Expression expected.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb	8	19	WindowsApplication1
Error	3	Expression expected.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb	9	19	WindowsApplication1
Error	4	Argument not specified for parameter 'sKey' of 'Public Sub EncryptFile(sInputFilename As String, sOutputFilename As String, sKey As String)'.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb	11	9	WindowsApplication1
Error	5	String constants must end with a double quote.	C:\Users\Owner\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb	11	35	WindowsApplication1
#8 · 15y ago
Jason
Jason
All pretty self explanatory errors. You forgot a double quote at the end of a string, forgot to pass the third parameter into the Encryption function and whatever the first ones were.
#9 · 15y ago
Lolland
Lolland
Please read over your errors before you post them /fp
#10 · 15y ago
Blubb1337
Blubb1337
Copy & Paste usually leads to several errors.
#11 · 15y ago
'Bruno
'Bruno
Quote Originally Posted by freedompeace View Post
It's a warning, who cares?
i really thought you were "awesome", but after this one... God...

start coding in C, ignore your warning, then cry that your program randomly crash's or that you have some random stupid outputs.
#12 · 15y ago
Jason
Jason
Quote Originally Posted by Brinuz View Post
i really thought you were "awesome", but after this one... God...

start coding in C, ignore your warning, then cry that your program randomly crash's or that you have some random stupid outputs.
Agreed. Some warnings can be ignored (if you KNOW it won't affect your program (i.e you've taken error handling into account, or it's just an unused variable). Otherwise shit may hit the fan in a large way.
#13 · 15y ago
o0OpurezO0o
o0OpurezO0o
I read them, i'm not a good error/warning reader.

@NextGen1 Im still awaiting on that book you told me bout. Just notfy me about the progress.
#14 · 15y ago
freedompeace
freedompeace
Quote Originally Posted by Brinuz View Post
i really thought you were "awesome", but after this one... God...

start coding in C, ignore your warning, then cry that your program randomly crash's or that you have some random stupid outputs.
There are many warnings that you can ignore, and many that you cannot. In Visual Basic, they've pretty much made it so that every warning that should not be ignored is now an error, so it's easier for people to grasp the language.

I do code in C, by the way. (Not much though, mainly C++)
#15 · 15y ago
Posts 1–15 of 25 · Page 1 of 2

Post a Reply

Tags for this Thread

None