Skip to content

Commit 618735a

Browse files
committed
change padding to Pkcs7
1 parent 94a3398 commit 618735a

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
lines changed

encryptcontent/decrypt-contents.tpl.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
/* encryptcontent/decrypt-contents.tpl.js */
22

3-
/* Strips the padding character from decrypted content. */
4-
function strip_padding(padded_content, padding_char) {
5-
for (var i = padded_content.length; i > 0; i--) {
6-
if (padded_content[i - 1] !== padding_char) {
7-
return padded_content.slice(0, i);
8-
}
9-
}
10-
return '';
11-
};
12-
133
/* Decrypts the content from the ciphertext bundle. */
144
function decrypt_content(password, iv_b64, ciphertext_b64, padding_char) {
155
var key = CryptoJS.MD5(password),
@@ -22,10 +12,10 @@ function decrypt_content(password, iv_b64, ciphertext_b64, padding_char) {
2212
};
2313
var plaintext = CryptoJS.AES.decrypt(bundle, key, {
2414
iv: iv,
25-
padding: CryptoJS.pad.NoPadding
15+
padding: CryptoJS.pad.Pkcs7
2616
});
2717
try {
28-
return strip_padding(plaintext.toString(CryptoJS.enc.Utf8), padding_char);
18+
return plaintext.toString(CryptoJS.enc.Utf8)
2919
} catch (err) {
3020
// encoding failed; wrong password
3121
return false;

encryptcontent/plugin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
import math
99
from pathlib import Path
1010
from os.path import exists
11-
from Crypto import Random
1211
from jinja2 import Template
1312
from Crypto.Cipher import AES
13+
from Crypto.Random import get_random_bytes
14+
from Crypto.Util.Padding import pad
1415
from bs4 import BeautifulSoup
1516
from mkdocs.plugins import BasePlugin
1617
from mkdocs.config import config_options
@@ -26,7 +27,6 @@
2627
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/core.js','b55ae8027253d4d54c4f1ef3b6254646'],
2728
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/enc-base64.js','f551ce1340a86e5edbfef4a6aefa798f'],
2829
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/cipher-core.js','dfddc0e33faf7a794e0c3c140544490e'],
29-
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/pad-nopadding.js','e288e14e2cd299c3247120114e1178e6'],
3030
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/md5.js','349498f298a6e6e6a85789d637e89109'],
3131
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/aes.js','da81b91b1b57c279c29b3469649d9b86'],
3232
]
@@ -124,12 +124,12 @@ def __encrypt_text_aes__(self, text, password):
124124
""" Encrypts text with AES-256. """
125125
BLOCK_SIZE = 32
126126
PADDING_CHAR = b'^'
127-
iv = Random.new().read(16)
127+
iv = get_random_bytes(16)
128128
# key must be 32 bytes for AES-256, so the password is hashed with md5 first
129129
cipher = AES.new(self.__hash_md5__(password), AES.MODE_CBC, iv)
130130
plaintext = text.encode('utf-8')
131131
# plaintext must be padded to be a multiple of BLOCK_SIZE
132-
plaintext_padded = plaintext + (BLOCK_SIZE - len(plaintext) % BLOCK_SIZE) * PADDING_CHAR
132+
plaintext_padded = pad(plaintext, 16, style='pkcs7')
133133
ciphertext = cipher.encrypt(plaintext_padded)
134134
return (
135135
base64.b64encode(iv),

0 commit comments

Comments
 (0)