1
- // Author: Dominic Beger (Trade/ProgTrade) 2016
1
+ // Copyright © Dominic Beger 2018
2
2
3
3
using System ;
4
4
using System . IO ;
@@ -10,16 +10,16 @@ namespace nUpdate.Administration.Core
10
10
public class AesManager
11
11
{
12
12
/// <summary>
13
- /// Encrypts a string with the given key and initializing vector.
13
+ /// Decrypts a string with the given key and initializing vector.
14
14
/// </summary>
15
- /// <param name="plainText ">The text to encrypt .</param>
16
- /// <param name="keyPassword">The password which the key should be derived from .</param>
17
- /// <param name="ivPassword">The password which the initializing vector should be drived from .</param>
18
- /// <returns>Returns the encrypted string as a byte-array .</returns>
19
- public static byte [ ] Encrypt ( string plainText , string keyPassword , string ivPassword )
15
+ /// <param name="cipherText ">The byte-array of the encrypted string .</param>
16
+ /// <param name="keyPassword">The key to use .</param>
17
+ /// <param name="ivPassword">The initializing vector to use .</param>
18
+ /// <returns>Returns the plain string as SecureString .</returns>
19
+ public static SecureString Decrypt ( byte [ ] cipherText , string keyPassword , string ivPassword )
20
20
{
21
- if ( string . IsNullOrEmpty ( plainText ) )
22
- throw new ArgumentNullException ( nameof ( plainText ) ) ;
21
+ if ( cipherText == null || cipherText . Length <= 0 )
22
+ throw new ArgumentNullException ( nameof ( cipherText ) ) ;
23
23
if ( keyPassword == null || keyPassword . Length <= 0 )
24
24
throw new ArgumentNullException ( nameof ( keyPassword ) ) ;
25
25
if ( ivPassword == null || ivPassword . Length <= 0 )
@@ -29,47 +29,49 @@ public static byte[] Encrypt(string plainText, string keyPassword, string ivPass
29
29
new byte [ ] { 0x43 , 0x87 , 0x23 , 0x72 , 0x45 , 0x56 , 0x68 , 0x14 , 0x62 , 0x84 } ) ;
30
30
var ivPasswordDeriveBytes = new Rfc2898DeriveBytes ( ivPassword ,
31
31
new byte [ ] { 0x43 , 0x87 , 0x23 , 0x72 , 0x45 , 0x56 , 0x68 , 0x14 , 0x62 , 0x84 } ) ;
32
- byte [ ] encrypted ;
33
32
33
+ string plaintext ;
34
34
using ( var aesAlg = new AesManaged ( ) )
35
35
{
36
36
aesAlg . KeySize = 256 ;
37
- aesAlg . Key = keyPasswordDeriveBytes . GetBytes ( aesAlg . KeySize / 8 ) ;
38
- aesAlg . IV = ivPasswordDeriveBytes . GetBytes ( aesAlg . BlockSize / 8 ) ;
37
+ aesAlg . Key = keyPasswordDeriveBytes . GetBytes ( aesAlg . KeySize / 8 ) ;
38
+ aesAlg . IV = ivPasswordDeriveBytes . GetBytes ( aesAlg . BlockSize / 8 ) ;
39
39
40
40
// Create a decrytor to perform the stream transform.
41
- var encryptor = aesAlg . CreateEncryptor ( aesAlg . Key , aesAlg . IV ) ;
41
+ var decryptor = aesAlg . CreateDecryptor ( aesAlg . Key , aesAlg . IV ) ;
42
42
43
- // Create the streams used for encryption .
44
- using ( var msEncrypt = new MemoryStream ( ) )
43
+ // Create the streams used for decryption .
44
+ using ( var msDecrypt = new MemoryStream ( cipherText ) )
45
45
{
46
- using ( var csEncrypt = new CryptoStream ( msEncrypt , encryptor , CryptoStreamMode . Write ) )
46
+ using ( var csDecrypt = new CryptoStream ( msDecrypt , decryptor , CryptoStreamMode . Read ) )
47
47
{
48
- using ( var swEncrypt = new StreamWriter ( csEncrypt ) )
48
+ using ( var srDecrypt = new StreamReader ( csDecrypt ) )
49
49
{
50
- //Write all data to the stream.
51
- swEncrypt . Write ( plainText ) ;
50
+ // Read the decrypted bytes from the decrypting stream
51
+ // and place them in a string.
52
+ plaintext = srDecrypt . ReadToEnd ( ) ;
52
53
}
53
- encrypted = msEncrypt . ToArray ( ) ;
54
54
}
55
55
}
56
56
}
57
57
58
- // Return the encrypted bytes from the memory stream.
59
- return encrypted ;
58
+ var securedPlainText = new SecureString ( ) ;
59
+ foreach ( var c in plaintext )
60
+ securedPlainText . AppendChar ( c ) ;
61
+ return securedPlainText ;
60
62
}
61
63
62
64
/// <summary>
63
- /// Decrypts a string with the given key and initializing vector.
65
+ /// Encrypts a string with the given key and initializing vector.
64
66
/// </summary>
65
- /// <param name="cipherText ">The byte-array of the encrypted string .</param>
66
- /// <param name="keyPassword">The key to use .</param>
67
- /// <param name="ivPassword">The initializing vector to use .</param>
68
- /// <returns>Returns the plain string as SecureString .</returns>
69
- public static SecureString Decrypt ( byte [ ] cipherText , string keyPassword , string ivPassword )
67
+ /// <param name="plainText ">The text to encrypt .</param>
68
+ /// <param name="keyPassword">The password which the key should be derived from .</param>
69
+ /// <param name="ivPassword">The password which the initializing vector should be drived from .</param>
70
+ /// <returns>Returns the encrypted string as a byte-array .</returns>
71
+ public static byte [ ] Encrypt ( string plainText , string keyPassword , string ivPassword )
70
72
{
71
- if ( cipherText == null || cipherText . Length <= 0 )
72
- throw new ArgumentNullException ( nameof ( cipherText ) ) ;
73
+ if ( string . IsNullOrEmpty ( plainText ) )
74
+ throw new ArgumentNullException ( nameof ( plainText ) ) ;
73
75
if ( keyPassword == null || keyPassword . Length <= 0 )
74
76
throw new ArgumentNullException ( nameof ( keyPassword ) ) ;
75
77
if ( ivPassword == null || ivPassword . Length <= 0 )
@@ -79,36 +81,35 @@ public static SecureString Decrypt(byte[] cipherText, string keyPassword, string
79
81
new byte [ ] { 0x43 , 0x87 , 0x23 , 0x72 , 0x45 , 0x56 , 0x68 , 0x14 , 0x62 , 0x84 } ) ;
80
82
var ivPasswordDeriveBytes = new Rfc2898DeriveBytes ( ivPassword ,
81
83
new byte [ ] { 0x43 , 0x87 , 0x23 , 0x72 , 0x45 , 0x56 , 0x68 , 0x14 , 0x62 , 0x84 } ) ;
84
+ byte [ ] encrypted ;
82
85
83
- string plaintext ;
84
86
using ( var aesAlg = new AesManaged ( ) )
85
87
{
86
88
aesAlg . KeySize = 256 ;
87
- aesAlg . Key = keyPasswordDeriveBytes . GetBytes ( aesAlg . KeySize / 8 ) ;
88
- aesAlg . IV = ivPasswordDeriveBytes . GetBytes ( aesAlg . BlockSize / 8 ) ;
89
+ aesAlg . Key = keyPasswordDeriveBytes . GetBytes ( aesAlg . KeySize / 8 ) ;
90
+ aesAlg . IV = ivPasswordDeriveBytes . GetBytes ( aesAlg . BlockSize / 8 ) ;
89
91
90
92
// Create a decrytor to perform the stream transform.
91
- var decryptor = aesAlg . CreateDecryptor ( aesAlg . Key , aesAlg . IV ) ;
93
+ var encryptor = aesAlg . CreateEncryptor ( aesAlg . Key , aesAlg . IV ) ;
92
94
93
- // Create the streams used for decryption .
94
- using ( var msDecrypt = new MemoryStream ( cipherText ) )
95
+ // Create the streams used for encryption .
96
+ using ( var msEncrypt = new MemoryStream ( ) )
95
97
{
96
- using ( var csDecrypt = new CryptoStream ( msDecrypt , decryptor , CryptoStreamMode . Read ) )
98
+ using ( var csEncrypt = new CryptoStream ( msEncrypt , encryptor , CryptoStreamMode . Write ) )
97
99
{
98
- using ( var srDecrypt = new StreamReader ( csDecrypt ) )
100
+ using ( var swEncrypt = new StreamWriter ( csEncrypt ) )
99
101
{
100
- // Read the decrypted bytes from the decrypting stream
101
- // and place them in a string.
102
- plaintext = srDecrypt . ReadToEnd ( ) ;
102
+ //Write all data to the stream.
103
+ swEncrypt . Write ( plainText ) ;
103
104
}
105
+
106
+ encrypted = msEncrypt . ToArray ( ) ;
104
107
}
105
108
}
106
109
}
107
110
108
- var securedPlainText = new SecureString ( ) ;
109
- foreach ( var c in plaintext )
110
- securedPlainText . AppendChar ( c ) ;
111
- return securedPlainText ;
111
+ // Return the encrypted bytes from the memory stream.
112
+ return encrypted ;
112
113
}
113
114
}
114
115
}
0 commit comments