Instances of the Symmetric cipher class arew used to cipher peer-to-peer datagram packets. More...
Public Member Functions | |
SymmetricCipher (SecretKey secretKey, String verificator) | |
Wraps existing secret key with information about verificatory (if any). | |
SymmetricCipher (String algorithm, int keySize, boolean attnReport) | |
Generates a new secret key using specified algorithm and key size. | |
SecretKey | getSecretKey () |
Returns secret key. | |
boolean | isActive () |
Returns if cipher is properly initialized. | |
boolean | isVerified () |
Returns if public key was verified. | |
String | getVerificatorName () |
Returns if name of the verificator from authorized keys that verified this public key. | |
String | getAlgorithmDesc () |
Returns description of the cipher algorithm. | |
byte[] | encrypt (int randomPreambleLen, byte[] plainText) |
Encrypts random preamble of the given length appended with the input plain text. | |
byte[] | decrypt (int randomPreambleLen, byte[] cipherText) |
Decrypts cipher text first then discards random preamble of the given length. | |
String | encrypt (String plainText) |
Encrypts text message with random preamble and returns Base64 encoded cipher text. | |
String | decrypt (String encodedCipherText) |
Decodes Base64 encoded cipher text, decrypts text message and discards random preamble. | |
Private Attributes | |
Cipher | cipher = null |
Instance of the cipher used to encrypt/decrypt data. | |
SecretKey | secretKey = null |
Secret key used to encrypt/decrypt data. | |
String | verificator = null |
Contains name of the verificator (i.e the name associated with authorized public key that has verified this public key). | |
Static Private Attributes | |
static final String | mode = "/CBC" |
Cipher-Block Chaining mode. | |
static final String | padding = "/PKCS5Padding" |
PKCS5 padding. |
Instances of the Symmetric cipher class arew used to cipher peer-to-peer datagram packets.
Cipher's secret key is exchanged using asymmetric cipher.
Definition at line 24 of file SymmetricCipher.java.
crypto.SymmetricCipher.SymmetricCipher | ( | SecretKey | secretKey, |
String | verificator | ||
) |
Wraps existing secret key with information about verificatory (if any).
Definition at line 58 of file SymmetricCipher.java.
References crypto.SymmetricCipher.cipher, crypto.SymmetricCipher.mode, crypto.SymmetricCipher.padding, crypto.SymmetricCipher.secretKey, and crypto.SymmetricCipher.verificator.
{ this.secretKey = secretKey; this.verificator = verificator; try { this.cipher = Cipher.getInstance( secretKey.getAlgorithm () + mode + padding ); Log.trace( "New remote symmetric cipher: " + this.cipher.getAlgorithm () ); } catch( NoSuchAlgorithmException e ) { Log.exception( Log.ERROR, e ); } catch( NoSuchPaddingException e ) { Log.exception( Log.ERROR, e ); } if ( this.cipher == null ) { this.secretKey = null; } }
crypto.SymmetricCipher.SymmetricCipher | ( | String | algorithm, |
int | keySize, | ||
boolean | attnReport | ||
) |
Generates a new secret key using specified algorithm and key size.
Definition at line 86 of file SymmetricCipher.java.
References crypto.SymmetricCipher.cipher, crypto.SymmetricCipher.mode, crypto.SymmetricCipher.padding, and crypto.SymmetricCipher.secretKey.
{ this.secretKey = null; try { KeyGenerator keyGen = KeyGenerator.getInstance( algorithm ); keyGen.init( keySize ); this.secretKey = keyGen.generateKey (); this.cipher = Cipher.getInstance( secretKey.getAlgorithm () + mode + padding ); Log.trace( "New local symmetric cipher: " + this.cipher.getAlgorithm () ); if ( attnReport ) { Log.attn( "New symmetric cipher: " + algorithm + "/" + keySize ); } } catch( InvalidParameterException e ) { Log.exception( Log.ERROR, e ); if ( attnReport ) { Log.attn( "Error: Invalid parameter: " + e.getMessage () ); } } catch( NoSuchAlgorithmException e ) { Log.exception( Log.ERROR, e ); if ( attnReport ) { Log.attn( "Error: No such algorithm: " + e.getMessage () ); } } catch( NoSuchPaddingException e ) { Log.exception( Log.ERROR, e ); if ( attnReport ) { Log.attn( "Error: No such padding: " + e.getMessage () ); } } if ( this.cipher == null ) { this.secretKey = null; } }
byte [] crypto.SymmetricCipher.decrypt | ( | int | randomPreambleLen, |
byte[] | cipherText | ||
) |
Decrypts cipher text first then discards random preamble of the given length.
Definition at line 228 of file SymmetricCipher.java.
References crypto.SymmetricCipher.cipher.
Referenced by crypto.SymmetricCipher.decrypt(), CryptoPhoneApp.deferredOnInstantMessage(), and protocol.DatagramChannel.run().
{ if ( this.cipher == null ) { return null; } byte[] plainText = null; synchronized( this.cipher ) { try { byte[] ivBytes = new byte[8]; IvParameterSpec ivSpec = new IvParameterSpec( ivBytes ); this.cipher.init( Cipher.DECRYPT_MODE, this.secretKey, ivSpec ); byte[] buf = new byte[ cipher.getOutputSize( cipherText.length ) ]; int bufLen = cipher.update( cipherText, 0, cipherText.length, buf, 0 ); bufLen += cipher.doFinal( buf, bufLen ); /* Remove the IV and random preamble from the start of the message */ plainText = new byte[ bufLen - ivBytes.length - randomPreambleLen ]; System.arraycopy( buf, ivBytes.length + randomPreambleLen, plainText, 0, plainText.length ); } catch( Exception e ) { Log.exception( Log.PDU, e ); } } return plainText; }
String crypto.SymmetricCipher.decrypt | ( | String | encodedCipherText ) |
Decodes Base64 encoded cipher text, decrypts text message and discards random preamble.
Definition at line 299 of file SymmetricCipher.java.
References crypto.SymmetricCipher.decrypt().
{ String clearText = null; try { byte[] cipherText = Base64.decode( encodedCipherText ); byte[] data = this.decrypt( /*randomPreambleLen*/ 256, cipherText ); if ( data != null ) { String msg = new String( data, "UTF8" ); if ( msg.startsWith( "[BEGIN]" ) ) { clearText = msg.substring( 7 ); // skip prefix } } } catch( UnsupportedEncodingException e ) { Log.exception( Log.TRACE, e ); } catch( IOException e ) { Log.exception( Log.TRACE, e ); } return clearText; }
String crypto.SymmetricCipher.encrypt | ( | String | plainText ) |
Encrypts text message with random preamble and returns Base64 encoded cipher text.
Definition at line 270 of file SymmetricCipher.java.
References crypto.SymmetricCipher.encrypt().
{ String encodedCipherText = null; /* Send encrypted message to peer */ try { byte[] plainBin = ( "[BEGIN]" + plainText ).getBytes( "UTF8" ); byte[] cipherText = this.encrypt( /*randomPreambleLen*/ 256, plainBin ); if ( cipherText != null ) { encodedCipherText = Base64.encodeBytes( cipherText ); } } catch( UnsupportedEncodingException e ) { Log.exception( Log.TRACE, e ); } return encodedCipherText; }
byte [] crypto.SymmetricCipher.encrypt | ( | int | randomPreambleLen, |
byte[] | plainText | ||
) |
Encrypts random preamble of the given length appended with the input plain text.
Definition at line 181 of file SymmetricCipher.java.
References crypto.SymmetricCipher.cipher.
Referenced by crypto.SymmetricCipher.encrypt(), protocol.DatagramChannel.send(), and CryptoPhoneApp.sendInstantMessage().
{ if ( this.cipher == null ) { return null; } /* Generate random preamble */ byte[] preamble = new byte[ randomPreambleLen ]; for ( int i = 0; i < randomPreambleLen; ++i ) { preamble[i] = (byte)( Math.random () * 0x100 - 0x100 ); } /* IV specification for the CBC */ byte[] ivBytes = new byte[8]; IvParameterSpec ivSpec = new IvParameterSpec( ivBytes ); byte[] cipherText = null; synchronized( this.cipher ) { try { this.cipher.init( Cipher.ENCRYPT_MODE, this.secretKey, ivSpec ); int ptLength = ivBytes.length + preamble.length + plainText.length; cipherText = new byte[ cipher.getOutputSize( ptLength ) ]; int ctLength = cipher.update( ivBytes, 0, ivBytes.length, cipherText, 0 ); ctLength += cipher.update( preamble, 0, preamble.length, cipherText, ctLength ); ctLength += cipher.update( plainText, 0, plainText.length, cipherText, ctLength ); ctLength += cipher.doFinal( cipherText, ctLength ); } catch( Exception e ) { Log.exception( Log.PDU, e ); } } return cipherText; }
String crypto.SymmetricCipher.getAlgorithmDesc | ( | ) |
Returns description of the cipher algorithm.
Definition at line 168 of file SymmetricCipher.java.
References crypto.SymmetricCipher.secretKey.
Referenced by protocol.DatagramChannel.useSymmetricCipher().
SecretKey crypto.SymmetricCipher.getSecretKey | ( | ) |
Returns secret key.
Definition at line 134 of file SymmetricCipher.java.
References crypto.SymmetricCipher.secretKey.
Referenced by crypto.CipherEngine.getSignedSecretKey().
{ return secretKey; }
String crypto.SymmetricCipher.getVerificatorName | ( | ) |
Returns if name of the verificator from authorized keys that verified this public key.
Definition at line 160 of file SymmetricCipher.java.
References crypto.SymmetricCipher.verificator.
Referenced by CryptoPhoneApp.deferredOnAccept().
{ return this.verificator; }
boolean crypto.SymmetricCipher.isActive | ( | ) |
Returns if cipher is properly initialized.
Definition at line 142 of file SymmetricCipher.java.
References crypto.SymmetricCipher.cipher.
Referenced by CryptoPhoneApp.deferredOnAccept(), and crypto.CipherEngine.generateNewSecret().
{ return this.cipher != null; }
boolean crypto.SymmetricCipher.isVerified | ( | ) |
Returns if public key was verified.
Definition at line 150 of file SymmetricCipher.java.
References crypto.SymmetricCipher.verificator.
Referenced by CryptoPhoneApp.deferredOnAccept().
{ return this.verificator != null; }
Cipher crypto.SymmetricCipher.cipher = null [private] |
Instance of the cipher used to encrypt/decrypt data.
Definition at line 41 of file SymmetricCipher.java.
Referenced by crypto.SymmetricCipher.decrypt(), crypto.SymmetricCipher.encrypt(), crypto.SymmetricCipher.isActive(), and crypto.SymmetricCipher.SymmetricCipher().
final String crypto.SymmetricCipher.mode = "/CBC" [static, private] |
Cipher-Block Chaining mode.
Definition at line 30 of file SymmetricCipher.java.
Referenced by crypto.SymmetricCipher.SymmetricCipher().
final String crypto.SymmetricCipher.padding = "/PKCS5Padding" [static, private] |
PKCS5 padding.
Definition at line 36 of file SymmetricCipher.java.
Referenced by crypto.SymmetricCipher.SymmetricCipher().
SecretKey crypto.SymmetricCipher.secretKey = null [private] |
Secret key used to encrypt/decrypt data.
Definition at line 46 of file SymmetricCipher.java.
Referenced by crypto.SymmetricCipher.getAlgorithmDesc(), crypto.SymmetricCipher.getSecretKey(), and crypto.SymmetricCipher.SymmetricCipher().
String crypto.SymmetricCipher.verificator = null [private] |
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.
Definition at line 53 of file SymmetricCipher.java.
Referenced by crypto.SymmetricCipher.getVerificatorName(), crypto.SymmetricCipher.isVerified(), and crypto.SymmetricCipher.SymmetricCipher().