On Sat, Jan 31, 2026 at 9:53 AM Nick Couchman <[email protected]> wrote:
>
> On Fri, Jan 30, 2026 at 10:11 PM Tenay Lowden <[email protected]> wrote:
> >
> > Any methods to verify if it's PKCS7 vs PKCS5? Like how they did it here:
> > https://stackoverflow.com/questions/67891550/detect-if-a-text-encrypted-using-aes-cbc-is-padded-or-not
> >
>
> Thanks, Tenay,
> That particular post seems to be indicating what I mentioned before,
> that Java's PKCS5 padding is actually PKCS7 padding - it was just
> implemented when it was PKCS5 and never changed to be PKCS7. I suppose
> I could change the Java code to decrypt using NoPadding and see what
> happens.
>
> I have verified that the encrypted data is actually padded - the
> length of the data before running it through the JS encrypt function
> is 503 bytes, the length of the encrypted data is 512 bytes. So, that
> part seems to be working as expected, and I would guess it is properly
> padding as PKCS7 since that's what JavaScript implements. That also
> makes me think that the padding exception may either be due to
> something else - mismatched encryption key or some issue encoding the
> data from encrypted to Base64. Even if Java did PKCS5 instead of
> PKCS7, 503 bytes padded to 512 is still a multiple of 64-bit block
> size, so I'd think that would still be valid? Anyway, I'll keep
> hacking away at it and figure out what I'm doing wrong...
>

Okay, I figured it out. Turns out that the proper way to take the
hexadecimal key value and turn it into a buffer is not to use
TextEncoder.encode(), but to use Uint8Array.fromHex(). Also, combining
the signature and data into a single array needed to be done a bit
more carefully. Final code looks like this:

==
// The JSON string containing the connection information.
let json='{...}'

// Encode JSON
let encoder = new TextEncoder();
let encoded = encoder.encode(json);

// Turn encryption key into an array
let key = '21b26b643ac580ff181ded6639f21e6f';
let keyData = Uint8Array.fromHex(key);

// Import the key for HMAC signature
let keyObj1 = await
window.crypto.subtle.importKey("raw",keyData,{name:"HMAC",
hash:"SHA-256"},false,["sign"]);

// Get the signature for the encoded JSON data, then convert to an array.
let signature = await window.crypto.subtle.sign("HMAC", keyObj1, encoded);
let signatureArray = new Uint8Array(signature);

// New array that combines signature + data
let signedArray = new Uint8Array(encoded.length + signatureArray.length);
signedArray.set(signatureArray);
signedArray.set(encoded, signatureArray.length);

// Null IV
let zeroiv = new Uint8Array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]);

// Import the key for the encryption
let keyObj2 = await
window.crypto.subtle.importKey("raw",keyData,{name:
"AES-CBC"},false,["encrypt"]);

// Encrypt the data
let encrypted = await crypto.subtle.encrypt({name:"AES-CBC", iv:
zeroiv}, keyObj2, signedArray);

// Encode the data as base64
var base64String = btoa(String.fromCharCode.apply(null, new
Uint8Array(encrypted)));
==

base64String can then be fed to curl or otherwise posted to the
api/tokens endpoint.

-Nick

Reply via email to