@@ -35,6 +35,8 @@ public sealed class AesCipher : BlockCipher
35
35
private bool isCTRMode ;
36
36
37
37
private uint [ ] _ctrIV ;
38
+
39
+ CipherPadding _padding ;
38
40
#endif
39
41
40
42
#region Static Definition Tables
@@ -695,22 +697,24 @@ public override byte[] Encrypt(byte[] data, int offset, int length)
695
697
{
696
698
if ( useCSP )
697
699
{
700
+ if ( length % BlockSize > 0 )
701
+ {
702
+ if ( _padding == null )
703
+ {
704
+ throw new ArgumentException ( "data" ) ;
705
+ }
706
+ data = _padding . Pad ( BlockSize , data , offset , length ) ;
707
+ offset = 0 ;
708
+ length = data . Length ;
709
+ }
710
+
698
711
if ( isCTRMode )
699
712
return CTREncryptDecrypt ( data , offset , length ) ;
700
713
else
701
714
{
702
- if ( length % BlockSize == 0 )
703
- {
704
- byte [ ] output = new byte [ length ] ;
705
- aesEncryptor . TransformBlock ( data , offset , length , output , 0 ) ;
706
- return output ;
707
- }
708
- else
709
- {
710
- // adds padding
711
- byte [ ] output = aesEncryptor . TransformFinalBlock ( data , offset , length ) ;
712
- return output ;
713
- }
715
+ byte [ ] output = new byte [ length ] ;
716
+ aesEncryptor . TransformBlock ( data , offset , length , output , 0 ) ;
717
+ return output ;
714
718
}
715
719
}
716
720
else
@@ -730,34 +734,24 @@ public override byte[] Decrypt(byte[] data, int offset, int length)
730
734
{
731
735
if ( useCSP )
732
736
{
737
+ if ( length % BlockSize > 0 )
738
+ {
739
+ if ( _padding == null )
740
+ {
741
+ throw new ArgumentException ( "data" ) ;
742
+ }
743
+ data = _padding . Pad ( BlockSize , data , offset , length ) ;
744
+ offset = 0 ;
745
+ length = data . Length ;
746
+ }
747
+
733
748
if ( isCTRMode )
734
749
return CTREncryptDecrypt ( data , offset , length ) ;
735
750
else
736
751
{
737
- if ( length % BlockSize == 0 )
738
- {
739
- byte [ ] output = new byte [ length ] ;
740
- aesDecryptor . TransformBlock ( data , offset , length , output , 0 ) ;
741
- return output ;
742
- }
743
- else
744
- {
745
- // handles padding
746
- byte [ ] output = aesDecryptor . TransformFinalBlock ( data , offset , length ) ;
747
- return output ;
748
- }
749
-
750
-
751
- //byte[] ok = base.Decrypt(data, offset, length);
752
- //for (int i = 0; i < a1.Length; i++)
753
- // if (a1[i] != ok[i] || a1.Length != ok.Length)
754
- // return null;
755
-
756
- //for (int i = 0; i < a1.Length; i++)
757
- // if (a2[i] != ok[i] || a1.Length != ok.Length)
758
- // return null;
759
-
760
- //return a1;
752
+ byte [ ] output = new byte [ length ] ;
753
+ aesDecryptor . TransformBlock ( data , offset , length , output , 0 ) ;
754
+ return output ;
761
755
}
762
756
}
763
757
else
@@ -769,7 +763,11 @@ private bool initCryptoServiceProvider(CipherMode mode, CipherPadding padding)
769
763
{
770
764
try
771
765
{
772
- csp . PaddingMode cspPadding = padding == null ? csp . PaddingMode . None : csp . PaddingMode . PKCS7 ; // PKCS5 is same as PKCS7
766
+ // use the provided CipherPadding object
767
+ _padding = padding ;
768
+ csp . PaddingMode cspPadding = csp . PaddingMode . None ;
769
+
770
+ // set the Mode
773
771
csp . CipherMode cspMode = 0 ;
774
772
isCTRMode = mode is Modes . CtrCipherMode ;
775
773
0 commit comments