Skip to content

Commit dcd0d61

Browse files
committed
Java keygen checked in.
1 parent 3937050 commit dcd0d61

File tree

6 files changed

+346
-0
lines changed

6 files changed

+346
-0
lines changed

source/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

source/java-keygen/pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4+
http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>sample</groupId>
7+
<artifactId>sample</artifactId>
8+
<version>0.1.0-SNAPSHOT</version>
9+
<name>sample</name>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<jmh.version>1.17.3</jmh.version>
14+
<javac.target>1.8</javac.target>
15+
<exec.class>sample.sample1</exec.class>
16+
<args></args>
17+
</properties>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<version>3.1</version>
25+
<configuration>
26+
<compilerVersion>${javac.target}</compilerVersion>
27+
<source>${javac.target}</source>
28+
<target>${javac.target}</target>
29+
<compilerArgument>-Xlint</compilerArgument>
30+
</configuration>
31+
</plugin>
32+
<plugin>
33+
<artifactId>maven-clean-plugin</artifactId>
34+
<version>2.5</version>
35+
<configuration>
36+
<filesets>
37+
<fileset>
38+
<directory>.</directory>
39+
<includes>
40+
<include>**/*~</include>
41+
</includes>
42+
</fileset>
43+
</filesets>
44+
</configuration>
45+
</plugin>
46+
<plugin>
47+
<groupId>org.codehaus.mojo</groupId>
48+
<artifactId>exec-maven-plugin</artifactId>
49+
<version>1.5.0</version>
50+
<configuration>
51+
<executable>java</executable>
52+
<commandlineArgs>-classpath %classpath ${exec.class} ${args}</commandlineArgs>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package sample;
2+
3+
import java.io.Writer;
4+
import java.io.FileWriter;
5+
import java.io.OutputStreamWriter;
6+
7+
import java.security.Key;
8+
import java.security.KeyPair;
9+
import java.security.KeyPairGenerator;
10+
11+
import java.util.Base64;
12+
13+
public class sample1
14+
{
15+
static private Base64.Encoder encoder = Base64.getEncoder();
16+
17+
static private void writeBase64(Writer out,Key key)
18+
throws java.io.IOException
19+
{
20+
byte[] buf = key.getEncoded();
21+
out.write(encoder.encodeToString(buf));
22+
out.write("\n");
23+
}
24+
25+
static public void main(String[] args) throws Exception
26+
{
27+
if ( args.length == 0 ) {
28+
System.err.println("usage: java algo keySize [outFile]");
29+
System.exit(1);
30+
}
31+
32+
int index = 0;
33+
String algo = args[index]; index++;
34+
int keySize = Integer.parseInt(args[index]); index++;
35+
String outFile = null;
36+
if ( index < args.length ) outFile = args[index]; index++;
37+
38+
KeyPairGenerator kpg = KeyPairGenerator.getInstance(algo);
39+
40+
/* initialize with keySize: typically 2048 for RSA */
41+
kpg.initialize(keySize);
42+
KeyPair kp = kpg.generateKeyPair();
43+
44+
Writer out = null;
45+
try {
46+
if ( outFile != null ) out = new FileWriter(outFile + ".key");
47+
else out = new OutputStreamWriter(System.out);
48+
49+
System.err.println("Private key format: " +
50+
kp.getPrivate().getFormat());
51+
out.write("-----BEGIN RSA PRIVATE KEY-----\n");
52+
writeBase64(out, kp.getPrivate());
53+
out.write("-----END RSA PRIVATE KEY-----\n");
54+
55+
if ( outFile != null ) {
56+
out.close();
57+
out = new FileWriter(outFile + ".pub");
58+
}
59+
60+
System.err.println("Public key format: " +
61+
kp.getPublic().getFormat());
62+
out.write("-----BEGIN RSA PUBLIC KEY-----\n");
63+
writeBase64(out, kp.getPublic());
64+
out.write("-----END RSA PUBLIC KEY-----\n");
65+
} finally {
66+
if ( out != null ) out.close();
67+
}
68+
}
69+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sample;
2+
3+
import java.io.Writer;
4+
import java.io.FileWriter;
5+
import java.io.OutputStreamWriter;
6+
import java.io.OutputStream;
7+
import java.io.FileOutputStream;
8+
9+
import java.security.Key;
10+
import java.security.KeyPair;
11+
import java.security.KeyPairGenerator;
12+
13+
import java.util.Base64;
14+
15+
public class sample2
16+
{
17+
static private Base64.Encoder encoder = Base64.getEncoder();
18+
19+
static private void writeBinary(OutputStream out,Key key)
20+
throws java.io.IOException
21+
{
22+
out.write(key.getEncoded());
23+
}
24+
25+
static public void main(String[] args) throws Exception
26+
{
27+
if ( args.length != 3 ) {
28+
System.err.println("usage: java algo keySize outFile");
29+
System.exit(1);
30+
}
31+
32+
int index = 0;
33+
String algo = args[index]; index++;
34+
int keySize = Integer.parseInt(args[index]); index++;
35+
String outFile = args[index]; index++;
36+
37+
KeyPairGenerator kpg = KeyPairGenerator.getInstance(algo);
38+
39+
/* initialize with keySize: typically 2048 for RSA */
40+
kpg.initialize(keySize);
41+
KeyPair kp = kpg.generateKeyPair();
42+
43+
OutputStream out = null;
44+
try {
45+
System.err.println("Private key format: " +
46+
kp.getPrivate().getFormat());
47+
out = new FileOutputStream(outFile + ".key");
48+
writeBinary(out, kp.getPrivate());
49+
out.close();
50+
51+
System.err.println("Public key format: " +
52+
kp.getPublic().getFormat());
53+
out = new FileOutputStream(outFile + ".pub");
54+
writeBinary(out, kp.getPublic());
55+
} finally {
56+
if ( out != null ) out.close();
57+
}
58+
}
59+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package sample;
2+
3+
import java.io.OutputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.InputStream;
6+
import java.io.FileInputStream;
7+
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import java.nio.file.Path;
11+
12+
import java.security.Key;
13+
import java.security.PrivateKey;
14+
import java.security.KeyPair;
15+
import java.security.KeyPairGenerator;
16+
import java.security.KeyFactory;
17+
import java.security.Signature;
18+
19+
import java.security.spec.PKCS8EncodedKeySpec;
20+
21+
import java.util.Base64;
22+
23+
public class sample3
24+
{
25+
static private Base64.Encoder encoder = Base64.getEncoder();
26+
27+
static private void writeBinary(OutputStream out,Key key)
28+
throws java.io.IOException
29+
{
30+
out.write(key.getEncoded());
31+
}
32+
33+
static public void main(String[] args) throws Exception
34+
{
35+
if ( args.length != 4 ) {
36+
System.err.println("generate digital signature.");
37+
System.err.println("usage: java algo pvtKeyFile dataFile signFile");
38+
System.exit(1);
39+
}
40+
41+
int index = 0;
42+
String algo = args[index]; index++;
43+
String keyFile = args[index]; index++;
44+
String dataFile = args[index]; index++;
45+
String signFile = args[index]; index++;
46+
47+
/* Read the private key bytes */
48+
Path path = Paths.get(keyFile);
49+
byte[] bytes = Files.readAllBytes(path);
50+
51+
/* Generate private key. */
52+
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(bytes);
53+
KeyFactory kf = KeyFactory.getInstance("RSA");
54+
PrivateKey pvt = kf.generatePrivate(ks);
55+
56+
Signature sign = Signature.getInstance("SHA256withRSA");
57+
sign.initSign(pvt);
58+
59+
InputStream in = null;
60+
try {
61+
in = new FileInputStream(dataFile);
62+
byte[] buf = new byte[2048];
63+
int len;
64+
while ((len = in.read(buf)) != -1) {
65+
sign.update(buf, 0, len);
66+
}
67+
} finally {
68+
if ( in != null ) in.close();
69+
}
70+
71+
OutputStream out = null;
72+
try {
73+
out = new FileOutputStream(signFile);
74+
byte[] signature = sign.sign();
75+
out.write(signature);
76+
} finally {
77+
if ( out != null ) out.close();
78+
}
79+
}
80+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package sample;
2+
3+
import java.io.OutputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.InputStream;
6+
import java.io.FileInputStream;
7+
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import java.nio.file.Path;
11+
12+
import java.security.Key;
13+
import java.security.PrivateKey;
14+
import java.security.KeyPair;
15+
import java.security.KeyPairGenerator;
16+
import java.security.KeyFactory;
17+
import java.security.Signature;
18+
import java.security.PublicKey;
19+
20+
import java.security.spec.PKCS8EncodedKeySpec;
21+
import java.security.spec.X509EncodedKeySpec;
22+
23+
import java.util.Base64;
24+
25+
public class sample4
26+
{
27+
static private Base64.Encoder encoder = Base64.getEncoder();
28+
29+
static private void writeBinary(OutputStream out,Key key)
30+
throws java.io.IOException
31+
{
32+
out.write(key.getEncoded());
33+
}
34+
35+
static public void main(String[] args) throws Exception
36+
{
37+
if ( args.length != 4 ) {
38+
System.err.println("verify digital signature.");
39+
System.err.println("usage: java algo pubKeyFile dataFile signFile");
40+
System.exit(1);
41+
}
42+
43+
int index = 0;
44+
String algo = args[index]; index++;
45+
String keyFile = args[index]; index++;
46+
String dataFile = args[index]; index++;
47+
String signFile = args[index]; index++;
48+
49+
/* Read the public key bytes */
50+
Path path = Paths.get(keyFile);
51+
byte[] bytes = Files.readAllBytes(path);
52+
53+
/* Generate public key. */
54+
X509EncodedKeySpec ks = new X509EncodedKeySpec(bytes);
55+
KeyFactory kf = KeyFactory.getInstance("RSA");
56+
PublicKey pub = kf.generatePublic(ks);
57+
58+
Signature sign = Signature.getInstance("SHA256withRSA");
59+
sign.initVerify(pub);
60+
61+
InputStream in = null;
62+
try {
63+
in = new FileInputStream(dataFile);
64+
byte[] buf = new byte[2048];
65+
int len;
66+
while ((len = in.read(buf)) != -1) {
67+
sign.update(buf, 0, len);
68+
}
69+
} finally {
70+
if ( in != null ) in.close();
71+
}
72+
73+
/* Read the signature bytes */
74+
path = Paths.get(signFile);
75+
bytes = Files.readAllBytes(path);
76+
System.out.println(dataFile + ": Signature " +
77+
(sign.verify(bytes) ? "OK" : "Not OK"));
78+
}
79+
}

0 commit comments

Comments
 (0)