Encryption and decryption

zhaozj2021-02-16  90

Imports System.IOMPORTS System.Security.cryptography

'Data Plus / Decryption 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, 6, 5, 14, 5, 9, 4, 3, 8, 2, 10} public retadonly cryiv as byte () = {7, 1, 8, 8, 2, 8, 7, 1, 4, 5, 6, 3, 5, 6, 7}

"File encryption public sub encryptdddata (byval inname as string, _ optional byval rijnkey () as byte = Nothing, _ optional byval rijniv () as byte = nothing

IF rijnkey is nothing the rijnkey = CryKey

End if if rijniv is nothing the 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 is intermediate storage for the encryption. Dim rdlen As Long = 0 'This is the total number of bytes written. Dim totlen As Long = fin.Length 'Total length of the input file. Dim len As Integer' This is the number of bytes to be 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

'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 Outname As String, _ Optional Byval Rijnkey () AS BYTE = Nothing, _ Optional Byval Rijniv () AS BYTE = Nothing

IF rijnkey is nothing the rijnkey = CryKey

END IF

IF rijniv is nothing the 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 is intermediate storage for the encryption. Dim rdlen As Long = 0 'This is the total number of bytes written. Dim totlen As Long = fin.Length 'Total length of the input file. Dim len As Integer' This is the number of bytes to be 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, the encrypt and write to the output file. While Rdlen

End while

EncStream.close () fout.close () Fin.Close ()

End sub '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 the rijnkey = CryKey

END IF

IF rijniv is nothing the 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)' storage stream, Dim outStream As New MemoryStream () '(arrInByte, True)' (arrInByte, True) 'Create variables to help with read and write. Dim bin (1024) As Byte' This is intermediate storage for the encryption. Dim rdlen As LONG = 0 'this is the total number of bytes Written. Dim Totlen as long = fin.length' Total Length 'this is the number of bytes to be written at a time.' Creates the Default Implementation, Which is rijndaelmanaged. While Rdlen

End while

Outstream.seek (0, seekorigin.begin)

Dim rijn as symmetricalgorithm = symmetricgorithm.create () DIM ENCSTREAM As New Cryptostream (Outstream, _ rijn.createdeptor (Rijnkey, Rijniv), CryptostreamMode.Read)

'Console.Writeline ("Encrypting ...")

'Read from the Input File, The 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

'Encrypted' IN: INTEXT Terminal The original text 'IN: Rijnkey 32-bit key' in: Rijniv 16-bit initialization vector 'out: returna encrypted text (empty encryption failed) 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 (Intelt) Diml Len as long = arrinbyte.length if (Len MOD 32)> 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 ()). encStream ReadToEnd (). Close () outstream.close () Return ReturnValue End Function '

-------------------------------------------------- -------------------------------------------------- ---- 'Decrypt' IN: IntexT To solve the densive 'in: Rijnkey 32-bit decryption key (the same as encrypted)' in: Rijniv 16-bit decrypts initialization vector (the same as the same as encryption) 'OUT : Return decryption text (for empty decryption failed) 'Decryption process: write secret in line to byte to write the memory stream, and then read all data from decryption stream according to Rijnkey and Rijniv. -------------------------------------------------- -------------------------------------------------- --- Public Function Decrypt (Byval Intext As String, _ Optional Byval Rijnkey () AS BYTE = Nothing, _ Optional Byval Rijniv () AS BYTE = Nothing) AS String 'Cipheet Convert into byte DIM ArrinByte as Byte () = (New System.Text.UnicodeEncoding ()). GetBytes (inText) 'storage 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 FunctionEnd Class 'encryption' http://community.9cbs.net/Expert/topic/ 3199 / 3199494.xml? Temp = .982464 Public Shared Function EncryPtText (Byval Strtext As String) AS String Return Encrypt (string Return Encrypt (string ")") End Function

'Decryption Public Shared Function DecryptText (Byval Strtext As String) AS String Return Decrypt (Strtext, "& %#@z") End Function

'Encryption function 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, & H90, & HAB, & HCD , & Hef} try 'bykey = system.text.Encoding.utf8.getbytes (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 EXMESSAGE 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, & H90, & HAB, & 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

转载请注明原文地址:https://www.9cbs.com/read-11763.html

New Post(0)