@@ -25,6 +25,8 @@ public sealed class AesCipher : BlockCipher
25
25
private bool useCSP ; // set to false when CSP is not available for a given mode; falls back to legacy code
26
26
private bool isCTRMode ;
27
27
private uint [ ] _ctrIV ;
28
+
29
+ CipherPadding _padding ;
28
30
#endif
29
31
30
32
#region Static Definition Tables
@@ -689,22 +691,24 @@ public override byte[] Encrypt(byte[] data, int offset, int length)
689
691
{
690
692
if ( useCSP )
691
693
{
694
+ if ( length % BlockSize > 0 )
695
+ {
696
+ if ( _padding == null )
697
+ {
698
+ throw new ArgumentException ( "data" ) ;
699
+ }
700
+ data = _padding . Pad ( BlockSize , data , offset , length ) ;
701
+ offset = 0 ;
702
+ length = data . Length ;
703
+ }
704
+
692
705
if ( isCTRMode )
693
706
return CTREncryptDecrypt ( data , offset , length ) ;
694
707
else
695
708
{
696
- if ( length % BlockSize == 0 )
697
- {
698
- byte [ ] output = new byte [ length ] ;
699
- aesEncryptor . TransformBlock ( data , offset , length , output , 0 ) ;
700
- return output ;
701
- }
702
- else
703
- {
704
- // adds padding
705
- byte [ ] output = aesEncryptor . TransformFinalBlock ( data , offset , length ) ;
706
- return output ;
707
- }
709
+ byte [ ] output = new byte [ length ] ;
710
+ aesEncryptor . TransformBlock ( data , offset , length , output , 0 ) ;
711
+ return output ;
708
712
}
709
713
}
710
714
else
@@ -724,34 +728,24 @@ public override byte[] Decrypt(byte[] data, int offset, int length)
724
728
{
725
729
if ( useCSP )
726
730
{
731
+ if ( length % BlockSize > 0 )
732
+ {
733
+ if ( _padding == null )
734
+ {
735
+ throw new ArgumentException ( "data" ) ;
736
+ }
737
+ data = _padding . Pad ( BlockSize , data , offset , length ) ;
738
+ offset = 0 ;
739
+ length = data . Length ;
740
+ }
741
+
727
742
if ( isCTRMode )
728
743
return CTREncryptDecrypt ( data , offset , length ) ;
729
744
else
730
745
{
731
- if ( length % BlockSize == 0 )
732
- {
733
- byte [ ] output = new byte [ length ] ;
734
- aesDecryptor . TransformBlock ( data , offset , length , output , 0 ) ;
735
- return output ;
736
- }
737
- else
738
- {
739
- // handles padding
740
- byte [ ] output = aesDecryptor . TransformFinalBlock ( data , offset , length ) ;
741
- return output ;
742
- }
743
-
744
-
745
- //byte[] ok = base.Decrypt(data, offset, length);
746
- //for (int i = 0; i < a1.Length; i++)
747
- // if (a1[i] != ok[i] || a1.Length != ok.Length)
748
- // return null;
749
-
750
- //for (int i = 0; i < a1.Length; i++)
751
- // if (a2[i] != ok[i] || a1.Length != ok.Length)
752
- // return null;
753
-
754
- //return a1;
746
+ byte [ ] output = new byte [ length ] ;
747
+ aesDecryptor . TransformBlock ( data , offset , length , output , 0 ) ;
748
+ return output ;
755
749
}
756
750
}
757
751
else
@@ -763,7 +757,11 @@ private bool initCryptoServiceProvider(CipherMode mode, CipherPadding padding)
763
757
{
764
758
try
765
759
{
766
- csp . PaddingMode cspPadding = padding == null ? csp . PaddingMode . None : csp . PaddingMode . PKCS7 ; // PKCS5 is same as PKCS7
760
+ // use the provided CipherPadding object
761
+ _padding = padding ;
762
+ csp . PaddingMode cspPadding = csp . PaddingMode . None ;
763
+
764
+ // set the Mode
767
765
csp . CipherMode cspMode = 0 ;
768
766
isCTRMode = mode is Modes . CtrCipherMode ;
769
767
0 commit comments