44using Org . BouncyCastle . Math ;
55using Org . BouncyCastle . Security ;
66using System . Security . Cryptography ;
7+ using System . IO ;
8+ using System ;
9+ using System . Text ;
710
811public class AES256CBC
912{
1013 private static string TRANSFORMATION = "AES/CBC/PKCS7PADDING" ;
1114 private byte [ ] AES_ENCRYPTION_KEY ;
1215 private byte [ ] ENCRYPTION_IV ;
1316
17+ private byte [ ] MAC_KEY ;
18+ private byte [ ] ENCRYPTION_EPHEM_KEY ;
19+
1420 public AES256CBC ( string privateKeyHex , string ephemPublicKeyHex , string encryptionIvHex )
1521 {
1622 using ( SHA512 shaM = new SHA512Managed ( ) )
@@ -24,35 +30,39 @@ public AES256CBC(string privateKeyHex, string ephemPublicKeyHex, string encrypti
2430 System . Array . Copy ( hash , encKeyBytes , 32 ) ;
2531
2632 AES_ENCRYPTION_KEY = encKeyBytes ;
27- ENCRYPTION_IV = toByteArray ( encryptionIvHex ) ;
2833
34+ MAC_KEY = new byte [ hash . Length - 32 ] ;
35+ System . Array . Copy ( hash , 32 , MAC_KEY , 0 , MAC_KEY . Length ) ;
36+
37+ ENCRYPTION_IV = toByteArray ( encryptionIvHex ) ;
38+ ENCRYPTION_EPHEM_KEY = toByteArray ( ephemPublicKeyHex ) ;
2939 }
3040 }
3141
32- public string encrypt ( byte [ ] src )
42+ public byte [ ] encrypt ( byte [ ] src )
3343 {
3444 var key = ParameterUtilities . CreateKeyParameter ( "AES" , AES_ENCRYPTION_KEY ) ;
3545 var parametersWithIv = new ParametersWithIV ( key , ENCRYPTION_IV ) ;
3646
3747 var cipher = CipherUtilities . GetCipher ( TRANSFORMATION ) ;
3848 cipher . Init ( true , parametersWithIv ) ;
3949
40- return System . Text . Encoding . UTF8 . GetString (
41- cipher . DoFinal ( src )
42- ) ;
50+ return cipher . DoFinal ( src ) ;
4351 }
4452
45- public string decrypt ( byte [ ] src )
53+ public byte [ ] decrypt ( byte [ ] src , string mac )
4654 {
55+ if ( ! hmacSha256Verify ( MAC_KEY , getCombinedData ( src ) , mac ) )
56+ {
57+ throw new SystemException ( "Bad MAC error during decrypt" ) ;
58+ }
4759 var key = ParameterUtilities . CreateKeyParameter ( "AES" , AES_ENCRYPTION_KEY ) ;
4860 var parametersWithIv = new ParametersWithIV ( key , ENCRYPTION_IV ) ;
4961
5062 var cipher = CipherUtilities . GetCipher ( TRANSFORMATION ) ;
5163 cipher . Init ( false , parametersWithIv ) ;
5264
53- return System . Text . Encoding . UTF8 . GetString (
54- cipher . DoFinal ( src )
55- ) ;
65+ return cipher . DoFinal ( src ) ;
5666 }
5767
5868
@@ -97,4 +107,35 @@ public static byte[] toByteArray(BigInteger bi)
97107 }
98108 return b ;
99109 }
110+
111+ public byte [ ] getCombinedData ( byte [ ] cipherTextBytes )
112+ {
113+ using ( MemoryStream outputStream = new MemoryStream ( ) )
114+ {
115+ outputStream . Write ( ENCRYPTION_IV , 0 , ENCRYPTION_IV . Length ) ;
116+ outputStream . Write ( ENCRYPTION_EPHEM_KEY , 0 , ENCRYPTION_EPHEM_KEY . Length ) ;
117+ outputStream . Write ( cipherTextBytes , 0 , cipherTextBytes . Length ) ;
118+ return outputStream . ToArray ( ) ;
119+ }
120+ }
121+
122+ public byte [ ] getMac ( byte [ ] cipherTextBytes )
123+ {
124+ return hmacSha256Sign ( MAC_KEY , getCombinedData ( cipherTextBytes ) ) ;
125+ }
126+
127+ public byte [ ] hmacSha256Sign ( byte [ ] key , byte [ ] data )
128+ {
129+ using ( HMACSHA256 hmac = new HMACSHA256 ( key ) )
130+ {
131+ return hmac . ComputeHash ( data ) ;
132+ }
133+ }
134+
135+ public bool hmacSha256Verify ( byte [ ] key , byte [ ] data , string sig )
136+ {
137+ byte [ ] expectedSig = hmacSha256Sign ( key , data ) ;
138+ string expectedSigHex = BitConverter . ToString ( expectedSig ) . Replace ( "-" , "" ) . ToLower ( ) ;
139+ return expectedSigHex . Equals ( sig ) ;
140+ }
100141}
0 commit comments