crypto
Class AsymmetricCipher

java.lang.Object
  extended by crypto.AsymmetricCipher

public class AsymmetricCipher
extends java.lang.Object

Implements asymmetric cipher with public and private keys used to retrieve secret key (used for symmetric ciphering of peer-to-peer datagram packets) from remote peer. Remote peer sends its secret key encrypted with our public key. Transmission of the SecretKey can be schematically shown:

  Send secret key:
  secretKey >> serialize >> encrypt (with PubKey) >> encode to Base64 >> transmit
  
  Reconstruct secret key:
  receive >> decode from Base64 >> decrypt (with PrivKey) >> deserialize >> secretKey
  

Author:
Mikica B Kocic

Field Summary
private static java.lang.String algorithm
          Asymmetric cipher algorithm
private  javax.crypto.Cipher cipher
          Instance of the decrypting engine based on our private key
private static java.lang.String digest
          Message digest used for creating/validating signatures
private  java.lang.String keyPairComment
          The comment (description) of the key pair
private static int keySize
          Default key size for algorithm
private static java.lang.String padding
          Padding to be used when ciphering/deciphering.
private  java.security.PrivateKey privateKey
          Private key used for deciphering and signing messages
private static java.lang.String privateKeyFile
          The name of the file holding saved private key
private  java.security.PublicKey publicKey
          Public key corresponding to our private key
private static java.lang.String publicKeyFile
          The name of the file holding saved public key
private  java.lang.String serializedPublicKey
          Our public key: serialized and encoded as Base64 string.
 
Constructor Summary
AsymmetricCipher()
          Generates a pair of keys and serializes public key as Base64 string.
 
Method Summary
 byte[] decrypt(byte[] cipherText)
          Decrypts cipher text using the private key.
 SymmetricCipher deserializeEncryptedSecretKey(java.lang.String serializedSecretKey)
          Reconstructs secret key from Base64 respresentation of encrypted (using our public key) serialized secret key.
private  void destruct()
          Destructs object (makes it inactive)
 void exportPublicKey(java.lang.String fileName)
          Save public key into file
private  void generateKeyPair()
          Generates a key pair
 java.lang.String getNamedPublicKey()
          Returns serializable named publicKey (with comment) encoded as Base64
 java.lang.String getSerializedAndSignedPublicKey()
          Returns serialized public key used for encryption of datagrams as Base64 string.
private  void instantiateCipher()
          Instantiates a cipher
 boolean isActive()
          Returns if cipher is properly initialized
static java.lang.Object loadObject(java.lang.String fileName)
          Loads serializable object encoded in Base64 from file
private  boolean loadSavedKeyPair()
          Load saved key pair
private  boolean sanityCheck()
          Sanity check whether PublicEncryptor works with PrivateEncryption
private  void saveKeyPair()
          Saves private/public key pair with description (this.keyPairComment)
static boolean saveObject(java.io.Serializable object, java.lang.String fileName, java.lang.String comment)
          Saves serializable object encoded in Base64 to file
private  void serializePublicKey()
          Serializes the public key, signs it and encodes in Base64 format
 java.security.SignedObject signObject(java.io.Serializable object)
          Signs object using private key
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

algorithm

private static final java.lang.String algorithm
Asymmetric cipher algorithm

See Also:
Constant Field Values

keySize

private static final int keySize
Default key size for algorithm

See Also:
Constant Field Values

padding

private static final java.lang.String padding
Padding to be used when ciphering/deciphering. JCE does not support RSA/CBC so the CBC mode is built on the top of ECB in AsymmetricCipher.decrypt().

See Also:
Constant Field Values

digest

private static final java.lang.String digest
Message digest used for creating/validating signatures

See Also:
Constant Field Values

privateKeyFile

private static final java.lang.String privateKeyFile
The name of the file holding saved private key

See Also:
Constant Field Values

publicKeyFile

private static final java.lang.String publicKeyFile
The name of the file holding saved public key

See Also:
Constant Field Values

privateKey

private java.security.PrivateKey privateKey
Private key used for deciphering and signing messages


publicKey

private java.security.PublicKey publicKey
Public key corresponding to our private key


keyPairComment

private java.lang.String keyPairComment
The comment (description) of the key pair


cipher

private javax.crypto.Cipher cipher
Instance of the decrypting engine based on our private key


serializedPublicKey

private java.lang.String serializedPublicKey
Our public key: serialized and encoded as Base64 string.

Constructor Detail

AsymmetricCipher

public AsymmetricCipher()
Generates a pair of keys and serializes public key as Base64 string.

Method Detail

destruct

private void destruct()
Destructs object (makes it inactive)


loadSavedKeyPair

private boolean loadSavedKeyPair()
Load saved key pair


saveKeyPair

private void saveKeyPair()
Saves private/public key pair with description (this.keyPairComment)


generateKeyPair

private void generateKeyPair()
Generates a key pair


instantiateCipher

private void instantiateCipher()
Instantiates a cipher


serializePublicKey

private void serializePublicKey()
Serializes the public key, signs it and encodes in Base64 format


isActive

public boolean isActive()
Returns if cipher is properly initialized


getSerializedAndSignedPublicKey

public java.lang.String getSerializedAndSignedPublicKey()
Returns serialized public key used for encryption of datagrams as Base64 string.


exportPublicKey

public void exportPublicKey(java.lang.String fileName)
Save public key into file

Parameters:
fileName - file where to save public key; if null, it will be the default: AssymmetricCipher.publicKeyFile

getNamedPublicKey

public java.lang.String getNamedPublicKey()
Returns serializable named publicKey (with comment) encoded as Base64


saveObject

public static boolean saveObject(java.io.Serializable object,
                                 java.lang.String fileName,
                                 java.lang.String comment)
Saves serializable object encoded in Base64 to file


loadObject

public static java.lang.Object loadObject(java.lang.String fileName)
Loads serializable object encoded in Base64 from file


sanityCheck

private boolean sanityCheck()
Sanity check whether PublicEncryptor works with PrivateEncryption


decrypt

public byte[] decrypt(byte[] cipherText)
Decrypts cipher text using the private key. Emulates CBC (cipher-block chaining) using plain ECB. Why? -- Because JCE does not support RSA/CBC cipher (only RSA/ECB). See Cipher-block chaining (CBC) \image html cbc_decryption.png

See Also:
PublicEncryptor.encrypt(byte[])

signObject

public java.security.SignedObject signObject(java.io.Serializable object)
Signs object using private key


deserializeEncryptedSecretKey

public SymmetricCipher deserializeEncryptedSecretKey(java.lang.String serializedSecretKey)
Reconstructs secret key from Base64 respresentation of encrypted (using our public key) serialized secret key.