Skip to content

Instantly share code, notes, and snippets.

@shu22203
Last active May 10, 2018 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shu22203/5b2cc0421899670cc4b048229195e814 to your computer and use it in GitHub Desktop.
Save shu22203/5b2cc0421899670cc4b048229195e814 to your computer and use it in GitHub Desktop.
[SigTest.java]
import client.Client;
import server.Server;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
public class SigTest {
public static void main(String[] args) throws Exception {
String text = "This is plain text!";
KeyPair kp = Client.ReadKey();
PrivateKey privateKey = kp.getPrivate();
PublicKey publicKey = kp.getPublic();
byte[] signature = Client.Sign(text, privateKey);
Server.Verify(text, signature, publicKey);
}
}
[Client.java]
package client;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.xml.bind.DatatypeConverter;
import java.security.Security;
import java.security.Signature;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.SignatureException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class Client {
public static byte[] Sign(String text, PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, SignatureException {
Signature dsa = Signature.getInstance("SHA256withECDSA");
dsa.initSign(privateKey);
dsa.update(text.getBytes("UTF-8"));
byte[] signature = dsa.sign();
System.out.println("Signature: " + DatatypeConverter.printHexBinary(signature));
return signature;
}
public static KeyPair ReadKey() throws IOException {
String filename = "./key/secret.key";
// String filename = "./key/private_prime256v1.key";
PEMParser pemParser = new PEMParser(new FileReader(new File(filename)));
PEMKeyPair params = (PEMKeyPair) pemParser.readObject();
KeyPair kp = new JcaPEMKeyConverter().getKeyPair(params);
return kp;
}
}
[Server.java]
package server;
import java.io.UnsupportedEncodingException;
import java.security.Signature;
import java.security.PublicKey;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.SignatureException;
public class Server {
public static void Verify(String text, byte[] signature, PublicKey publicKey) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, SignatureException {
Signature dsa = Signature.getInstance("SHA256withECDSA");
dsa.initVerify(publicKey);
dsa.update(text.getBytes("UTF-8"));
boolean verifyResult = dsa.verify(signature);
System.out.println("Verify: " + verifyResult);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment