Skip to content

Instantly share code, notes, and snippets.

@kartikv11
Last active October 22, 2022 13:36
Show Gist options
  • Save kartikv11/ad4c75a5d80c54ecf5b6d9c4519a11d4 to your computer and use it in GitHub Desktop.
Save kartikv11/ad4c75a5d80c54ecf5b6d9c4519a11d4 to your computer and use it in GitHub Desktop.
Signing & Verification of secp256k1 in Java
/*
Create Keys using:
Private Key(2 Step: Create & convert to newer PEM format):
openssl ecparam -name secp256k1 -genkey -out privateKey.pem
openssl pkcs8 -topk8 -inform pem -in privateKey.pem -outform pem -nocrypt -out privateKeyv2.pem
Public Key(using Private Key):
openssl ec -in privateKeyv2.pem -pubout -out publicKeyv2.pem
*/
package com.example.demo.service;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class signAndVerifySecp256K1Util {
private static final String ENCRYPTION_ALGORITHM = "EC";
private static final String HASH_ENCRYPTION_ALGO = "SHA256withECDSA";
public String sign() throws Exception {
PrivateKey privateKey = getPemPrivateKey(
"/Users/kiwi/workspace/training/pem-keys/privateKeyv2.pem",
ENCRYPTION_ALGORITHM
);
String plainText = "Hello";
Signature ecdsaSign = Signature.getInstance(HASH_ENCRYPTION_ALGO);
ecdsaSign.initSign(privateKey);
ecdsaSign.update(plainText.getBytes("UTF-8"));
byte[] signature = ecdsaSign.sign();
String sig = Base64.getEncoder().encodeToString(signature);
return sig;
}
public boolean verify(String signature) throws Exception {
PublicKey publicKey= getPemPubKey(
"/Users/kiwi/workspace/training/pem-keys/publicKeyv2.pem",
ENCRYPTION_ALGORITHM
);
Signature ecdsaVerify = Signature.getInstance(HASH_ENCRYPTION_ALGO);
ecdsaVerify.initVerify(publicKey);
ecdsaVerify.update(signature.getBytes("UTF-8"));
boolean result = ecdsaVerify.verify(
Base64.getMimeDecoder().decode(signature)
);
return result;
}
private PrivateKey getPemPrivateKey(String filename, String algorithm) throws Exception {
String temp = new String(Files.readAllBytes(Paths.get(filename)));
String privKeyPEM = temp.replace("-----BEGIN PRIVATE KEY-----\n","");
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----","");
byte[] decoded = Base64.getMimeDecoder().decode(privKeyPEM.getBytes());
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePrivate(spec);
}
private PublicKey getPemPubKey(String filename, String algorithm) throws Exception {
String temp = new String(Files.readAllBytes(Paths.get(filename)));
String pubKeyPEM = temp.replace("-----BEGIN PUBLIC KEY-----\n","");
pubKeyPEM = pubKeyPEM.replace("-----END PUBLIC KEY-----","");
byte[] decoded = Base64.getMimeDecoder().decode(pubKeyPEM);
X509EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePublic(spec);
}
}
@smartkodian
Copy link

Hello sir

I tried it but it gives me error Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : version mismatch: (supported: 00, parsed:
any idea bro?

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment