Datei: NDODLL/AesHelper.cs
Last Commit (831b962)
1 | using System; |
2 | using System.Collections.Generic; |
3 | using System.IO; |
4 | using System.Linq; |
5 | using System.Security.Cryptography; |
6 | using System.Text; |
7 | using System.Threading.Tasks; |
8 | namespace NDO |
9 | { |
10 | ····class AesHelper |
11 | ····{ |
12 | |
13 | ········public static string Encrypt( string s, byte[] privateKey ) |
14 | ········{ |
15 | ············if (String.IsNullOrEmpty( s )) |
16 | ················return String.Empty; |
17 | |
18 | using ( var aes= new AesCryptoServiceProvider( ) |
19 | ············{ |
20 | Key = privateKey, |
21 | Mode = CipherMode. CBC, |
22 | Padding = PaddingMode. PKCS7 |
23 | } ) |
24 | ············{ |
25 | |
26 | ················var input = Encoding.UTF8.GetBytes( s ); |
27 | ················aes.GenerateIV();··// This creates the Initialization Vector |
28 | ················var iv = aes.IV; |
29 | using ( var encrypter = aes. CreateEncryptor( aes. Key, iv ) ) |
30 | ················using (var cipherStream = new MemoryStream()) |
31 | ················{ |
32 | ····················using (var tCryptoStream = new CryptoStream( cipherStream, encrypter, CryptoStreamMode.Write )) |
33 | ····················using (var tBinaryWriter = new BinaryWriter( tCryptoStream )) |
34 | ····················{ |
35 | ························// Prepend IV to data. |
36 | ························// Write directly to the Memorystream |
37 | ························cipherStream.Write( iv, 0, iv.Length ); |
38 | ························tBinaryWriter.Write( input ); |
39 | ························tCryptoStream.FlushFinalBlock(); |
40 | ····················} |
41 | |
42 | ····················return Convert.ToBase64String( cipherStream.ToArray() ); |
43 | ················} |
44 | ············} |
45 | ········} |
46 | |
47 | ········public static string Decrypt( string s, byte[] privateKey ) |
48 | ········{ |
49 | ············if (s == null) |
50 | ················return null; |
51 | ············if (s == String.Empty) |
52 | ················return String.Empty; |
53 | var aes= new AesCryptoServiceProvider( ) |
54 | { |
55 | Key = privateKey, |
56 | Mode = CipherMode. CBC, |
57 | ················Padding = PaddingMode.PKCS7 |
58 | ············}; |
59 | |
60 | byte[] input = Convert. FromBase64String( s ) ; |
61 | |
62 | // Get first 16 bytes of IV and use it to decrypt |
63 | ············var iv = new byte[16]; |
64 | ············Array.Copy( input, 0, iv, 0, iv.Length ); |
65 | |
66 | ············using (var ms = new MemoryStream()) |
67 | ············{ |
68 | using ( var cs = new CryptoStream( ms, aes. CreateDecryptor( aes. Key, iv ) , CryptoStreamMode. Write ) ) |
69 | ················using (var binaryWriter = new BinaryWriter( cs )) |
70 | ················{ |
71 | ····················// Decrypt Cipher Text from Message |
72 | ····················binaryWriter.Write( |
73 | ························input, |
74 | ························iv.Length, |
75 | ························input.Length - iv.Length |
76 | ····················); |
77 | ················} |
78 | |
79 | return Encoding. Default. GetString( ms. ToArray( ) ) ; |
80 | ············} |
81 | ········} |
82 | ····} |
83 | } |
84 |
New Commit (add8490)
1 | using System; |
2 | using System.IO; |
3 | using System.Security.Cryptography; |
4 | using System.Text; |
5 | namespace NDO |
6 | { |
7 | ····class AesHelper |
8 | ····{ |
9 | |
10 | ········public static string Encrypt( string s, byte[] privateKey ) |
11 | ········{ |
12 | ············if (String.IsNullOrEmpty( s )) |
13 | ················return String.Empty; |
14 | |
15 | using ( var aes = Aes. Create( ) ) |
16 | ············{ |
17 | aes. Key = privateKey; |
18 | aes. Mode = CipherMode. CBC; |
19 | aes. Padding = PaddingMode. PKCS7; |
20 | var input = Encoding. UTF8. GetBytes( s) ; |
21 | ················aes.GenerateIV();··// This creates the Initialization Vector |
22 | ················var iv = aes.IV; |
23 | using ( var encrypter = aes. CreateEncryptor( privateKey, iv ) ) |
24 | ················using (var cipherStream = new MemoryStream()) |
25 | ················{ |
26 | ····················using (var tCryptoStream = new CryptoStream( cipherStream, encrypter, CryptoStreamMode.Write )) |
27 | ····················using (var tBinaryWriter = new BinaryWriter( tCryptoStream )) |
28 | ····················{ |
29 | ························// Prepend IV to data. |
30 | ························// Write directly to the Memorystream |
31 | ························cipherStream.Write( iv, 0, iv.Length ); |
32 | ························tBinaryWriter.Write( input ); |
33 | ························tCryptoStream.FlushFinalBlock(); |
34 | ····················} |
35 | |
36 | ····················return Convert.ToBase64String( cipherStream.ToArray() ); |
37 | ················} |
38 | ············} |
39 | ········} |
40 | |
41 | ········public static string Decrypt( string s, byte[] privateKey ) |
42 | ········{ |
43 | ············if (s == null) |
44 | ················return null; |
45 | ············if (s == String.Empty) |
46 | ················return String.Empty; |
47 | var aes = Aes. Create( ) ; |
48 | aes. Key = privateKey; |
49 | aes. Mode = CipherMode. CBC; |
50 | aes. Padding = PaddingMode. PKCS7; |
51 | |
52 | byte[] input = Convert. FromBase64String( s) ; |
53 | |
54 | // Get first 16 bytes as IV and use it to decrypt |
55 | ············var iv = new byte[16]; |
56 | ············Array.Copy( input, 0, iv, 0, iv.Length ); |
57 | |
58 | ············using (var ms = new MemoryStream()) |
59 | ············{ |
60 | using ( var cs = new CryptoStream( ms, aes. CreateDecryptor( privateKey, iv ) , CryptoStreamMode. Write ) ) |
61 | ················using (var binaryWriter = new BinaryWriter( cs )) |
62 | ················{ |
63 | ····················// Decrypt Cipher Text from Message |
64 | ····················binaryWriter.Write( |
65 | ························input, |
66 | ························iv.Length, |
67 | ························input.Length - iv.Length |
68 | ····················); |
69 | ················} |
70 | |
71 | return Encoding. UTF8. GetString( ms. ToArray( ) ) ; |
72 | ············} |
73 | ········} |
74 | ····} |
75 | } |
76 |