Created
February 24, 2020 21:37
-
-
Save psenger/7f74719282ff5bbac2106bcec17a8107 to your computer and use it in GitHub Desktop.
[Bouncy Castle Sign and Verify JWS Example] #Java #JWS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; | |
| import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; | |
| import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; | |
| import org.jose4j.jws.AlgorithmIdentifiers; | |
| import org.jose4j.jws.JsonWebSignature; | |
| import org.jose4j.lang.JoseException; | |
| import java.io.IOException; | |
| import java.security.PrivateKey; | |
| import java.security.PublicKey; | |
| /** | |
| * <p>Example of JWS Sign and Verify</p> | |
| * | |
| * @author psenger | |
| * @version 1.0 | |
| * @see | |
| * @since 2/25/20 | |
| */ | |
| public class ExampleSignAndVerify { | |
| public static void main(String[] args) throws IOException, JoseException { | |
| JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); | |
| PemFile pubPem = PemFile.read("public.pem"); | |
| PemFile privPem = PemFile.read("private.pem"); | |
| PrivateKeyInfo info2 = PrivateKeyInfo.getInstance(privPem.getContent()); | |
| SubjectPublicKeyInfo info3 = SubjectPublicKeyInfo.getInstance(pubPem.getContent()); | |
| PrivateKey privKeyStruct = converter.getPrivateKey(info2); | |
| PublicKey pubKeyStruct = converter.getPublicKey(info3); | |
| JsonWebSignature jws = new JsonWebSignature(); | |
| jws.setKeyIdHeaderValue("c1:43:58:0e:f6:9f:4b:fb:b9:71:c7:af:aa:c6:d5:ba"); | |
| jws.setAlgorithmHeaderValue("RS256"); | |
| jws.setPayload("hello"); | |
| jws.setKey(privKeyStruct); | |
| String payload = jws.getCompactSerialization(); | |
| System.out.println("JWS"); | |
| System.out.println(payload); | |
| JsonWebSignature jws2 = new JsonWebSignature(); | |
| jws2.setKey(pubKeyStruct); | |
| jws2.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256); | |
| jws2.setCompactSerialization(payload); | |
| Boolean res = jws2.verifySignature(); | |
| System.out.println(res); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>com.cngrgroup</groupId> | |
| <artifactId>JWSExample</artifactId> | |
| <version>1.0-SNAPSHOT</version> | |
| <name>JWSExample</name> | |
| <properties> | |
| <maven.compiler.source>1.8</maven.compiler.source> | |
| <maven.compiler.target>1.8</maven.compiler.target> | |
| </properties> | |
| <contributors> | |
| <contributor> | |
| <name>Philip A Senger</name> | |
| <email>philip.a.senger@cngrgroup.com</email> | |
| </contributor> | |
| </contributors> | |
| <dependencies> | |
| <dependency> | |
| <groupId>junit</groupId> | |
| <artifactId>junit</artifactId> | |
| <version>4.12</version> | |
| <scope>test</scope> | |
| </dependency> | |
| <!-- logging --> | |
| <dependency> | |
| <groupId>log4j</groupId> | |
| <artifactId>log4j</artifactId> | |
| <version>1.2.17</version> | |
| </dependency> | |
| <!-- used for HTTP Rest Client --> | |
| <dependency> | |
| <groupId>org.apache.httpcomponents</groupId> | |
| <artifactId>httpclient</artifactId> | |
| <version>4.5.10</version> | |
| </dependency> | |
| <!-- JOSE tools --> | |
| <dependency> | |
| <groupId>org.bitbucket.b_c</groupId> | |
| <artifactId>jose4j</artifactId> | |
| <version>0.7.0</version> | |
| </dependency> | |
| <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-ext-jdk15on --> | |
| <dependency> | |
| <groupId>org.bouncycastle</groupId> | |
| <artifactId>bcprov-ext-jdk15on</artifactId> | |
| <version>1.64</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.bouncycastle</groupId> | |
| <artifactId>bcpkix-jdk15on</artifactId> | |
| <version>1.64</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.bouncycastle</groupId> | |
| <artifactId>bcprov-jdk15on</artifactId> | |
| <version>1.48</version> | |
| </dependency> | |
| </dependencies> | |
| </project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment