About signature authentication with java 1st

Signature and Authentication Example (Java)

    1. Required jar files: commons-codec-1.11.jar, bcprov-ext-jdk15on-158.jar
  1. Sample source for signature

Data to be signed (character string (42 bytes) that combines "transaction date and time" (14 bytes), "terminal identification number" (13 bytes), "terminal processing serial number" (15 bytes)) String message = "20171201091011A004012345678012345678901234";

Reading the private key file Security.addProvider(new BouncyCastleProvider());

Generate a private key target PKCS8EncodedKeySpec pkcs8 = new PKCS8EncodedKeySpec(privateKeyByte);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8);

Sign with the data to be signed and the private key target Signature signer = Signature.getInstance("SHA256withRSA");
signer.initSign(rsaPrivateKey);
signer.update(message.getBytes("UTF-8"));
byte[] signByte = signer.sign();

Convert to hexadecimal format based on the generated signature bytes Hex.encodeHexString(signByte);

7154172627db05f2eb6c8d0f522bd6d1ae0551899944e5a06170abd40e86d49a9204d29e1b251898fc47c633236168aafccf07d6458179f2cceb606dc4c84a3f7f9767fc4b00d3c43feabbc11ef750ca6d85bd7084e74b6c9bdd5bc2f497dae392b9f833a3e52133df74213770da74d9e2a9b08cbe2cf50d5bf1fbcdc00f4bab59ea885aeaaeb8e7a400491ba87c6121c4273a9daf723b89df0e6c4d62823a97db89e6eadea6800a0b692d6d4c19a343b0d762d4dcfd63ce7f41b291619ce2fda299533268d795188cb0cdfbd1a3f1e13f5399cfd52087396cf1a20361629495507569da9472602c985f3d90d35e4838cb9228a52ed211635faa55c94ba0dfbf

    1. Sample source for authentication

Read public key file PemReader pemPubReader = new PemReader(new FileReader("./file/public-key.pem"));
PemObject objPub = pemPubReader.readPemObject();
byte[] publicKeyByte = objPub.getContent();

Generate a public key target X509EncodedKeySpec x509 = new X509EncodedKeySpec(encodedByte);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(x509);

Authenticate with public key target Signature verifier = Signature.getInstance("SHA256withRSA");
verifier.initVerify(rsaPublicKey);
verifier.update(message.getBytes("UTF-8"));
byte [] messageByte = Hex.decodeHex (Signed message above (hexadecimal format)); boolean result = verifier.verify(messageByte);

Recommended Posts

About signature authentication with java 1st
Basic Authentication with Java 11 HttpClient
Learn about transaction savepoints (with Java)
About Java interface
[Java] About Java 12 features
[Java] About arrays
Something about java
Where about java
About Java features
About Java threads
[Java] About interface
About Java class
About Java arrays
About java inheritance
About interface, java interface
About List [Java]
About java var
About Java literals
About Java commands
I tried to make Basic authentication with Java
About Java log output
About Java functional interface
Install java with Homebrew
Java, about 2D arrays
About class division (Java)
About Spring Security authentication
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
Change seats with java
Install Java with Ansible
[Java] About Singleton Class
About Java method binding
Comfortable download with JAVA
[Java] About anonymous classes
[Java Silver] About initialization
Switch java with direnv
About Java Array List
About Java Polymorphism super ()
About inheritance (Java Silver)
About Java String class
About Java access modifiers
Download Java with Ansible
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
Let's scrape with Java! !!
[Java] About enum type
A story about developing ROS called rosjava with java
All about Java programming
Build Java with Wercker
About java abstract class
Endian conversion with JAVA
About the behavior when doing a file map with java
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java
Java multi-project creation with Gradle
Getting Started with Java Collection
What I researched about Java 8
About an instance of java
Java Config with Spring MVC