@@ -19,46 +19,40 @@ private static byte[] GetRandomBytes()
1919
2020 private static byte [ ] AES_Encrypt ( byte [ ] bytesToBeEncrypted , byte [ ] passwordBytes )
2121 {
22- byte [ ] encryptedBytes ;
2322 byte [ ] saltBytes = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;
24- using ( var ms = new MemoryStream ( ) )
25- {
26- var key = new Rfc2898DeriveBytes ( passwordBytes , saltBytes , 2000 ) ;
27- using var aes = new RijndaelManaged { KeySize = 256 , BlockSize = 128 , Mode = CipherMode . CBC } ;
28- aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
29- aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
30- using ( var cs = new CryptoStream ( ms , aes . CreateEncryptor ( ) , CryptoStreamMode . Write ) )
31- {
32- cs . Write ( bytesToBeEncrypted , 0 , bytesToBeEncrypted . Length ) ;
33- cs . Close ( ) ;
34- }
35-
36- encryptedBytes = ms . ToArray ( ) ;
37- }
38-
39- return encryptedBytes ;
23+
24+ using var ms = new MemoryStream ( ) ;
25+
26+ var key = new Rfc2898DeriveBytes ( passwordBytes , saltBytes , 2000 ) ;
27+ using var aes = new RijndaelManaged { KeySize = 256 , BlockSize = 128 , Mode = CipherMode . CBC } ;
28+ aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
29+ aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
30+
31+ using var cs = new CryptoStream ( ms , aes . CreateEncryptor ( ) , CryptoStreamMode . Write ) ;
32+
33+ cs . Write ( bytesToBeEncrypted , 0 , bytesToBeEncrypted . Length ) ;
34+ cs . Close ( ) ;
35+
36+ return ms . ToArray ( ) ;
4037 }
4138
4239 private static byte [ ] AES_Decrypt ( byte [ ] bytesToBeDecrypted , byte [ ] passwordBytes )
4340 {
44- byte [ ] decryptedBytes ;
4541 byte [ ] saltBytes = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;
46- using ( var ms = new MemoryStream ( ) )
47- {
48- var key = new Rfc2898DeriveBytes ( passwordBytes , saltBytes , 2000 ) ;
49- using var aes = new RijndaelManaged { KeySize = 256 , BlockSize = 128 , Mode = CipherMode . CBC } ;
50- aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
51- aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
52- using ( var cs = new CryptoStream ( ms , aes . CreateDecryptor ( ) , CryptoStreamMode . Write ) )
53- {
54- cs . Write ( bytesToBeDecrypted , 0 , bytesToBeDecrypted . Length ) ;
55- cs . Close ( ) ;
56- }
57-
58- decryptedBytes = ms . ToArray ( ) ;
59- }
60-
61- return decryptedBytes ;
42+
43+ using var ms = new MemoryStream ( ) ;
44+
45+ var key = new Rfc2898DeriveBytes ( passwordBytes , saltBytes , 2000 ) ;
46+ using var aes = new RijndaelManaged { KeySize = 256 , BlockSize = 128 , Mode = CipherMode . CBC } ;
47+ aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
48+ aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
49+
50+ using var cs = new CryptoStream ( ms , aes . CreateDecryptor ( ) , CryptoStreamMode . Write ) ;
51+
52+ cs . Write ( bytesToBeDecrypted , 0 , bytesToBeDecrypted . Length ) ;
53+ cs . Close ( ) ;
54+
55+ return ms . ToArray ( ) ;
6256 }
6357
6458 public static string EncryptString ( string text , string password )
@@ -71,21 +65,21 @@ public static string EncryptString(string text, string password)
7165 for ( var i = 0 ; i < baSalt . Length ; i ++ ) baEncrypted [ i ] = baSalt [ i ] ;
7266 for ( var i = 0 ; i < baText . Length ; i ++ ) baEncrypted [ i + baSalt . Length ] = baText [ i ] ;
7367 baEncrypted = AES_Encrypt ( baEncrypted , baPwdHash ) ;
74- string result = Convert . ToBase64String ( baEncrypted ) ;
75- return result ;
68+
69+ return Convert . ToBase64String ( baEncrypted ) ;
7670 }
7771
7872 public static string DecryptString ( string text , string password )
7973 {
80- byte [ ] baPwd = Encoding . UTF8 . GetBytes ( password ) ;
81- byte [ ] baPwdHash = SHA256 . Create ( ) . ComputeHash ( baPwd ) ;
82- byte [ ] baText = Convert . FromBase64String ( text ) ;
83- byte [ ] baDecrypted = AES_Decrypt ( baText , baPwdHash ) ;
84- int saltLength = SaltLength ;
85- byte [ ] baResult = new byte [ baDecrypted . Length - saltLength ] ;
74+ var baPwd = Encoding . UTF8 . GetBytes ( password ) ;
75+ var baPwdHash = SHA256 . Create ( ) . ComputeHash ( baPwd ) ;
76+ var baText = Convert . FromBase64String ( text ) ;
77+ var baDecrypted = AES_Decrypt ( baText , baPwdHash ) ;
78+ var saltLength = SaltLength ;
79+ var baResult = new byte [ baDecrypted . Length - saltLength ] ;
8680 for ( int i = 0 ; i < baResult . Length ; i ++ ) baResult [ i ] = baDecrypted [ i + saltLength ] ;
87- string result = Encoding . UTF8 . GetString ( baResult ) ;
88- return result ;
81+
82+ return Encoding . UTF8 . GetString ( baResult ) ;
8983 }
9084 }
9185}
0 commit comments