When I convert the signature value to it returns a more than 64 length hex string i want an exact 64 length hex string
Below is the code that I'm using
public static void GenerateSingature() {
try {
String plaintext = "<PlainTextToBeEncrypted>";
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp128r1");
KeyPairGenerator g = KeyPairGenerator.getInstance("EC");
g.initialize(ecSpec, new SecureRandom());
KeyPair keypair = g.generateKeyPair();
PublicKey publicKey = keypair.getPublic();
PrivateKey privateKey = keypair.getPrivate();
//SHA-256withPLAIN-ECDSA
Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initSign(privateKey);
ecdsaSign.update(plaintext.getBytes("UTF-8"));
byte[] signature = ecdsaSign.sign();
String pub = Base64.getEncoder().encodeToString(publicKey.getEncoded());
//String sig = Base64.getEncoder().encodeToString(signature);
String sig = Hex.toHexString(signature);
//System.out.println(sighex);
//System.out.println(Base64.getEncoder().encodeToString(Hex.decode(sighex)));
System.out.println("----------- Signature --------------------");
System.out.println(sig);
// ------------------- Verify -------------------------------------------
Signature ecdsaVerify = Signature.getInstance("SHA256withECDSA");
KeyFactory kf = KeyFactory.getInstance("EC");
//EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(obj.getString("publicKey")));
//KeyFactory keyFactory = KeyFactory.getInstance("EC");
//PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
ecdsaVerify.initVerify(publicKey);
ecdsaVerify.update("<PlainTextToBeEncrypted>".getBytes("UTF-8"));
boolean result = ecdsaVerify.verify(Hex.decode(sig));
System.out.printf("Result: %b", result);
} catch (Exception e) {
System.out.println(e);
System.out.println("Error......");
}
}
secp128r1
Algorithm Used for elliptic curve 128 Bit encryption
Came across SHA-256withPLAIN-ECDSA
but the bouncy castle is not supporting this in JAVA where the same exists in the C# package
Any input on how we can achieve a constant 64 Char length encryption Using the above-mentioned algorithm.
Note: secp128r1 is not recommended after 2010 since I'm working with a legacy system