You are on page 1of 5

C:\Users\Cengiz\Desktop\Yeni klasör\Encryption.

vb 1
1 Imports Microsoft.VisualBasic.CompilerServices
2 Imports System
3 Imports System.IO
4 Imports System.Security.Cryptography
5 Imports System.Text
6
7 Namespace Encryption
8 Public Module Encryption
9 Private _passPhrase As String
10
11 Private _saltValue As String
12
13 Private _hashAlgorithm As String
14
15 Private _passwordIterations As Integer
16
17 Private _initVector As String
18
19 Private _keySize As Integer
20
21 Sub New()
22 ._passPhrase = "Pas5pr@se"
23 ._saltValue = "s@1tValue"
24 ._hashAlgorithm = "SHA1"
25 ._passwordIterations = 2
26 ._initVector = "@1B2c3D4e5F6g7H8"
27 ._keySize = 256
28 End Sub
29
30 Public Sub Example()
31 Dim str As String = "Hello, World!"
32 Dim str1 As String = "Pas5pr@se"
33 Dim str2 As String = "s@1tValue"
34 Dim str3 As String = "SHA1"
35 Dim str4 As String = "@1B2c3D4e5F6g7H8"
36 Console.WriteLine(String.Format("Plaintext : {0}", str))
37 Dim str5 As String =  .DataEncryptor.Encrypt(str, str1, 
str2, str3, 2, str4, 256)
38 Console.WriteLine(String.Format("Encrypted : {0}", str5))
39 str =  .DataEncryptor.Decrypt(str5, str1, str2, str3, 2, 
str4, 256)
40 Console.WriteLine(String.Format("Decrypted : {0}", str))
41 End Sub
42
43 Private Function  (ByVal string_0 As String) As Byte()
44 Dim numArray(string_0.Length * 2 ­ 1 + 1 ­ 1) As Byte
45 Buffer.BlockCopy(string_0.ToCharArray(), 0, numArray, 0, 
(numArray.Length))
46 Return numArray
47 End Function
48
49 Public Sub LoadOptionsFiles(ByVal OptionsFolder As String, ByRef 
SAG10_Options As SAG10Options)
50 If  Not Directory.Exists(OptionsFolder) Then
51 Directory.CreateDirectory(OptionsFolder)
52 End If
C:\Users\Cengiz\Desktop\Yeni klasör\Encryption.vb 2
53 Dim str As String = String.Concat(OptionsFolder, "\Options.xml")
54 If  Not File.Exists(str) Then
55 SAG10_Options.DefaultChartType = "Southwire"
56 SAG10Options.Save(SAG10_Options, str)
57 Else
58 SAG10_Options = SAG10Options.Load(str)
59 End If
60 End Sub
61
62 Public Function Predicate_DataFiles(ByVal File As FileInfo) As Boolean
63 If  Operators.CompareString(File.Extension, ".dat", False) = 0
Then
64 Return True
65 End If
66 Return False
67 End Function
68
69 Public Class DataEncryptor
70 Private Const  As Integer = 4096
71
72 Public Sub New()
73 MyBase.New()
74 End Sub
75
76 Public Shared Function Decrypt(ByVal cipherText As String) As 
String
77 Dim str As String =  .Decrypt
(cipherText,  ._passPhrase,  ._saltValue, 
._hashAlgorithm,  ._passwordIterations, 
._initVector,  ._keySize)
78 Return str
79 End Function
80
81 Public Shared Function Decrypt(ByVal cipherText As String, ByVal 
passPhrase As String, ByVal saltValue As String, ByVal 
hashAlgorithm As String, ByVal passwordIterations As Integer, 
ByVal initVector As String, ByVal keySize As Integer) As String
82 Dim bytes As Byte() = Encoding.ASCII.GetBytes(initVector)
83 Dim numArray As Byte() = Encoding.ASCII.GetBytes(saltValue)
84 Dim numArray1 As Byte() = Convert.FromBase64String(cipherText)
85 Dim passwordDeriveByte As PasswordDeriveBytes = New 
PasswordDeriveBytes(passPhrase, numArray, hashAlgorithm, 
passwordIterations)
86 Dim bytes1 As Byte() = passwordDeriveByte.GetBytes(
(Math.Round( (keySize) / 8)))
87 Dim rijndaelManaged As 
.RijndaelManaged = New 
.RijndaelManaged() With
88 {
89 .Mode = CipherMode.CBC
90 }
91 Dim cryptoTransform As ICryptoTransform = 
rijndaelManaged.CreateDecryptor(bytes1, bytes)
92 Dim memoryStream As  .MemoryStream = New 
.MemoryStream(numArray1)
93 Dim cryptoStream As  .CryptoStream 
C:\Users\Cengiz\Desktop\Yeni klasör\Encryption.vb 3
= New  .CryptoStream
(memoryStream, cryptoTransform, CryptoStreamMode.Read)
94 Dim numArray2( (numArray1.Length) + 1 ­ 1) As Byte
95 Dim ınt32 As Integer = cryptoStream.Read(numArray2, 0, 
(numArray2.Length))
96 memoryStream.Close()
97 cryptoStream.Close()
98 Return Encoding.UTF8.GetString(numArray2, 0, ınt32)
99 End Function
100
101 Public Shared Sub DecryptFile(ByVal sourceFileName As String, 
ByVal outFileName As String)
102 Dim ınt32 As Integer
103 Dim bytes As Byte() = Encoding.ASCII.GetBytes
( ._initVector)
104 Dim numArray As Byte() = Encoding.ASCII.GetBytes
( ._saltValue)
105 Dim rfc2898DeriveByte As Rfc2898DeriveBytes = New 
Rfc2898DeriveBytes( ._passPhrase, numArray, 
._passwordIterations)
106 Dim bytes1 As Byte() = rfc2898DeriveByte.GetBytes(
(Math.Round( ( ._keySize) / 8)))
107 Dim rijndaelManaged As 
.RijndaelManaged = New 
.RijndaelManaged() With
108 {
109 .Mode = CipherMode.CBC
110 }
111 Dim cryptoTransform As ICryptoTransform = 
rijndaelManaged.CreateDecryptor(bytes1, bytes)
112 Dim numArray1(4096) As Byte
113 Using fileStream As  .FileStream = New 
.FileStream(sourceFileName, FileMode.Open)
114 Using fileStream1 As  .FileStream = New 
.FileStream(outFileName, FileMode.Create)
115 Using cryptoStream As 
.CryptoStream = New 
.CryptoStream(fileStream, 
cryptoTransform, CryptoStreamMode.Read)
116 Do
117 ınt32 = cryptoStream.Read(numArray1, 0, 4096)
118 If  ınt32 <= 0 Then
119 Continue Do
120 End If
121 fileStream1.Write(numArray1, 0, ınt32)
122 Loop While ınt32 >= 4096
123 End Using
124 End Using
125 End Using
126 End Sub
127
128 Public Shared Function Encrypt(ByVal plainText As String) As 
String
129 Dim str As String =  .Encrypt
(plainText,  ._passPhrase,  ._saltValue, 
._hashAlgorithm,  ._passwordIterations, 
C:\Users\Cengiz\Desktop\Yeni klasör\Encryption.vb 4
._initVector,  ._keySize)
130 Return str
131 End Function
132
133 Public Shared Function Encrypt(ByVal plainText As String, ByVal 
passPhrase As String, ByVal saltValue As String, ByVal 
hashAlgorithm As String, ByVal passwordIterations As Integer, 
ByVal initVector As String, ByVal keySize As Integer) As String
134 Dim bytes As Byte() = Encoding.ASCII.GetBytes(initVector)
135 Dim numArray As Byte() = Encoding.ASCII.GetBytes(saltValue)
136 Dim bytes1 As Byte() = Encoding.UTF8.GetBytes(plainText)
137 Dim passwordDeriveByte As PasswordDeriveBytes = New 
PasswordDeriveBytes(passPhrase, numArray, hashAlgorithm, 
passwordIterations)
138 Dim numArray1 As Byte() = passwordDeriveByte.GetBytes(
(Math.Round( (keySize) / 8)))
139 Dim rijndaelManaged As 
.RijndaelManaged = New 
.RijndaelManaged() With
140 {
141 .Mode = CipherMode.CBC
142 }
143 Dim cryptoTransform As ICryptoTransform = 
rijndaelManaged.CreateEncryptor(numArray1, bytes)
144 Dim memoryStream As  .MemoryStream = New 
.MemoryStream()
145 Dim cryptoStream As  .CryptoStream 
= New  .CryptoStream
(memoryStream, cryptoTransform, CryptoStreamMode.Write)
146 cryptoStream.Write(bytes1, 0,  (bytes1.Length))
147 cryptoStream.FlushFinalBlock()
148 Dim array As Byte() = memoryStream.ToArray()
149 memoryStream.Close()
150 cryptoStream.Close()
151 Return Convert.ToBase64String(array)
152 End Function
153
154 Public Shared Sub EncryptFile(ByVal sourceFileName As String, 
ByVal outFileName As String)
155 Dim ınt32 As Integer
156 If  Not File.Exists(sourceFileName) Then
157 Throw New FileNotFoundException(String.Format("Cannot find
source file '{0}'", sourceFileName))
158 End If
159 Dim bytes As Byte() = Encoding.ASCII.GetBytes
( ._initVector)
160 Dim numArray As Byte() = Encoding.ASCII.GetBytes
( ._saltValue)
161 Dim rfc2898DeriveByte As Rfc2898DeriveBytes = New 
Rfc2898DeriveBytes( ._passPhrase, numArray, 
._passwordIterations)
162 Dim bytes1 As Byte() = rfc2898DeriveByte.GetBytes(
(Math.Round( ( ._keySize) / 8)))
163 Dim rijndaelManaged As 
.RijndaelManaged = New 
.RijndaelManaged() With
C:\Users\Cengiz\Desktop\Yeni klasör\Encryption.vb 5
164 {
165 .Mode = CipherMode.CBC
166 }
167 Dim cryptoTransform As ICryptoTransform =
rijndaelManaged.CreateEncryptor(bytes1, bytes)
168 Dim numArray1(4096) As Byte
169 Using fileStream As .FileStream = New
.FileStream(sourceFileName, FileMode.Open)
170 Using fileStream1 As .FileStream = New
.FileStream(outFileName, FileMode.Create)
171 Using cryptoStream As
.CryptoStream = New
.CryptoStream(fileStream1,
cryptoTransform, CryptoStreamMode.Write)
172 Do
173 ınt32 = fileStream.Read(numArray1, 0, 4096)
174 If ınt32 <= 0 Then
175 Continue Do
176 End If
177 cryptoStream.Write(numArray1, 0, ınt32)
178 Loop While ınt32 >= 4096
179 cryptoStream.FlushFinalBlock()
180 End Using
181 End Using
182 End Using
183 End Sub
184 End Class
185 End Module
186 End Namespace

You might also like