Skip to content

Commit e849569

Browse files
authored
Merge pull request #2013 from plvie/master
Add ECB/NoPadding and CBC/NoPadding support to AES encryption
2 parents 74d631c + fa559fd commit e849569

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/core/operations/AESDecrypt.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class AESDecrypt extends Operation {
112112
run(input, args) {
113113
const key = Utils.convertToByteString(args[0].string, args[0].option),
114114
iv = Utils.convertToByteString(args[1].string, args[1].option),
115-
mode = args[2].substring(0, 3),
115+
mode = args[2].split("/")[0],
116116
noPadding = args[2].endsWith("NoPadding"),
117117
inputType = args[3],
118118
outputType = args[4],

src/core/operations/AESEncrypt.mjs

+19-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ class AESEncrypt extends Operation {
6666
{
6767
name: "ECB",
6868
off: [5]
69+
},
70+
{
71+
name: "CBC/NoPadding",
72+
off: [5]
73+
},
74+
{
75+
name: "ECB/NoPadding",
76+
off: [5]
6977
}
7078
]
7179
},
@@ -98,7 +106,8 @@ class AESEncrypt extends Operation {
98106
run(input, args) {
99107
const key = Utils.convertToByteString(args[0].string, args[0].option),
100108
iv = Utils.convertToByteString(args[1].string, args[1].option),
101-
mode = args[2],
109+
mode = args[2].split("/")[0],
110+
noPadding = args[2].endsWith("NoPadding"),
102111
inputType = args[3],
103112
outputType = args[4],
104113
aad = Utils.convertToByteString(args[5].string, args[5].option);
@@ -114,11 +123,20 @@ The following algorithms will be used based on the size of the key:
114123

115124
input = Utils.convertToByteString(input, inputType);
116125

126+
// Handle NoPadding modes
127+
if (noPadding && input.length % 16 !== 0) {
128+
throw new OperationError("Input length must be a multiple of 16 bytes for NoPadding modes.");
129+
}
117130
const cipher = forge.cipher.createCipher("AES-" + mode, key);
118131
cipher.start({
119132
iv: iv,
120133
additionalData: mode === "GCM" ? aad : undefined
121134
});
135+
if (noPadding) {
136+
cipher.mode.pad = function(output, options) {
137+
return true;
138+
};
139+
}
122140
cipher.update(forge.util.createBuffer(input));
123141
cipher.finish();
124142

0 commit comments

Comments
 (0)