-
Notifications
You must be signed in to change notification settings - Fork 1
/
genkeys.java
74 lines (64 loc) · 2.14 KB
/
genkeys.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// from http://www.cs.ru.nl/~martijno/
// Martijn Oostdijk. They appear to be samples from a course.
// This functionality is built into linktool. The first time it is
// deployed keys are put in the sakai configuration directory.
// This program was used during development. It should still work,
// and create keys equivalent to those created by the tool.
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
/**
* Generate an RSA public/private keypair.
*
* @version $Revision: 1.1 $
*/
public class genkeys
{
/**
* Generates an RSA public/private key pair.
*/
public genkeys() {
try {
/* Generate keypair. */
System.out.println("Generating keys...");
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(1024);
KeyPair keypair = generator.generateKeyPair();
RSAPublicKey publickey = (RSAPublicKey)keypair.getPublic();
RSAPrivateKey privatekey = (RSAPrivateKey)keypair.getPrivate();
/* Write public key to file. */
writeKey(publickey, "publickey");
/* Write private key to file. */
writeKey(privatekey, "privatekey");
System.out.println("modulus = " + publickey.getModulus());
System.out.println("pubexpint = " + publickey.getPublicExponent());
System.out.println("privexpint = " + privatekey.getPrivateExponent());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Writes <code>key</code> to file with name <code>filename</code> in
* standard encoding (X.509 for RSA public key, PKCS#8 for RSA private key).
*
* @param key the key to write.
* @param filename the name of the file.
*
* @throws IOException if something goes wrong.
*/
public static void writeKey(Key key, String filename) throws IOException {
FileOutputStream file = new FileOutputStream(filename);
file.write(key.getEncoded());
file.close();
}
/**
* The main method just calls the constructor.
*
* @param arg The command line arguments.
*/
public static void main(String[] arg) {
new genkeys();
}
}