webfunds.sox
Class Crypto

java.lang.Object
  |
  +--webfunds.sox.Crypto

public final class Crypto
extends java.lang.Object

XXX: Do Not Touch This File!!! Centralized crypto methods. Currently being overhauled.


Field Summary
static int cipher_keylen
           
static java.lang.String pk_alg
           
static java.lang.String sig_alg
           
static java.security.SecureRandom sr
           
 
Constructor Summary
Crypto()
           
 
Method Summary
static java.security.PrivateKey decodePrivateKey(byte[] data)
          Decode an RSA PrivateKey object from encoded data
static java.security.PublicKey decodePublicKey(byte[] data)
          Decode an RSA PublicKey object from encoded data
static byte[] decodeSignature(byte[] data)
          Extract the signature data from an X509 Signature packet
static byte[] decrypt(java.security.Key key, byte[] data)
          Decrypt the data and return the encrypted binary data The data is uncompressed after decrypting
static byte[] digest(byte[] data)
          Create a message digest (as a byte array) from data (as a byte array) The digest uses the algorithm specified in crypto.message-digest.algorithm, and if not defined, defaults to the MD_ALGORITHM algorithm.
static byte[] encodePrivateKey(java.security.PrivateKey key)
          Encode an RSA PrivateKey object
static byte[] encodePublicKey(java.security.PublicKey key)
          Encode an RSA PublicKey object
static byte[] encodeSignature(byte[] sig)
          Encode a signature into an X509 signature packet
static byte[] encrypt(java.security.Key key, byte[] data)
          Encrypt the data and return the encrypted binary data The data is compressed before encrypting
static boolean equals(java.security.PrivateKey left, java.security.PrivateKey right)
           
static boolean equals(java.security.PublicKey left, java.security.PublicKey right)
           
static byte[] fingerprint(java.security.PublicKey key)
           
static java.security.Key generateKey()
          Generate (randomly) a new Key object, suitable for the default symmetric cipher.
static java.security.KeyPair generateKeys(int bits)
          Useful for making accounts, better than certs.
static byte[] getMagnitude(java.math.BigInteger x)
           
static java.security.PublicKey getPublicKeyFromCert(java.security.cert.Certificate cert)
          Extract the public key from an X509 Certificate
static java.security.SecureRandom getSecureRandom()
          get the PRNG.
static void main(java.lang.String[] args)
           
static byte[] pk_encrypt(java.security.Key key, java.security.PublicKey pk, byte[] data)
          Encrypt the data and return the encrypted binary data.
static byte[] pk_encrypt(java.security.Key key, java.security.PublicKey pk, byte[] data, int offset, int len)
          Encrypt the data and return the encrypted binary data.
static byte[] pk_encrypt(java.security.PublicKey pk, byte[] data, int offset, int len)
          Encrypt the data and return the encrypted binary data.
static void setSecureRandom(java.security.SecureRandom userSR)
          set a PRNG.
static byte[] sign(java.security.PrivateKey key, byte[] data)
          Sign the data and return the binary X509 signature
static java.security.PublicKey toCryptixJCEKey(java.security.PublicKey pk)
           
static java.security.PrivateKey toCryptixKey(java.security.PrivateKey pk)
           
static java.security.PublicKey toCryptixKey(java.security.PublicKey pk)
           
static boolean verify(java.security.PublicKey key, byte[] sigX509, byte[] data)
          Determine whether the signature on the data is valid
static boolean verifyCertificate(java.security.cert.Certificate cert, java.security.PublicKey key)
          Verify the signature on a signed certificate.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cipher_keylen

public static final int cipher_keylen

pk_alg

public static final java.lang.String pk_alg

sig_alg

public static final java.lang.String sig_alg

sr

public static java.security.SecureRandom sr
Constructor Detail

Crypto

public Crypto()
Method Detail

setSecureRandom

public static void setSecureRandom(java.security.SecureRandom userSR)
                            throws java.lang.IllegalArgumentException
set a PRNG. Sometimes the default is not good enough.

getSecureRandom

public static java.security.SecureRandom getSecureRandom()
get the PRNG.

generateKeys

public static java.security.KeyPair generateKeys(int bits)
Useful for making accounts, better than certs. Do this: PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate();

verifyCertificate

public static boolean verifyCertificate(java.security.cert.Certificate cert,
                                        java.security.PublicKey key)
Verify the signature on a signed certificate.
Parameters:
cert - The signed X509 certificate to be validated
key - The key used to sign the certificate
Returns:
true if the signature is valid, false if not

getPublicKeyFromCert

public static java.security.PublicKey getPublicKeyFromCert(java.security.cert.Certificate cert)
Extract the public key from an X509 Certificate
Parameters:
cert - The X509 certificate containing the key
Returns:
the key from the certificate

generateKey

public static java.security.Key generateKey()
                                     throws java.security.KeyException
Generate (randomly) a new Key object, suitable for the default symmetric cipher. Uses JCE 1.2.
Returns:
A new (random) Key object

digest

public static byte[] digest(byte[] data)
Create a message digest (as a byte array) from data (as a byte array) The digest uses the algorithm specified in crypto.message-digest.algorithm, and if not defined, defaults to the MD_ALGORITHM algorithm.
Parameters:
data - the data to be digested
Returns:
the message digest (as a byte array)

encrypt

public static byte[] encrypt(java.security.Key key,
                             byte[] data)
Encrypt the data and return the encrypted binary data The data is compressed before encrypting
Parameters:
key - the key with which to encrypt the data (as a byte array)
data - the data being encrypted (as a byte array)
Returns:
the encrypted data

decrypt

public static byte[] decrypt(java.security.Key key,
                             byte[] data)
                      throws java.security.KeyException
Decrypt the data and return the encrypted binary data The data is uncompressed after decrypting
Parameters:
key - the key with which to decrypt the data (as a byte array)
data - the data being decrypted (as a byte array)
Returns:
the decrypted data

pk_encrypt

public static byte[] pk_encrypt(java.security.Key key,
                                java.security.PublicKey pk,
                                byte[] data,
                                int offset,
                                int len)
                         throws java.security.KeyException
Encrypt the data and return the encrypted binary data.
Parameters:
key - the symmetric key with which to encrypt the data
pk - the PublicKey with which to encrypt the symmetric key
data - the data to be encrypted (as a byte array)
offset - the offset within data where the data starts
len - the length of the data with the data array
Returns:
the encrypted data (as a byte array)
Throws:
java.security.KeyException - a problem has occurred with the public key, such as it not being in the correct format.

pk_encrypt

public static byte[] pk_encrypt(java.security.PublicKey pk,
                                byte[] data,
                                int offset,
                                int len)
                         throws java.security.KeyException
Encrypt the data and return the encrypted binary data. The internal symmetric key is randomly generated.
Parameters:
pk - the PublicKey with which to encrypt the symmetric key
data - the data to be encrypted (as a byte array)
offset - the offset within data where the data starts
len - the length of the data with the data array
Returns:
the encrypted data (as a byte array)
Throws:
java.security.KeyException - a problem has occurred with the public key, such as it not being in the correct format.

pk_encrypt

public static byte[] pk_encrypt(java.security.Key key,
                                java.security.PublicKey pk,
                                byte[] data)
                         throws java.security.KeyException
Encrypt the data and return the encrypted binary data.
Parameters:
key - the symmetric key with which to encrypt the data
pk - the PublicKey with which to encrypt the symmetric key
data - the data to be encrypted (as a byte array)
Returns:
the encrypted data (as a byte array)
Throws:
java.security.KeyException - a problem has occurred with the public key, such as it not being in the correct format.

sign

public static byte[] sign(java.security.PrivateKey key,
                          byte[] data)
                   throws java.security.KeyException
Sign the data and return the binary X509 signature
Parameters:
key - a PrivateKey with which to sign the data
data - the data being signed (as a byte array)
Returns:
the binary X509 signature
Throws:
java.security.KeyException - a problem has occurred with the private key, such as the password being incorrect.

verify

public static boolean verify(java.security.PublicKey key,
                             byte[] sigX509,
                             byte[] data)
                      throws java.security.KeyException
Determine whether the signature on the data is valid
Parameters:
key - a PublicKey with which to verify the signature
signature - the X509 signature, as an ascii-armoured base64 encoded String
data - the data which was signed (as a byte array)
Returns:
a boolean value, true if the signature is valid for the key and data
Throws:
java.security.KeyException - a problem has occurred with the public key, such as an incorrectly formatted public key.

fingerprint

public static byte[] fingerprint(java.security.PublicKey key)
                          throws java.security.InvalidKeyException

getMagnitude

public static byte[] getMagnitude(java.math.BigInteger x)

decodeSignature

public static byte[] decodeSignature(byte[] data)
                              throws java.security.SignatureException
Extract the signature data from an X509 Signature packet
Parameters:
data - The X509 signature packet containing the raw signature
Returns:
the raw signature data
Throws:
java.security.SignatureException - the signature packet is invalid (i.e. incorrectly formatted)

encodeSignature

public static byte[] encodeSignature(byte[] sig)
Encode a signature into an X509 signature packet
Parameters:
key - The signature to be wrapped by the X509 signature packet
Returns:
the X509 signature packet (as a byte array)

decodePublicKey

public static java.security.PublicKey decodePublicKey(byte[] data)
                                               throws java.security.InvalidKeyException
Decode an RSA PublicKey object from encoded data
Parameters:
data - The encoded data containing the public key
Returns:
a PublicKey object, derived from the data
Throws:
java.security.InvalidKeyException - the encoded public key is invalid (i.e. incorrectly formatted)

encodePublicKey

public static byte[] encodePublicKey(java.security.PublicKey key)
Encode an RSA PublicKey object
Parameters:
key - The encoded data containing the public key
Returns:
the encoded key (as a byte array)

decodePrivateKey

public static java.security.PrivateKey decodePrivateKey(byte[] data)
                                                 throws java.security.InvalidKeyException
Decode an RSA PrivateKey object from encoded data
Parameters:
data - The encoded data containing the private key
Returns:
a PrivateKey object, derived from the data
Throws:
java.security.InvalidKeyException - the encoded private key is invalid (i.e. incorrectly formatted)

encodePrivateKey

public static byte[] encodePrivateKey(java.security.PrivateKey key)
Encode an RSA PrivateKey object
Parameters:
key - The encoded data containing the private key
Returns:
the encoded key (as a byte array)

toCryptixKey

public static java.security.PublicKey toCryptixKey(java.security.PublicKey pk)

toCryptixKey

public static java.security.PrivateKey toCryptixKey(java.security.PrivateKey pk)

equals

public static boolean equals(java.security.PublicKey left,
                             java.security.PublicKey right)

equals

public static boolean equals(java.security.PrivateKey left,
                             java.security.PrivateKey right)

toCryptixJCEKey

public static java.security.PublicKey toCryptixJCEKey(java.security.PublicKey pk)

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception