Encryption and decryption

Source: Internet
Author: User
Tags decrypt integer variables
Encryption | Decrypt Imports System.IO
Imports System.Security.Cryptography

' Data encryption/decryption class
Public Class Crydata
' Encryption key, initialization vector
Public ReadOnly Crykey as Byte () = {9, 4, 2, 8, 5, 1, 4, 9, 7, 6, 9, 5, 1, 13, 7, 5, 14, 9, 10, 15, 0, 1, 14, 5, 9, 4, 3, 8, 2, 10}
Public ReadOnly Cryiv as Byte () = {7, 1, 8, 8, 2, 8, 7, 1, 4, 5, 6, 3, 5, 6, 7}


' File encryption
Public Sub encryptdata (ByVal inname As String, ByVal Outname As String, _
Optional ByVal Rijnkey () as Byte = Nothing, _
Optional ByVal Rijniv () as Byte = Nothing)

If Rijnkey is nothing Then
Rijnkey = Crykey


End If
If Rijniv is nothing Then
Rijniv = Cryiv

End If
ReDim Preserve Rijnkey (31)
ReDim Preserve Rijniv (15)

' Create the ' file streams to handle the input and output files.
Dim Fin as New FileStream (Inname, FileMode.Open, FileAccess.Read)
Dim Fout as New FileStream (Outname, FileMode.OpenOrCreate, FileAccess.ReadWrite)
Fout. SetLength (0)

' Create variables to help with read and write.
Dim Bin (1024) as Byte ' This was intermediate storage for the encryption.
Dim Rdlen as Long = 0 ' is the total number of bytes written.
Dim Totlen as Long = Fin. Length ' total length ' of the input file.
Dim Len as Integer ' is the number of bytes to being written at a time.
' Creates the default implementation, which is rijndaelmanaged.
Dim Rijn as SymmetricAlgorithm = Symmetricalgorithm.create ()
Dim Encstream as New CryptoStream (Fout, _
Rijn. CreateEncryptor (Rijnkey, Rijniv), CryptoStreamMode.Write)

' Console.WriteLine ("Encrypting ...")

' Read from the ' input file, then encrypt and write to the output file.
While Rdlen < Totlen
Len = Fin. Read (Bin, 0, 1024)
Encstream.write (Bin, 0, Len)
Rdlen = Convert.ToInt32 (Rdlen + len)
' Console.WriteLine ("{0} bytes processed", Rdlen)
End While


' Fout. Seek (0, Seekorigin.begin)
' Dim returnvalue as String
' returnvalue = New StreamReader (fout). ReadToEnd ()

Encstream.close ()
Fout. Close ()
Fin. Close ()

End Sub

' File decryption

Public Sub Decryptdata (ByVal inname As String, ByVal Outname As String, _
Optional ByVal Rijnkey () as Byte = Nothing, _
Optional ByVal Rijniv () as Byte = Nothing)

If Rijnkey is nothing Then
Rijnkey = Crykey

End If

If Rijniv is nothing Then
Rijniv = Cryiv

End If

ReDim Preserve Rijnkey (31)
ReDim Preserve Rijniv (15)

' Create the ' file streams to handle the input and output files.
Dim Fin as New FileStream (Inname, FileMode.Open, FileAccess.Read)
Dim Fout as New FileStream (Outname, FileMode.OpenOrCreate, _
FileAccess.ReadWrite)
Fout. SetLength (0)

' Create variables to help with read and write.
Dim Bin (1024) as Byte ' This was intermediate storage for the encryption.
Dim Rdlen as Long = 0 ' is the total number of bytes written.
Dim Totlen as Long = Fin. Length ' total length ' of the input file.
Dim Len as Integer ' is the number of bytes to being written at a time.
' Creates the default implementation, which is rijndaelmanaged.
Dim Rijn as SymmetricAlgorithm = Symmetricalgorithm.create ()
Dim Encstream as New CryptoStream (Fout, _
Rijn. CreateDecryptor (Rijnkey, Rijniv), CryptoStreamMode.Write)

' Console.WriteLine ("Encrypting ...")

' Read from the ' input file, then encrypt and write to the output file.
While Rdlen < Totlen
Len = Fin. Read (Bin, 0, 1024)
Encstream.write (Bin, 0, Len)
Rdlen = Convert.ToInt32 (Rdlen + len)
' Console.WriteLine ("{0} bytes processed", Rdlen)

