org.ssonet.net
Class CryptoTools

java.lang.Object
  |
  +--org.ssonet.net.CryptoTools

public class CryptoTools
extends java.lang.Object

This class is a collection of helpfull Methods for handling cryptographic operations


Field Summary
static boolean debug
           
 
Constructor Summary
CryptoTools()
           
 
Method Summary
static java.security.cert.X509Certificate createCertificate(java.security.PublicKey publicKey, java.security.PrivateKey signKey, java.security.cert.X509Certificate issuerCertificate, java.util.Date notBefore, java.util.Date notAfter, java.lang.String subject)
          Create a X509Certificate for a public key (only DSA-Certificates are allowed)
static java.security.KeyPair createKeyPair(java.lang.String mechanismName, int keyLength)
          Create a keypair
static java.security.cert.X509Certificate createSelfSignedCertificate(java.security.PublicKey publicKey, java.security.PrivateKey signKey, java.util.Date notBefore, java.util.Date notAfter, java.lang.String subject)
          Create a selfsigned X509Certificate for a public key (only DSA-Certificates are allowed)
static java.lang.String getPrincipalElement(java.security.Principal p, java.lang.String commonSymbol)
          For X509Certificates: Get an element of the Name from IssuerDN or SubjectDN
static java.security.SecureRandom getSecureRandom()
          Initializes the secureRandom object.
static java.security.cert.X509Certificate readCertificate(java.io.InputStream is)
          Reads a certificate from an InputStream
static java.security.cert.X509Certificate readCertificate(java.lang.String certificateFileName)
          reads a certificate from file
static java.security.PrivateKey readPrivateKeyPKCS8(java.io.InputStream is, java.lang.String algName)
          Reads a PKCS8-encoded private key from stream.
static java.security.PrivateKey readPrivateKeyPKCS8(java.lang.String privateKeyFileName, java.lang.String algName)
          reads a DER encoded private key from file
static java.security.PublicKey readPublicKeyX509(java.lang.String publicKeyFileName, java.lang.String algName)
          reads a DER encoded public key from file
static void testCertificate(java.security.cert.X509Certificate toTest, java.security.PublicKey publicKey)
          checks, whether a certificate is valid.
static void writeCertificate(java.security.cert.X509Certificate certificate, java.lang.String fileName)
          Writes a X509 certificate to a file
static void writePrivateKeyPKCS8(java.security.PrivateKey privateKey, java.lang.String fileName)
          Writes a Private Key to a file in PKCS8 encoding.
static void writePublicKeyX509(java.security.PublicKey publicKey, java.lang.String fileName)
          Writes a Public Key to a file in X509 encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debug

public static boolean debug
Constructor Detail

CryptoTools

public CryptoTools()
Method Detail

getSecureRandom

public static java.security.SecureRandom getSecureRandom()
Initializes the secureRandom object. If the System property "preload=testing" is set, securerandom is initialized with a java.Random. This can be used to circumvent the waiting for random buffer generation.


createKeyPair

public static java.security.KeyPair createKeyPair(java.lang.String mechanismName,
                                                  int keyLength)
Create a keypair


createCertificate

public static java.security.cert.X509Certificate createCertificate(java.security.PublicKey publicKey,
                                                                   java.security.PrivateKey signKey,
                                                                   java.security.cert.X509Certificate issuerCertificate,
                                                                   java.util.Date notBefore,
                                                                   java.util.Date notAfter,
                                                                   java.lang.String subject)
                                                            throws java.lang.Exception
Create a X509Certificate for a public key (only DSA-Certificates are allowed)

Parameters:
publicKey - the public key to be signed
signKey - the private key used for signing
issuerCertificate - the certificate of the issuing public key
notBefore - the date, at which the certificate becomes valid
notAfter - the date, after which the certificate becomes invalid
subject - Distinguished Name of the subject of this certificate
java.lang.Exception

createSelfSignedCertificate

