0

How do we sign a message with a givenPrivateKey in java with elliptic curve(p256)

Basically a java implementation of

let elliptic = new EC('p256')
const sig = elliptic.sign(msgHashHex, privateKey, null)

I dont want to generate new private/public key pair. My privateKey = 'abc'

Also please let me know if there is an online tool to play around with digital signatures.

Thanks a lot in advance.

akash
  • 1,801
  • 7
  • 24
  • 42

1 Answers1

0
PrivateKey privateKey = ; // your  EC p256 private key
byte[] msgHashHex = ; // byte array data

Signature signature = Signature.getInstance("ECDSA"); // or SHA256WithECDSA etc.
signature.initSign(privateKey);
signature.update(msgHashHex);   
byte[] result = signature.sign();
zhh
  • 2,346
  • 1
  • 11
  • 22
  • java.security.NoSuchAlgorithmException: ECDSA Signature not available – akash Jul 30 '18 at 15:07
  • Also how to convert String 'abc' to PrivateKey object – akash Jul 30 '18 at 15:07
  • Your private key is ```abc```? It is not even a valid EC private key. Have a look at https://stackoverflow.com/questions/22963581/reading-elliptic-curve-private-key-from-file-with-bouncycastle – zhh Jul 30 '18 at 15:27
  • Is it not possible to just sign message with a string ? – akash Jul 30 '18 at 16:25
  • You can get ```byte[]``` message using ```String::getBytes```. As for the private key, if you already have the private/public key pair, you can get the encoded key either ```byte[]``` or ```String```. – zhh Jul 30 '18 at 16:39