@@ -56,25 +56,38 @@ function decrypt(key, cipherText, ivLength = 13) {
56
56
return "[protected]" ;
57
57
}
58
58
59
- const cipherTextBufferWithIv = Buffer . from ( cipherText , 'base64' ) ;
60
- const iv = cipherTextBufferWithIv . slice ( 0 , ivLength ) ;
59
+ try {
60
+ const cipherTextBufferWithIv = Buffer . from ( cipherText . toString ( ) , 'base64' ) ;
61
+ const iv = cipherTextBufferWithIv . slice ( 0 , ivLength ) ;
61
62
62
- const cipherTextBuffer = cipherTextBufferWithIv . slice ( ivLength ) ;
63
+ const cipherTextBuffer = cipherTextBufferWithIv . slice ( ivLength ) ;
63
64
64
- const decipher = crypto . createDecipheriv ( 'aes-128-cbc' , pad ( key ) , pad ( iv ) ) ;
65
+ const decipher = crypto . createDecipheriv ( 'aes-128-cbc' , pad ( key ) , pad ( iv ) ) ;
65
66
66
- const decryptedBytes = Buffer . concat ( [ decipher . update ( cipherTextBuffer ) , decipher . final ( ) ] ) ;
67
+ const decryptedBytes = Buffer . concat ( [ decipher . update ( cipherTextBuffer ) , decipher . final ( ) ] ) ;
67
68
68
- const digest = decryptedBytes . slice ( 0 , 4 ) ;
69
- const payload = decryptedBytes . slice ( 4 ) ;
69
+ const digest = decryptedBytes . slice ( 0 , 4 ) ;
70
+ const payload = decryptedBytes . slice ( 4 ) ;
70
71
71
- const computedDigest = shaArray ( payload ) . slice ( 0 , 4 ) ;
72
+ const computedDigest = shaArray ( payload ) . slice ( 0 , 4 ) ;
72
73
73
- if ( ! arraysIdentical ( digest , computedDigest ) ) {
74
- return false ;
75
- }
74
+ if ( ! arraysIdentical ( digest , computedDigest ) ) {
75
+ return false ;
76
+ }
76
77
77
- return payload ;
78
+ return payload ;
79
+ }
80
+ catch ( e ) {
81
+ // recovery from https://github.com/zadam/trilium/issues/510
82
+ if ( e . message && e . message . includes ( "WRONG_FINAL_BLOCK_LENGTH" ) ) {
83
+ log . info ( "Caught WRONG_FINAL_BLOCK_LENGTH, returning cipherText instead" ) ;
84
+
85
+ return cipherText ;
86
+ }
87
+ else {
88
+ throw e ;
89
+ }
90
+ }
78
91
}
79
92
80
93
function decryptString ( dataKey , cipherText ) {
0 commit comments