@@ -74,6 +74,42 @@ const decryptError = {
74
74
} , encryptedBuffer ) ;
75
75
assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
76
76
77
+ // Now with explicit RSA_PKCS1_PADDING.
78
+ encryptedBuffer = crypto . privateEncrypt ( {
79
+ padding : crypto . constants . RSA_PKCS1_PADDING ,
80
+ key : rsaKeyPemEncrypted ,
81
+ passphrase : Buffer . from ( 'password' )
82
+ } , bufferToEncrypt ) ;
83
+
84
+ decryptedBufferWithPassword = crypto . publicDecrypt ( {
85
+ padding : crypto . constants . RSA_PKCS1_PADDING ,
86
+ key : rsaKeyPemEncrypted ,
87
+ passphrase : Buffer . from ( 'password' )
88
+ } , encryptedBuffer ) ;
89
+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
90
+
91
+ // Omitting padding should be okay because RSA_PKCS1_PADDING is the default.
92
+ decryptedBufferWithPassword = crypto . publicDecrypt ( {
93
+ key : rsaKeyPemEncrypted ,
94
+ passphrase : Buffer . from ( 'password' )
95
+ } , encryptedBuffer ) ;
96
+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , input ) ;
97
+
98
+ // Now with RSA_NO_PADDING. Plaintext needs to match key size.
99
+ const plaintext = 'x' . repeat ( 128 ) ;
100
+ encryptedBuffer = crypto . privateEncrypt ( {
101
+ padding : crypto . constants . RSA_NO_PADDING ,
102
+ key : rsaKeyPemEncrypted ,
103
+ passphrase : Buffer . from ( 'password' )
104
+ } , Buffer . from ( plaintext ) ) ;
105
+
106
+ decryptedBufferWithPassword = crypto . publicDecrypt ( {
107
+ padding : crypto . constants . RSA_NO_PADDING ,
108
+ key : rsaKeyPemEncrypted ,
109
+ passphrase : Buffer . from ( 'password' )
110
+ } , encryptedBuffer ) ;
111
+ assert . strictEqual ( decryptedBufferWithPassword . toString ( ) , plaintext ) ;
112
+
77
113
encryptedBuffer = crypto . publicEncrypt ( certPem , bufferToEncrypt ) ;
78
114
79
115
decryptedBuffer = crypto . privateDecrypt ( keyPem , encryptedBuffer ) ;
0 commit comments