crypto
Class PublicEncryptor

java.lang.Object
  extended by crypto.PublicEncryptor

public class PublicEncryptor
extends java.lang.Object

Implements public part of the asymmetric cipher (with public key) used to send encrypted local secret key (used for symmetric ciphering of peer-to-peer datagram packets) to remote peer. The class holds also list of authorized public keys, which is used to verify signed objects received from peers.

Author:
Mikica B Kocic

Field Summary
private static java.util.ArrayList<NamedPublicKey> authorizedKeys
          Authorized public keys (loaded from file)
private static java.lang.String authorizedKeysFile
          The name of the file holding authorized public keys of remote peers
private  javax.crypto.Cipher cipher
          Instance of the encrypting engine based on remote public key
private static java.lang.String digest
          Message digest used for creating/validating signatures
private static 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 PublicEncryptor.encrypt().
private  java.security.PublicKey publicKey
          Public key
private  java.lang.String serializedPublicKey
          Remote public key: serialized and encoded as Base64 string.
private  java.lang.String verificator
          Contains name of the verificator (i.e the name associated with authorized public key that has verified this public key).
 
Constructor Summary
PublicEncryptor(java.lang.String serializedPublicKey, java.lang.String remoteUserId)
          Deserializes public key from the Base64 string and instantiates PublicEncryptor.
 
Method Summary
private static void createEmptyAuthorizedPublicKeys(java.lang.String filename)
          Create empty authorized keys file if it does not exist and adjust permissions.
 byte[] encrypt(byte[] plainText)
          Encrypts plain text using public key.
 java.lang.String encryptAndSerialize(java.io.Serializable object)
          Returns Base64 of encrypted (using our public key) object.
 java.lang.String getVerificatorName()
          Returns if name of the verificator from authorized keys that verified this public key
 boolean isActive()
          Returns if cipher is properly initialized
 boolean isVerified()
          Returns if public key was verified
static void loadAuthorizedPublicKeys()
          Loads authorized keys
static java.lang.String verifyObject(java.security.SignedObject object)
          Verifies signed object with a public key from the authorized public keys
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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 PublicEncryptor.encrypt().

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

authorizedKeysFile

private static final java.lang.String authorizedKeysFile
The name of the file holding authorized public keys of remote peers

See Also:
Constant Field Values

authorizedKeys

private static java.util.ArrayList<NamedPublicKey> authorizedKeys
Authorized public keys (loaded from file)


publicKey

private java.security.PublicKey publicKey
Public key


cipher

private javax.crypto.Cipher cipher
Instance of the encrypting engine based on remote public key


serializedPublicKey

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


verificator

private java.lang.String verificator
Contains name of the verificator (i.e the name associated with authorized public key that has verified this public key). Not null indicates that the public key was successfully verified.

Constructor Detail

PublicEncryptor

public PublicEncryptor(java.lang.String serializedPublicKey,
                       java.lang.String remoteUserId)
Deserializes public key from the Base64 string and instantiates PublicEncryptor. Verifies public key with the public key retrieved from the authorized keys.

Method Detail

createEmptyAuthorizedPublicKeys

private static void createEmptyAuthorizedPublicKeys(java.lang.String filename)
Create empty authorized keys file if it does not exist and adjust permissions.


loadAuthorizedPublicKeys

public static void loadAuthorizedPublicKeys()
Loads authorized keys


verifyObject

public static java.lang.String verifyObject(java.security.SignedObject object)
Verifies signed object with a public key from the authorized public keys

Returns:
not null if verified with the name associated to authorized public key

isActive

public boolean isActive()
Returns if cipher is properly initialized


isVerified

public boolean isVerified()
Returns if public key was verified


getVerificatorName

public java.lang.String getVerificatorName()
Returns if name of the verificator from authorized keys that verified this public key

Returns:
name of the verificator; May be null indicating not verified public key

encrypt

public byte[] encrypt(byte[] plainText)
Encrypts plain text using public 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_encryption.png

See Also:
AsymmetricCipher.decrypt(byte[])

encryptAndSerialize

public java.lang.String encryptAndSerialize(java.io.Serializable object)
Returns Base64 of encrypted (using our public key) object.