Sunday, October 30, 2016

Symmetric Block Ciphers

So, here is a partial list of symmetric block ciphers available in Bouncy Castle :
  • RC5 has 64 bit block size and 128 bit key size
  • Blowfish has 64 bit block size and 1 to 448 bit key size
  • AES has 128 bit block size and 128, 192, 256 bit key sizes
  • RC6 has 128 bit block size and 128, 192, 256 bit key sizes
  • Serpent has 128 bit block size and 128, 192, 256 bit key sizes
  • Twofish has 128 bit block size and key sizes up to 256 bits



You will certainly need a byte array for a Key, and a SecretKeySpec object to convert this byte array to a form acceptable to the Cipher object that does the actual encryption and decryption of your Input. The Key byte array doesn't need to be the same length as the Cipher block size, but must be a length acceptable to the Cipher. Note the above ciphers all have both a block size and acceptable key sizes.

Any of the above block ciphers without additional processing only securely encrypts a single block. To securely encrypt larger amounts of input, encryption Modes have been developed that use the encrypted results of the current block to additionally secure the following block.

Obviously an initial block must be available to securely encrypt the first block of actual input. This block is called the Initialization Vector (IV). It can be explicitly provided, or derived from the password or from looking ahead in the input.

CBC (Cipher Block Chaining) is a commonly used mode that uses an explicitly provided random byte block as the IV. Nothing has to be communicated to the receiver for decryption, the first block of the decrypted message is discarded and the remainder is the original message. CBC requires the input to be padded if necessary to be modulo 0 block size.



For Symmetric Block Ciphers to work at all, both parties must agree on the Cipher, the Mode, and the Key byte array.

Technically there is no reason that once established any of these need to be changed. However as a matter of policy, since the Key byte array may somehow become compromised, they are often changed on a schedule. Then a secure means must be found to communicate the new key to the other party.



Friday, October 28, 2016

SecretKeySpec Class

The simplest way to generate a secret key is select a block of bytes from an array of bytes.

However you want to do it, generate an array of bytes at least as long as your intended cipher requires. Most ciphers now require 16, 24, or 64 byte blocks as keys.

SecretKeySpec has two constructors which give you a choice of exactly the right sized array of bytes or some block of bytes selected from a larger byte array.
byte[] keyArray1 = new byte[16];
byte[] keyArray2 = new byte[24];
byte[] keyArray3 = new byte[64];
Here are the two SecretKeySpec constructors :
SecretKeySpec(byte[] key, String algorithm)
SecretKeySpec key1 = new SecretKeySpec(keyArray1,"AES");

SecretKeySpec(byte[] key, int offset, int len, String algorithm)
SecretKeySpec key2 = new SecretKeySpec(keyArray3,13,16,"Serpent");


Here is the link to the official Oracle Documentation




Where to Get the JCE and How to Install it

For JDK 1.2 & JDK 1.3, go to the Bouncy Castle web site and download JCE 1.2.2.

For JDK 1.4 and later, the JCE is already included.



For JDK 1.2 the files you want are all named xxx-jdk12-155.jar and xxx-jdk12-155.zip

The Provider pkg is bcprov-jdk12-155.jar

The Open PGP pkg is bcpg-jdk12-155.jar

The Test Class pkg is bctest-jdk12-155.jar



Copy the *.jar files to the jre/lib/ext folder in both the JDK and the JRE



Add Bouncy Castle to the provider list in jre/lib/security for both the JDK and the JRE

Use the next number available.

security.provider.5=org.bouncycastle.jce.provider.BouncyCastleProvider



Look around in the above compressed files for a program called SimpleProviderTest.

Compile and run it to get : BC provider is installed  msg.



Thursday, October 27, 2016

Creating a SecureRandom Object

import java.security.SecureRandom;

SecureRandom random = new SecureRandom();
byte[] bytes = new byte[20];
random.nextBytes(bytes);

If you don't have a hardware random number generator built into your CPU, or if you don't trust it, then the random.nextBytes() method will probably block until it can find enough entropy to generate the bytes you need. In my experience it only blocks on the first request, and later requests return quickly.


Links

Synopsis

Terse Systems

Examples



Wednesday, October 26, 2016

Using the Cipher Class for File Encryption



{snip}

    fileChooser1.setFileFilter(new BBSFilter());
    if(fileChooser1.APPROVE_OPTION != fileChooser1.showOpenDialog(this)) return;
    clearFileName = fileChooser1.getSelectedFile().getPath();
    File clearFile = new File(clearFileName);
    int prefix = clearFileName.length()-4;
    cryptoFileName = (clearFileName.substring(0,prefix)).concat(".XXX");
    File cryptoFile = new File(cryptoFileName);
    plainFileName = (clearFileName.substring(0,prefix)).concat(".TXT");
    File plainFile = new File(plainFileName);

    byte[] keyBytes = new byte[] {
           0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
           0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
           0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
    byte[] ivBytes = new byte[] {
           0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03,
           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };

    try{

      SecretKeySpec key = new SecretKeySpec(keyBytes,"Twofish");
      IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
      Cipher cipher = Cipher.getInstance("Twofish/CTR/NoPadding","BC");

      // encryption pass
      cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
      FileInputStream fis = new FileInputStream(clearFile);
      BufferedInputStream bis = new BufferedInputStream(fis,DSKBLOK);
      CipherInputStream cis = new CipherInputStream(bis,cipher);
      FileOutputStream fos = new FileOutputStream(cryptoFile);
      BufferedOutputStream bos = new BufferedOutputStream(fos,DSKBLOK);
      int ch; while((ch = cis.read()) >= 0) bos.write(ch);
      bos.flush(); bos.close();

      // decryption pass
      cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
      fis = new FileInputStream(cryptoFileName);
      bis = new BufferedInputStream(fis,DSKBLOK);
      cis = new CipherInputStream(bis,cipher);
      fos = new FileOutputStream(plainFile);
      bos = new BufferedOutputStream(fos,DSKBLOK);
      while ((ch = cis.read()) >= 0) bos.write(ch);
      bos.flush(); bos.close();
    } catch(Exception ee) { statusBar.setText("\n   Oooops!\n"); }


{snip}