step 3 working

This commit is contained in:
Cédric Seron
2025-11-12 17:14:14 +01:00
parent 3c0e94cb35
commit 8253ea2d2e
2 changed files with 13 additions and 19 deletions

Binary file not shown.

View File

@@ -9,17 +9,16 @@ import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.KeyFactory;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.security.spec.PSSParameterSpec;
import javax.crypto.spec.MGF1ParameterSpec;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PSSParameterSpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import java.util.Base64;
@@ -129,7 +128,7 @@ public class Main {
displayReceived("Received CT_RSA : " + CT_RSA);
// Decrypt PUBC: format PUBC:<base64(nonce||ciphertext)>
byte[] pubcDecoded = Base64.getDecoder().decode(PUBC.substring(PUBC.indexOf(':')+1));
byte[] pubcDecoded = Base64.getDecoder().decode(PUBC.substring(5));
if (pubcDecoded.length < 12) throw new IOException("PUBC too short");
byte[] pubcNonce = Arrays.copyOfRange(pubcDecoded, 0, 12);
byte[] pubcCt = Arrays.copyOfRange(pubcDecoded, 12, pubcDecoded.length);
@@ -152,18 +151,13 @@ public class Main {
byte[] Mbytes = rsaDec.doFinal(ctRsaDecoded);
// Sign M with RSASSA-PSS (SHA-256, MGF1(SHA-256), saltLen=32)
Signature signer = Signature.getInstance("SHA256withRSA/PSS");
Signature signer = Signature.getInstance("RSASSA-PSS");
PSSParameterSpec pssSpec = new PSSParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), 32, 1);
signer.setParameter(pssSpec);
signer.initSign(rsaKP.getPrivate(), new SecureRandom());
signer.update(Mbytes);
byte[] signature = signer.sign();
// Signature should be 256 bytes for 2048-bit RSA
if (signature.length != 256) {
// pad or handle as needed, but for now ensure we can split into two 128-byte chunks
}
// Split signature into 2 chunks of 128 bytes
byte[] sig1 = Arrays.copyOfRange(signature, 0, 128);
byte[] sig2 = Arrays.copyOfRange(signature, 128, 256);
@@ -173,15 +167,15 @@ public class Main {
rsaEnc.init(Cipher.ENCRYPT_MODE, clientPub);
byte[] encSig1 = rsaEnc.doFinal(sig1);
byte[] encSig2 = rsaEnc.doFinal(sig2);
// Send two messages (labels SIG1 and SIG2)
send(socket, "SIG1:" + Base64.getEncoder().encodeToString(encSig1));
String sig1B64 = Base64.getEncoder().encodeToString(encSig1);
String sig2B64 = Base64.getEncoder().encodeToString(encSig2);
send(socket, "SIG1:" + sig1B64);
displaySent("SIG1 sent");
send(socket, "SIG2:" + Base64.getEncoder().encodeToString(encSig2));
send(socket, "SIG2:" + sig2B64);
displaySent("SIG2 sent");
// Cleanup sensitive key material
Arrays.fill(key, (byte)0);
}
private static byte[] toFixedLen(BigInteger x, int len) {