End While

Encstream.close ()
Fout. Close ()
Fin. Close ()

End Sub
' File decryption

Public Function Decryptdata (ByVal inname as String, _
Optional ByVal Rijnkey () as Byte = Nothing, _
Optional ByVal Rijniv () as Byte = Nothing) as String

If Rijnkey is nothing Then
Rijnkey = Crykey

End If

If Rijniv is nothing Then
Rijniv = Cryiv

End If

ReDim Preserve Rijnkey (31)
ReDim Preserve Rijniv (15)

' Create the ' file streams to handle the input and output files.
Dim Fin as New FileStream (Inname, FileMode.Open, FileAccess.Read)
' Dim Fout as New FileStream (Outname, FileMode.OpenOrCreate, _
' FileAccess.ReadWrite)
' Store stream,
Dim OutStream as New MemoryStream () ' (Arrinbyte, True) ' (Arrinbyte, True)


' Create variables to help with read and write.
Dim Bin (1024) as Byte ' This was intermediate storage for the encryption.
Dim Rdlen as Long = 0 ' is the total number of bytes written.
Dim Totlen as Long = Fin. Length ' total length ' of the input file.
Dim Len as Integer ' is the number of bytes to being written at a time.
' Creates the default implementation, which is rijndaelmanaged.
While Rdlen < Totlen
Len = Fin. Read (Bin, 0, 1024)
Outstream.write (Bin, 0, Len)
Rdlen = Convert.ToInt32 (Rdlen + len)
' Console.WriteLine ("{0} bytes processed", Rdlen)

End While

Outstream.seek (0, Seekorigin.begin)

Dim Rijn as SymmetricAlgorithm = Symmetricalgorithm.create ()
Dim Encstream as New CryptoStream (OutStream, _
Rijn. CreateDecryptor (Rijnkey, Rijniv), CryptoStreamMode.Read)

' Console.WriteLine ("Encrypting ...")

' Read from the ' input file, then encrypt and write to the output file.


Dim ReturnValue as String
returnvalue = new StreamReader (Encstream, New System.Text.UnicodeEncoding ()). ReadToEnd ()


Encstream.close ()
Outstream.close ()
Fin. Close ()
Return returnvalue

End Function





' Encrypt
' In:intext to encrypt original text
' In:rijnkey 32-bit key
' In:rijniv 16-bit initialization vector

' Out:return encrypted text (failed for null encryption)

Public Function Crypt (ByVal intext as String, _
Optional ByVal Rijnkey () as Byte = Nothing, _
Optional ByVal Rijniv () as Byte = Nothing) as String



' Create the ' file streams to handle the input and output files.
' Dim fin as New FileStream (Inname, FileMode.Open, FileAccess.Read)
Dim Arrinbyte as Byte () = (New System.Text.UnicodeEncoding ()). GetBytes (Intext)

Dim len as Long = Arrinbyte.length

If (len Mod) > 0 Then
Dim Oldlen as Long = Len

Len = Oldlen + 32-oldlen Mod 32

ReDim Preserve arrinbyte (len-1)

End If

If Rijnkey is nothing Then
Rijnkey = Crykey

End If
If Rijniv is nothing Then
Rijniv = Cryiv

End If

ReDim Preserve Rijnkey (31)

ReDim Preserve Rijniv (15)


Dim OutStream as New MemoryStream () ' (Arrinbyte, True) ' (Arrinbyte, True)

Dim Rij as SymmetricAlgorithm = Symmetricalgorithm.create ()

Dim Encstream as New CryptoStream (OutStream, _
Rij. CreateEncryptor (Rijnkey, Rijniv), _
CryptoStreamMode.Write)

Encstream.write (arrinbyte, 0, Len)
Outstream.seek (0, Seekorigin.begin)

Dim ReturnValue as String
returnvalue = new StreamReader (OutStream, New System.Text.UnicodeEncoding ()). ReadToEnd ()

Encstream.close ()
Outstream.close ()

Return returnvalue

End Function

'--------------------------------------------------------------------------------------------------------
' Decryption

' In:intext to decrypt the text
' In:rijnkey 32-bit decryption key (must be the same as when encrypted)
' In:rijniv 16-bit decryption initialization vector (must be the same as when encrypted)

