1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Web ;
5
+ using System . Security . Cryptography ;
6
+ using System . Text ;
7
+ using System . IO ;
8
+
9
+ namespace Encrypt_JavaScript_Decrypt_Csharp
10
+ {
11
+ public class AESEncryption
12
+ {
13
+ public static string DecryptStringAES ( string cipherText )
14
+ {
15
+
16
+ var keybytes = Encoding . UTF8 . GetBytes ( "8080808080808080" ) ;
17
+ var iv = Encoding . UTF8 . GetBytes ( "8080808080808080" ) ;
18
+
19
+ var encrypted = Convert . FromBase64String ( cipherText ) ;
20
+ var decriptedFromJavascript = DecryptStringFromBytes ( encrypted , keybytes , iv ) ;
21
+ return string . Format ( decriptedFromJavascript ) ;
22
+ }
23
+
24
+ private static string DecryptStringFromBytes ( byte [ ] cipherText , byte [ ] key , byte [ ] iv )
25
+ {
26
+ // Check arguments.
27
+ if ( cipherText == null || cipherText . Length <= 0 )
28
+ {
29
+ throw new ArgumentNullException ( "cipherText" ) ;
30
+ }
31
+ if ( key == null || key . Length <= 0 )
32
+ {
33
+ throw new ArgumentNullException ( "key" ) ;
34
+ }
35
+ if ( iv == null || iv . Length <= 0 )
36
+ {
37
+ throw new ArgumentNullException ( "key" ) ;
38
+ }
39
+
40
+ // Declare the string used to hold
41
+ // the decrypted text.
42
+ string plaintext = null ;
43
+
44
+ // Create an RijndaelManaged object
45
+ // with the specified key and IV.
46
+ using ( var rijAlg = new RijndaelManaged ( ) )
47
+ {
48
+ //Settings
49
+ rijAlg . Mode = CipherMode . CBC ;
50
+ rijAlg . Padding = PaddingMode . PKCS7 ;
51
+ rijAlg . FeedbackSize = 128 ;
52
+
53
+ rijAlg . Key = key ;
54
+ rijAlg . IV = iv ;
55
+
56
+ // Create a decrytor to perform the stream transform.
57
+ var decryptor = rijAlg . CreateDecryptor ( rijAlg . Key , rijAlg . IV ) ;
58
+ try
59
+ {
60
+ // Create the streams used for decryption.
61
+ using ( var msDecrypt = new MemoryStream ( cipherText ) )
62
+ {
63
+ using ( var csDecrypt = new CryptoStream ( msDecrypt , decryptor , CryptoStreamMode . Read ) )
64
+ {
65
+
66
+ using ( var srDecrypt = new StreamReader ( csDecrypt ) )
67
+ {
68
+ // Read the decrypted bytes from the decrypting stream
69
+ // and place them in a string.
70
+ plaintext = srDecrypt . ReadToEnd ( ) ;
71
+
72
+ }
73
+
74
+ }
75
+ }
76
+ }
77
+ catch
78
+ {
79
+ plaintext = "keyError" ;
80
+ }
81
+ }
82
+
83
+ return plaintext ;
84
+ }
85
+
86
+ private static byte [ ] EncryptStringToBytes ( string plainText , byte [ ] key , byte [ ] iv )
87
+ {
88
+ // Check arguments.
89
+ if ( plainText == null || plainText . Length <= 0 )
90
+ {
91
+ throw new ArgumentNullException ( "plainText" ) ;
92
+ }
93
+ if ( key == null || key . Length <= 0 )
94
+ {
95
+ throw new ArgumentNullException ( "key" ) ;
96
+ }
97
+ if ( iv == null || iv . Length <= 0 )
98
+ {
99
+ throw new ArgumentNullException ( "key" ) ;
100
+ }
101
+ byte [ ] encrypted ;
102
+ // Create a RijndaelManaged object
103
+ // with the specified key and IV.
104
+ using ( var rijAlg = new RijndaelManaged ( ) )
105
+ {
106
+ rijAlg . Mode = CipherMode . CBC ;
107
+ rijAlg . Padding = PaddingMode . PKCS7 ;
108
+ rijAlg . FeedbackSize = 128 ;
109
+
110
+ rijAlg . Key = key ;
111
+ rijAlg . IV = iv ;
112
+
113
+ // Create a decrytor to perform the stream transform.
114
+ var encryptor = rijAlg . CreateEncryptor ( rijAlg . Key , rijAlg . IV ) ;
115
+
116
+ // Create the streams used for encryption.
117
+ using ( var msEncrypt = new MemoryStream ( ) )
118
+ {
119
+ using ( var csEncrypt = new CryptoStream ( msEncrypt , encryptor , CryptoStreamMode . Write ) )
120
+ {
121
+ using ( var swEncrypt = new StreamWriter ( csEncrypt ) )
122
+ {
123
+ //Write all data to the stream.
124
+ swEncrypt . Write ( plainText ) ;
125
+ }
126
+ encrypted = msEncrypt . ToArray ( ) ;
127
+ }
128
+ }
129
+ }
130
+
131
+ // Return the encrypted bytes from the memory stream.
132
+ return encrypted ;
133
+ }
134
+ }
135
+ }
0 commit comments