3DES symmetric encryption in .net

mH_p0rtal

Well-known member
Joined
Oct 17, 2006
Messages
61
Location
Persia
Programming Experience
3-5
hi
in this little paper we wanna use symmetric encryption. this type of encryption will be divided into 2 parts : stream cipher , block cipher
some of symmetric famous algorithms are : Twofish, Serpent, AES (Rijndael), Blowfish, CAST5, RC4, TDES,IDEA
so for doing this job , we need these stuffs :
one textbox - 3 buttons - & an open file dialog
for the first button we use this code :
VB.NET:
    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
i think that this part is easy to understand for all guys,
lets move to further step
VB.NET:
 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim encFile As String = textbox1.Text + ".enc"
        Dim fs As FileStream = New FileStream(encFile, _
        FileMode.Create, FileAccess.Write)
        Dim ir As Math
        
        Dim sr As StreamReader = New _
        StreamReader(textbox1.Text)
        Dim strinput As String = (sr).ReadToEnd()
        sr.Close()
        Dim bytearrayinput() As Byte = _
        Encoding.Default.GetBytes(strinput)
        des = New DESCryptoServiceProvider
        Dim desencrypt As ICryptoTransform = _
        des.CreateEncryptor()
        Dim CryptoStream As CryptoStream = _
        New CryptoStream(fs, desencrypt, _
        CryptoStreamMode.Write)
        CryptoStream.Write(bytearrayinput, 0, _
        bytearrayinput.Length)
        CryptoStream.Close()
        fs.Close()
        MessageBox.Show("encrypted :p")
    End Sub
this button will encrypt your plain text file & it will save it as .enc extension
we use two major task in this button , first reading plain text by our traditional way & then we use of an instantiated object to encrypt our bytes by DESCryptoServiceProvider
VB.NET:
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim fsread As FileStream = _
New FileStream(textbox1.Text, _
FileMode.Open, FileAccess.Read)
        Dim desdecrypt As ICryptoTransform = _
        des.CreateDecryptor()
        Dim cryptostreamDecr As CryptoStream = _
        New CryptoStream(fsread, _
        desdecrypt, CryptoStreamMode.Read)
        Dim decryptedFile As String = New _
        StreamReader(cryptostreamDecr).ReadToEnd()
        Dim fi As FileInfo = New FileInfo(textbox1.Text)
        Dim origionalFile As String = _
        textbox1.Text.Substring(0, textbox1.Text.Length _
        - fi.Extension.Length)
        Dim fileWriter As StreamWriter = New _
        StreamWriter(origionalFile)
        fileWriter.Write(decryptedFile)
        fileWriter.Close()
        MessageBox.Show("decrypted :D")
    End Sub
this time we used CreateDecryptor() to read & decrypt encrypted text & write it on a file by Streamwriter
except streamreader & Writer , we used another Stream that is CryptoStream
, for the first time CryptoStream mode is write & in the second time , its on Read mode , actually we use it when we wanna work with cipher streams
have a nice coding
 
Back
Top