11using System ;
22
3+ using Renci . SshNet . Common ;
34using Renci . SshNet . Security . Cryptography . Ciphers ;
5+ using Renci . SshNet . Security . Cryptography . Ciphers . Modes ;
46
57namespace Renci . SshNet . Security . Cryptography
68{
@@ -75,18 +77,29 @@ protected BlockCipher(byte[] key, byte blockSize, CipherMode mode, CipherPadding
7577 /// </returns>
7678 public override byte [ ] Encrypt ( byte [ ] input , int offset , int length )
7779 {
78- if ( length % _blockSize > 0 )
80+ var paddingLength = 0 ;
81+ if ( _padding is not null )
7982 {
80- if ( _padding is null )
81- {
82- throw new ArgumentException ( string . Format ( "The data block size is incorrect for {0}." , GetType ( ) . Name ) , "data" ) ;
83- }
84-
85- var paddingLength = _blockSize - ( length % _blockSize ) ;
83+ paddingLength = _blockSize - ( length % _blockSize ) ;
8684 input = _padding . Pad ( input , offset , length , paddingLength ) ;
8785 length += paddingLength ;
8886 offset = 0 ;
8987 }
88+ else if ( length % _blockSize > 0 )
89+ {
90+ if ( _mode is CfbCipherMode or OfbCipherMode or CtrCipherMode )
91+ {
92+ paddingLength = _blockSize - ( length % _blockSize ) ;
93+ input = input . Take ( offset , length ) ;
94+ length += paddingLength ;
95+ Array . Resize ( ref input , length ) ;
96+ offset = 0 ;
97+ }
98+ else
99+ {
100+ throw new ArgumentException ( string . Format ( "The data block size is incorrect for {0}." , GetType ( ) . Name ) , "data" ) ;
101+ }
102+ }
90103
91104 var output = new byte [ length ] ;
92105 var writtenBytes = 0 ;
@@ -108,6 +121,11 @@ public override byte[] Encrypt(byte[] input, int offset, int length)
108121 throw new InvalidOperationException ( "Encryption error." ) ;
109122 }
110123
124+ if ( _padding is null && paddingLength > 0 )
125+ {
126+ Array . Resize ( ref output , output . Length - paddingLength ) ;
127+ }
128+
111129 return output ;
112130 }
113131
@@ -122,16 +140,21 @@ public override byte[] Encrypt(byte[] input, int offset, int length)
122140 /// </returns>
123141 public override byte [ ] Decrypt ( byte [ ] input , int offset , int length )
124142 {
143+ var paddingLength = 0 ;
125144 if ( length % _blockSize > 0 )
126145 {
127- if ( _padding is null )
146+ if ( _padding is null && _mode is CfbCipherMode or OfbCipherMode or CtrCipherMode )
147+ {
148+ paddingLength = _blockSize - ( length % _blockSize ) ;
149+ input = input . Take ( offset , length ) ;
150+ length += paddingLength ;
151+ Array . Resize ( ref input , length ) ;
152+ offset = 0 ;
153+ }
154+ else
128155 {
129156 throw new ArgumentException ( string . Format ( "The data block size is incorrect for {0}." , GetType ( ) . Name ) , "data" ) ;
130157 }
131-
132- input = _padding . Pad ( _blockSize , input , offset , length ) ;
133- offset = 0 ;
134- length = input . Length ;
135158 }
136159
137160 var output = new byte [ length ] ;
@@ -154,6 +177,16 @@ public override byte[] Decrypt(byte[] input, int offset, int length)
154177 throw new InvalidOperationException ( "Encryption error." ) ;
155178 }
156179
180+ if ( _padding is not null )
181+ {
182+ paddingLength = _padding . PadCount ( output ) ;
183+ }
184+
185+ if ( paddingLength > 0 )
186+ {
187+ Array . Resize ( ref output , output . Length - paddingLength ) ;
188+ }
189+
157190 return output ;
158191 }
159192
0 commit comments