📄 keyring.java
字号:
package au.net.aba.pgp;
/*
* $Id: Keyring.java,v 1.8 1998/10/27 05:11:10 leachbj Exp $
* $Author: leachbj $
*
* Copyright (C) 1996-1998 Australian Business Access Pty Ltd.
* All rights reserved.
*
* Use, modification, copying and distribution of this software is subject the
* terms and conditions of the ABA Public Licence. See the file
* "PUBLIC_LICENCE" for additional information.
*
* If you have not received a copy of the Public Licence, you must destroy all
* copies of this file immediately.
*
* $Source: /aba/CVSROOT/jdk1.1/src/au.net.aba/pgp/Keyring.java,v $
* $Revision: 1.8 $
* $Date: 1998/10/27 05:11:10 $
* $State: Exp $
*/
import java.io.*;
import java.math.*;
import java.security.*;
/**
* A PGP keyring. To use this package in conjunction with the
* ABA crypto provider it is necessary to use a PGP 2.3 compatible
* keyring, with no passphrase on the keyring.
* <p>
* In your application:
* <pre>
* import au.net.aba.pgp.Keyring;
*
* Keyring keyRing = new Keyring(keyringdir);
* PublicKey publicKey = keyRing.getPublicKey(keyID);
* </pre>
* <p>
* Where <code>keyringdir</code> is directory containing the pgp
* <code>pubring.pgp</code> and <code>secring.pgp</code> files and
* <code>keyID</code> is the keyname to be retrieved.
*/
public class Keyring
{
public final static String ident = "$Id: Keyring.java,v 1.8 1998/10/27 05:11:10 leachbj Exp $";
private String keyring; // key ring directory
/**
* Construct with user supplied keyring.
*/
public Keyring (String keyring)
{
this.keyring = keyring;
}
/**
* Return the private key with the given key name.
*
* @param key the name of the key we want.
* @return the PrivateKey object representing that key.
* @exception KeyException a problem occured reading the key.
*/
public PrivateKey getPrivateKey(String key)
throws KeyException, IOException
{
SecretKeyCertificatePacket cert;
cert = readSecretKey(key);
return cert.getKey("ABA");
}
/**
* Return a private key generated for a specific provider.
*
* @param key the name of the key we want.
* @param provider the name of the provider we want to use.
* @return the PrivateKey object representing that key.
* @exception KeyException a problem occured reading the key.
*/
public PrivateKey getPrivateKey(
String key,
String provider)
throws KeyException, IOException
{
SecretKeyCertificatePacket cert;
cert = readSecretKey(key);
return cert.getKey(provider);
}
/**
* Return the public key with the given key name.
*
* @param key the name of the key we want.
* @return the PrivateKey object representing that key.
* @exception KeyException a problem occured reading the key.
*/
public PublicKey getPublicKey(String key)
throws KeyException, IOException
{
PublicKeyCertificatePacket cert;
cert = readPublicKey(key);
return cert.getKey("ABA");
}
/**
* Return a public key generated for a specific provider.
*
* @param key the name of the key we want.
* @param provider the name of the provider we want to use.
* @return the PrivateKey object representing that key.
* @exception KeyException a problem occured reading the key.
*/
public PublicKey getPublicKey(
String key,
String provider)
throws KeyException, IOException
{
PublicKeyCertificatePacket cert;
cert = readPublicKey(key);
return cert.getKey(provider);
}
/**
* Display a key in the given keyring as a dotted hexadecimal string
* as per the AsciiEncodedKeySpec.
*/
public static void main (String arg[])
throws IOException
{
if (arg.length < 2)
{
System.out.println("Usage: KeyRing key-ring [-public] [-secret] keyid [..keyid]");
System.exit(1);
}
// create keyring
Keyring ring = new Keyring (arg[0]);
// process argument list
boolean publicKey = true; // true to get a public key
// false to get a secret key
for (int i = 1; i < arg.length; i++) {
// check for public/secret flags
if (arg[i].equals ("-public")) {
publicKey = true;
}
else if (arg[i].equals ("-secret")) {
publicKey = false;
}
else {
// search for desired key
try {
// what sort of key?
if (publicKey) {
// read public key
PublicKeyCertificatePacket
cert = ring.readPublicKey (
arg[i]);
System.out.println (
cert.modulus.toString ()
+ "." +
cert.exponent.toString ());
}
else {
// read secret key
SecretKeyCertificatePacket
cert = ring.readSecretKey (
arg[i]);
if (cert.cipherAlgorithm != 0){
throw new IOException (
"passphrases not supported");
}
System.out.println (
cert.modulus.toString ()
+ "." +
cert.exponent.toString ()
+ "." +
cert.exponent_d.toString ()
+ "." +
cert.factor_p.toString ()
+ "." +
cert.factor_q.toString ());
}
}
catch (EOFException e) {
// out of luck
System.out.println (
arg[i] + ": key not found");
}
}
}
}
/**
* Read a public key certificate.
*/
public PublicKeyCertificatePacket readPublicKey (String key)
throws IOException
{
// open public keyring file
FileInputStream file = new FileInputStream (
new File (keyring,"pubring.pgp"));
BufferedInputStream input = new BufferedInputStream (file);
// read keyring
PublicKeyCertificatePacket certificate = null;
try
{
while (true){
// read PGP packet
Packet packet = Packet.readCipherPacket (input);
// what sort of packet?
if (packet instanceof
PublicKeyCertificatePacket){
// save certificate
certificate =
(PublicKeyCertificatePacket)
packet;
continue;
}
else if (packet instanceof UserIDPacket){
// is this the key we want?
UserIDPacket id = (UserIDPacket)packet;
if (id.user.indexOf (key,0) != -1
&& certificate != null){
file.close();
return (certificate);
}
}
}
}
catch (EOFException e)
{
file.close();
}
throw new EOFException ("no such key -\"" + key + "\"");
}
/**
* Read a private key.
*/
public SecretKeyCertificatePacket readSecretKey (String key)
throws IOException
{
// open private keyring file
FileInputStream file = new FileInputStream (
new File (keyring,"secring.pgp"));
BufferedInputStream input = new BufferedInputStream (file);
// read keyring
SecretKeyCertificatePacket certificate = null;
try
{
while (true){
// read PGP packet
Packet packet = Packet.readCipherPacket (input);
// what sort of packet?
if (packet instanceof
SecretKeyCertificatePacket){
// save certificate
certificate =
(SecretKeyCertificatePacket)
packet;
continue;
}
else if (packet instanceof UserIDPacket){
// is this the key we want?
UserIDPacket id = (UserIDPacket)packet;
if (id.user.indexOf (key,0) != -1
&& certificate != null){
file.close ();
return (certificate);
}
}
}
}
catch (EOFException e)
{
file.close ();
}
throw new EOFException ("no such key -\"" + key + "\"");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -