@@ -47,12 +47,31 @@ public override byte[] Encrypt(byte[] input, int offset, int length)
47
47
return _encryptor . TransformFinalBlock ( input , offset , length ) ;
48
48
}
49
49
50
+ var paddingLength = 0 ;
51
+ if ( length % BlockSize > 0 )
52
+ {
53
+ if ( _aes . Mode is System . Security . Cryptography . CipherMode . CFB or System . Security . Cryptography . CipherMode . OFB )
54
+ {
55
+ paddingLength = BlockSize - ( length % BlockSize ) ;
56
+ input = input . Take ( offset , length ) ;
57
+ length += paddingLength ;
58
+ Array . Resize ( ref input , length ) ;
59
+ offset = 0 ;
60
+ }
61
+ }
62
+
50
63
// Otherwise, (the most important case) assume this instance is
51
64
// used for one direction of an SSH connection, whereby the
52
65
// encrypted data in all packets are considered a single data
53
66
// stream i.e. we do not want to reset the state between calls to Encrypt.
54
67
var output = new byte [ length ] ;
55
68
_ = _encryptor . TransformBlock ( input , offset , length , output , 0 ) ;
69
+
70
+ if ( paddingLength > 0 )
71
+ {
72
+ Array . Resize ( ref output , output . Length - paddingLength ) ;
73
+ }
74
+
56
75
return output ;
57
76
}
58
77
@@ -65,12 +84,32 @@ public override byte[] Decrypt(byte[] input, int offset, int length)
65
84
return _decryptor . TransformFinalBlock ( input , offset , length ) ;
66
85
}
67
86
87
+ var paddingLength = 0 ;
88
+ if ( length % BlockSize > 0 )
89
+ {
90
+ if ( _aes . Mode is System . Security . Cryptography . CipherMode . CFB or System . Security . Cryptography . CipherMode . OFB )
91
+ {
92
+ paddingLength = BlockSize - ( length % BlockSize ) ;
93
+ var newInput = new byte [ input . Length + paddingLength ] ;
94
+ Buffer . BlockCopy ( input , offset , newInput , 0 , length ) ;
95
+ input = newInput ;
96
+ length = input . Length ;
97
+ offset = 0 ;
98
+ }
99
+ }
100
+
68
101
// Otherwise, (the most important case) assume this instance is
69
102
// used for one direction of an SSH connection, whereby the
70
103
// encrypted data in all packets are considered a single data
71
104
// stream i.e. we do not want to reset the state between calls to Decrypt.
72
105
var output = new byte [ length ] ;
73
106
_ = _decryptor . TransformBlock ( input , offset , length , output , 0 ) ;
107
+
108
+ if ( paddingLength > 0 )
109
+ {
110
+ Array . Resize ( ref output , output . Length - paddingLength ) ;
111
+ }
112
+
74
113
return output ;
75
114
}
76
115
0 commit comments