Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislattman committed Feb 11, 2025
1 parent 4ed0f9f commit 267f286
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions TLS.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void main(String[] args) throws Exception {
byte[] keyHash = sha256.digest(encodedServerEcdhPublicKey);
Signature rsaSign = Signature.getInstance("RSASSA-PSS");
PSSParameterSpec pssSpec = new PSSParameterSpec("SHA-256",
"MGF1", MGF1ParameterSpec.SHA256, 222, 1);
"MGF1", MGF1ParameterSpec.SHA256, (2048 / 8) - (256 / 8) - 2, 1);
rsaSign.setParameter(pssSpec);
rsaSign.initSign(rsaPrivateKey);
rsaSign.update(keyHash);
Expand Down Expand Up @@ -131,7 +131,8 @@ public static void main(String[] args) throws Exception {
}

/*
* AES-256-GCM is used to encrypt a message. The ciphertext,
* AES-256-GCM is used to encrypt a message. The ciphertext (should be
* 28 bytes long, 12 bytes of data followed by 16 bytes for the tag),
* initialization vector (IV, sometimes called a nonce), and additional
* associated data (AAD) is sent over.
*
Expand Down
8 changes: 5 additions & 3 deletions tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ const aad = Buffer.from("authenticated but unencrypted data");
const iv = crypto.randomBytes(12);
const cipher = crypto.createCipheriv("aes-256-gcm", aesKey, iv);
cipher.setAAD(aad);
let ciphertext = cipher.update(Buffer.from(plaintext));
// ciphertext += cipher.final();
let ciphertext = cipher.update(plaintext);
ciphertext = Buffer.concat([ciphertext, cipher.final()]);
const authTag = cipher.getAuthTag();

const decipher = crypto.createDecipheriv("aes-256-gcm", aesKey, iv);
decipher.setAAD(aad);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(ciphertext);
// decrypted += decipher.final();
decrypted = Buffer.concat([decrypted, decipher.final()]);
const recovered = decrypted.toString();
if (plaintext !== recovered) {
throw new Error("Plaintexts don't match.");
Expand Down

0 comments on commit 267f286

Please sign in to comment.