Page 1 of 1

RC4 TextFile Encrypter!

Posted: Fri Nov 11, 2011 3:18 pm
by pip
What you need:
1 open file dialog
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 save file dialog
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 text box's:
Textbox 1 needs to be read only!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 buttons:
Button 1 is called select file and goes
by textbox 1
Button 2 is called encrypt file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 label
RC4 Password and goes by textbox 2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Imports:
Code: Select all
Imports System.IO
Imports System.Text
The whole code from start to end after import copy and paste!
Code: Select all
Public Class Form1
    Dim filereadin, fileout As String

    Public Shared Function rc4(ByVal message As String, ByVal password As String) As String
        Dim i As Integer = 0
        Dim j As Integer = 0
        Dim cipher As New StringBuilder
        Dim returnCipher As String = String.Empty
        Dim sbox As Integer() = New Integer(256) {}
        Dim key As Integer() = New Integer(256) {}
        Dim intLength As Integer = password.Length
        Dim a As Integer = 0
        While a <= 255
            Dim ctmp As Char = (password.Substring((a Mod intLength), 1).ToCharArray()(0))
            key(a) = Microsoft.VisualBasic.Strings.Asc(ctmp)
            sbox(a) = a
            System.Math.Max(System.Threading.Interlocked.Increment(a), a - 1)
        End While
        Dim x As Integer = 0
        Dim b As Integer = 0
        While b <= 255
            x = (x + sbox(b) + key(b)) Mod 256
            Dim tempSwap As Integer = sbox(b)
            sbox(b) = sbox(x)
            sbox(x) = tempSwap
            System.Math.Max(System.Threading.Interlocked.Increment(b), b - 1)
        End While
        a = 1
        While a <= message.Length
            Dim itmp As Integer = 0
            i = (i + 1) Mod 256
            j = (j + sbox(i)) Mod 256
            itmp = sbox(i)
            sbox(i) = sbox(j)
            sbox(j) = itmp
            Dim k As Integer = sbox((sbox(i) + sbox(j)) Mod 256)
            Dim ctmp As Char = message.Substring(a - 1, 1).ToCharArray()(0)
            itmp = Asc(ctmp)
            Dim cipherby As Integer = itmp Xor k
            cipher.Append(Chr(cipherby))
            System.Math.Max(System.Threading.Interlocked.Increment(a), a - 1)
        End While
        returnCipher = cipher.ToString
        cipher.Length = 0
        Return returnCipher
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
        TextBox1.Text = OpenFileDialog1.FileName
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Then
            MessageBox.Show("Pick A File!", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        If TextBox2.Text = "" Then
            MessageBox.Show("Pick A Password", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        FileOpen(1, TextBox1.Text, OpenMode.Binary, OpenAccess.Read, OpenShare.Default)
        filereadin = Space(LOF(1))
        FileGet(1, filereadin)
        FileClose(1)
        SaveFileDialog1.ShowDialog()
        Try
            fileout = SaveFileDialog1.FileName
            FileOpen(1, fileout, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Default)
            FilePut(1, rc4(filereadin, TextBox2.Text))
            FileClose(1)
            TextBox1.Text = ""
            MessageBox.Show(fileout & " Was created!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Catch ex As Exception
        End Try
    End Sub
End Class
Credits:
An old friend of mine for helping me: 90%
Me: 10%

cooll;

Re: RC4 TextFile Encrypter!

Posted: Fri Nov 11, 2011 4:14 pm
by Dummy1912
so is the password gonna be saved in the same file as the orginal file or will there be a separate file with the password.

Re: RC4 TextFile Encrypter!

Posted: Fri Nov 11, 2011 4:16 pm
by pip
Seperate thats why the save file dialog is there :D

Re: RC4 TextFile Encrypter!

Posted: Fri Nov 11, 2011 4:25 pm
by Dummy1912
pip wrote:
Seperate thats why the save file dialog is there :D

so what will happen if someone delete that file.
can you still access your rc4 file without it

or do i understand it a little wrong?

Re: RC4 TextFile Encrypter!

Posted: Fri Nov 11, 2011 5:00 pm
by pip
its whatever yo utype password or text it encrypts in file it really is back up but encrypted

Re: RC4 TextFile Encrypter!

Posted: Fri Nov 11, 2011 5:05 pm
by Dummy1912
o cool :)

thanks pip :D

Re: RC4 TextFile Encrypter!

Posted: Tue Nov 15, 2011 9:56 am
by clanc789
+rep for this <3! I searched for this a long long time!!!

Re: RC4 TextFile Encrypter!

Posted: Wed Jan 01, 2014 10:15 pm
by hungryhounduk
Hey Pip
What i would really like is to be able to DECrypt the Encrypted file back to it's original state.... It would make a great little addition to an app i am working on, where you could send someone a encrypted file and as long as they have the Decrypter app and Password to the Encrypted file, they could then turn the file back to it's original state..

Just a Thought

Chris

Re: RC4 TextFile Encrypter!

Posted: Wed Jan 01, 2014 10:57 pm
by pip
#hungryhounduk A bit lazy to try, but here is a supposed idea this code that creates it:
Code: Select all
 Try
            fileout = SaveFileDialog1.FileName
            FileOpen(1, fileout, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Default)
            FilePut(1, rc4(filereadin, TextBox2.Text))
            FileClose(1)
            TextBox1.Text = ""
            MessageBox.Show(fileout & " Was created!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Catch ex As Exception
        End Try
You need to take this : FilePut(1, rc4(filereadin, TextBox2.Text)) and whatever hash or encrypted thing try to make a function that reverses the encryption and just make it do this:
Code: Select all
FilePut(1, rc4(filereadin, EncryptedTextBox2.Text))
so prob some stuff to select were file is and a textbox for entering the pass like one of shims apps, and maybe another string.