Use vb.net to encrypt and decrypt files. (like English spirit, Chinese is not good)

Source: Internet
Author: User
Tags decrypt
Encryption | decryption | Chinese Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text

Module Encrypt
Private Const sSecretKey as String = "Password"

Public Sub Main ()
EncryptFile ("C:\temp\test.txt", _
"C:\temp\Encrypted.txt", _
sSecretKey)
DecryptFile ("C:\temp\Encrypted.txt", _
"C:\temp\Decrypted.txt", _
sSecretKey)
End Sub

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 ()

' Sets the security code for the DES algorithm.
' must be 64 bits, 8 bytes
Des. Key = ASCIIEncoding.ASCII.GetBytes (SKey)


DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey)

' Create des plus password instance
Dim desencrypt as ICryptoTransform = DES. CreateEncryptor ()
'
Dim CryptoStream as New CryptoStream (fsencrypted, _
Desencrypt, _
CryptoStreamMode.Write)

' Read files to array
Dim bytearrayinput (fsinput.length-1) as Byte
fsInput.Read (bytearrayinput, 0, bytearrayinput. Length)
' Write to the 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 ()
Des. Key () = ASCIIEncoding.ASCII.GetBytes (SKey)
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey)

' Read Encrypted File
Dim Fsread as New FileStream (sInputFilename, FileMode.Open, FileAccess.Read)
'
Dim desdecrypt As ICryptoTransform = DES. CreateDecryptor ()
'
Dim CRYPTOSTREAMDECR as New CryptoStream (fsread, Desdecrypt, CryptoStreamMode.Read)
' Output to decrypted file
Dim fsdecrypted as New StreamWriter (sOutputFileName)
Fsdecrypted.write (New StreamReader (CRYPTOSTREAMDECR). READTOEND)
Fsdecrypted.flush ()
Fsdecrypted.close ()
End Sub
End Module

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.