' Out:return decrypted text (failed to decrypt if null)
' Decryption process: convert ciphertext into bytes and write to memory stream, then create decryption stream according to Rijnkey and Rijniv
' Read all data from the decryption stream
'--------------------------------------------------------------------------------------------------------

Public Function Decrypt (ByVal intext as String, _
Optional ByVal Rijnkey () as Byte = Nothing, _
Optional ByVal Rijniv () as Byte = Nothing) as String



' Ciphertext converted into bytes

Dim Arrinbyte as Byte () = (New System.Text.UnicodeEncoding ()). GetBytes (Intext)

' Store stream,
Dim OutStream as New MemoryStream () ' (Arrinbyte, True) ' (Arrinbyte, True)

Dim len as Long = Arrinbyte.length
If Rijnkey is nothing Then
Rijnkey = Crykey

End If

If Rijniv is nothing Then
Rijniv = Cryiv

End If

ReDim Preserve Rijnkey (31)

ReDim Preserve Rijniv (15)


Dim Rij as SymmetricAlgorithm = Symmetricalgorithm.create ()

Outstream.write (arrinbyte, 0, Len)

Outstream.seek (0, Seekorigin.begin)

Dim Encstream as New CryptoStream (OutStream, _
Rij. CreateDecryptor (Rijnkey, Rijniv), _
CryptoStreamMode.Read)

Dim ReturnValue as String
returnvalue = new StreamReader (Encstream, New System.Text.UnicodeEncoding ()). ReadToEnd ()


Encstream.close ()
Outstream.close ()

Return returnvalue

End Function
End Class





' Encrypt
' http://community.csdn.net/Expert/topic/3199/3199494.xml?temp=.982464
Public Shared Function Encrypttext (ByVal StrText As String) as String
Return Encrypt (StrText, &%#@?) /)")
End Function

' Decryption
Public Shared Function Decrypttext (ByVal StrText As String) as String
Return Decrypt (StrText, &%#@?) /)")
End Function

' Cryptographic functions
Private Shared Function Encrypt (ByVal strText As String, ByVal Strencrkey as String) as String
Dim Bykey () as Byte = {}
Dim IV () as Byte = {&h12, &h34, &h56, &h78, &AMP;H90, &hab, &AMP;HCD, &hef}
Try
' Bykey = System.Text.Encoding.UTF8.GetBytes (Left (Strencrkey, 8))
Bykey = System.Text.Encoding.UTF8.GetBytes (strencrkey.substring (0, 8))
Dim des as New System.Security.Cryptography.DESCryptoServiceProvider
Dim Inputbytearray () as Byte = System.Text.Encoding.UTF8.GetBytes (StrText)
Dim MS as New MemoryStream
Dim CS as New CryptoStream (MS, Des. CreateEncryptor (Bykey, IV), CryptoStreamMode.Write)
Cs. Write (Inputbytearray, 0, Inputbytearray.length)
Cs. FlushFinalBlock ()
Return convert.tobase64string (Ms. ToArray ())
Catch ex as Exception
Return ex. Message
End Try
End Function

' Decryption function
Private Shared Function Decrypt (ByVal strText As String, ByVal Sdecrkey as String) as String
Dim Bykey () as Byte = {}
Dim IV () as Byte = {&h12, &h34, &h56, &h78, &AMP;H90, &hab, &AMP;HCD, &hef}
Dim Inputbytearray (strtext.length) as Byte
Try
' Bykey = System.Text.Encoding.UTF8.GetBytes (Left (Sdecrkey, 8))
Bykey = System.Text.Encoding.UTF8.GetBytes (sdecrkey.substring (0, 8))

Dim des as New descryptoserviceprovider
Inputbytearray = convert.frombase64string (StrText)
Dim MS as New MemoryStream
Dim CS as New CryptoStream (MS, Des. CreateDecryptor (Bykey, IV), CryptoStreamMode.Write)
Cs. Write (Inputbytearray, 0, Inputbytearray.length)
Cs. FlushFinalBlock ()
Dim encoding as System.Text.Encoding = System.Text.Encoding.UTF8
return encoding. GetString (Ms. ToArray ())
Catch ex as Exception
Return ex. Message
End Try
End Function






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.