@@ -25,6 +25,8 @@ public sealed class AesCipher : BlockCipher
2525 private bool useCSP ; // set to false when CSP is not available for a given mode; falls back to legacy code
2626 private bool isCTRMode ;
2727 private uint [ ] _ctrIV ;
28+
29+ CipherPadding _padding ;
2830#endif
2931
3032 #region Static Definition Tables
@@ -689,22 +691,24 @@ public override byte[] Encrypt(byte[] data, int offset, int length)
689691 {
690692 if ( useCSP )
691693 {
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+
692705 if ( isCTRMode )
693706 return CTREncryptDecrypt ( data , offset , length ) ;
694707 else
695708 {
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 ;
708712 }
709713 }
710714 else
@@ -724,34 +728,24 @@ public override byte[] Decrypt(byte[] data, int offset, int length)
724728 {
725729 if ( useCSP )
726730 {
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+
727742 if ( isCTRMode )
728743 return CTREncryptDecrypt ( data , offset , length ) ;
729744 else
730745 {
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 ;
755749 }
756750 }
757751 else
@@ -763,7 +757,11 @@ private bool initCryptoServiceProvider(CipherMode mode, CipherPadding padding)
763757 {
764758 try
765759 {
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
767765 csp . CipherMode cspMode = 0 ;
768766 isCTRMode = mode is Modes . CtrCipherMode ;
769767
0 commit comments