Click here to Skip to main content
15,867,209 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Encrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Encrypt.Click
        Dim x As String = ""
        Dim xKalimat As String = ""
        Dim PjgPlain As Integer

        PjgPlain = Len(Plain.Text)
        For i As Integer = 1 To PjgPlain
            x = Mid(Plain.Text, i, i)
            x = Chr((Asc(x) + Key.Text) Mod 256)
            xKalimat = xKalimat + x
        Next
        Cipher.Text = xKalimat
    End Sub

    Private Sub Decrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Decrypt.Click
        Dim x As String = ""
        Dim xkalimat As String = ""
        For i As Integer = 1 To Len(Cipher.Text)
            x = Mid(Cipher.Text, i, i)
            x = Chr((Asc(x) - Key.Text) Mod 256)
            xkalimat = xkalimat + x
        Next
        Plain.Text = xkalimat
    End Sub
End Class
Posted
Updated 17-Feb-15 11:00am
v2
Comments
PIEBALDconsult 17-Feb-15 17:05pm    
First I'd check to be sure that 256 is the correct value. Then I'd define x as a character rather than a string and eliminate the calls to Mid (you don't need them, just index into the string). I'd also use a StringBuilder rather than string concatenation. Also, why do you use Len(Plain.Text) rather than Plain.Text.Length ?

1 solution

Hello I think you can use this.
First we are going to define the name of the file to encrypt/decrypt (in this example the same path of the app)
VB
Dim FILE_NAME As String = Application.StartupPath.ToString & "\textfile.txt"

Then we start with the encryption code
VB
Private Sub Encrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Encrypt.Click
    'Create a reader for the file
    Dim objReader As New System.IO.StreamReader(FILE_NAME)
    'Set the file text as a variable
    Dim Plain As String = objReader.ReadToEnd
    Dim xKalimat As String
    'Close the reader
    objReader.Close()
    'Loop though each character and save the changes into xKalimat
    For Each c As Char In Plain
        c = Chr((Asc(c) + Key.Text) Mod 256)
        xKalimat &= c
    Next
    'Create the writer
    Dim objWriter As New System.IO.StreamWriter(FILE_NAME, False)
    'Write the encrypted string overwriting the original file
    objWriter.Write(xKalimat)
    'Close the writer
    objWriter.Close()
End Sub

And finally we create the decryption method
VB
Private Sub Decrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Decrypt.Click
    'Create a reader for the file
    Dim objReader As New System.IO.StreamReader(FILE_NAME)
    'Set the file text as a variable
    Dim Cipher As String = objReader.ReadToEnd
    Dim xkalimat As String
    'Close the reader
    objReader.Close()
    'Loop though each character and save the changes into xKalimat
    For Each c As Char In Cipher
        c = Chr((Asc(c) - Key.Text) Mod 256)
        xkalimat &= c
    Next
    'Create the writer
    Dim objWriter As New System.IO.StreamWriter(FILE_NAME, False)
    'Write the decrypted string overwriting the original file
    objWriter.Write(xkalimat)
    'Close the writer
    objWriter.Close()
End Sub

Sorry for the delay on the answer but I read it today
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900