org.ssonet.util
Class CryptoTools

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

public class CryptoTools
extends java.lang.Object

Title: Cryptotools for SSONET Description: This class is a collection of helpfull Methods for handling cryptographic operations Copyright: Copyright (c) 2001 Company:


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.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.lang.String certificateFileName)
          reads a certificate from file
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:
duration - Gültigkeitszeitraum in Tagen
subject - Distinguished Name of the subject of this certificate

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

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

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

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