public static java.security.cert.X509Certificate createSelfSignedCertificate(java.security.PublicKey publicKey,
                                                                             java.security.PrivateKey signKey,
                                                                             java.util.Date notBefore,
                                                                             java.util.Date notAfter,
                                                                             java.lang.String subject)
                                                                      throws java.lang.Exception
Create a selfsigned X509Certificate for a public key (only DSA-Certificates are allowed)

Parameters:
publicKey - the public key to be signed
signKey - the private key used for signing
notBefore - the date, at which the certificate becomes valid
notAfter - the date, after which the certificate becomes invalid
subject - Distinguished Name of the subject of this certificate
java.lang.Exception

testCertificate

public static void testCertificate(java.security.cert.X509Certificate toTest,
                                   java.security.PublicKey publicKey)
                            throws java.security.cert.CertificateException,
                                   java.security.NoSuchAlgorithmException,
                                   java.security.InvalidKeyException,
                                   java.security.SignatureException
checks, whether a certificate is valid. If the certificate is valid, the method just returns. Otherwise an exception according to the cause of invalidity of the certificate is thrown.

Parameters:
toTest - certificate to test
publicKey - certificate issuers public key
Throws:
CertificateException, - if the certificate is not valid
java.security.cert.CertificateException
java.security.NoSuchAlgorithmException
java.security.InvalidKeyException
java.security.SignatureException

readCertificate

public static java.security.cert.X509Certificate readCertificate(java.lang.String certificateFileName)
reads a certificate from file

Parameters:
certificateFileName - certificate file name
Returns:
X509Certificate read certificate

readCertificate

public static java.security.cert.X509Certificate readCertificate(java.io.InputStream is)
Reads a certificate from an InputStream

Parameters:
is - the InputStream
Returns:
X509Certificate read certificate, or null, if an error occured

writeCertificate

public static void writeCertificate(java.security.cert.X509Certificate certificate,
                                    java.lang.String fileName)
                             throws java.lang.Exception
Writes a X509 certificate to a file

Parameters:
certificate - the certificate
fileName - the filename of the file the certificate shall be written to
Throws:
java.lang.Exception - if an error occures while writing the certificate

readPrivateKeyPKCS8

public static java.security.PrivateKey readPrivateKeyPKCS8(java.lang.String privateKeyFileName,
                                                           java.lang.String algName)
reads a DER encoded private key from file

Parameters:
privateKeyFileName - private key file name
Returns:
PrivateKey read private key

readPrivateKeyPKCS8

public static java.security.PrivateKey readPrivateKeyPKCS8(java.io.InputStream is,
                                                           java.lang.String algName)
Reads a PKCS8-encoded private key from stream. Thereby, the stream is read until EOF occures.

Parameters:
is - the InputStream to read from
algName - the name of the algorithm, for which the key is used
Returns:
the private key read, or null, if an error occured

writePrivateKeyPKCS8

public static void writePrivateKeyPKCS8(java.security.PrivateKey privateKey,
                                        java.lang.String fileName)
                                 throws java.lang.Exception
Writes a Private Key to a file in PKCS8 encoding.

Parameters:
privateKey - the private key
fileName - the filename of the file the key shall be written to
Throws:
java.lang.Exception - if the encoding is not PKCS8, and if an error occures during writing

readPublicKeyX509

public static java.security.PublicKey readPublicKeyX509(java.lang.String publicKeyFileName,
                                                        java.lang.String algName)
reads a DER encoded public key from file

Parameters:
publicKeyFileName - public key file name
Returns:
PublicKey read public key

writePublicKeyX509

public static void writePublicKeyX509(java.security.PublicKey publicKey,
                                      java.lang.String fileName)
                               throws java.lang.Exception
Writes a Public Key to a file in X509 encoding.

Parameters:
publicKey - the public key
fileName - the filename of the file the key shall be written to
Throws:
java.lang.Exception - if the encoding is not X509, and if an error occures during writing

getPrincipalElement

public static java.lang.String getPrincipalElement(java.security.Principal p,
                                                   java.lang.String commonSymbol)
For X509Certificates: Get an element of the Name from IssuerDN or SubjectDN



Copyright © 2003 DRIM Team. All Rights Reserved.