Cryptography Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.00

JCE - Cipher for Encryption and Decryption

Part:   1  2  3 

(Continued from previous part...)

      System.out.println();
      System.out.println("KeySpec Object Info: ");
      System.out.println("Saved File = "+fl);
      System.out.println("Length = "+kb.length);
      System.out.println("toString = "+ks.toString());
      System.out.println();
      System.out.println("SecretKey Object Info: ");
      System.out.println("Algorithm = "+ky.getAlgorithm());
      System.out.println("toString = "+ky.toString());
      return ky;
   }
   private static void secretCipher(String algorithm, String mode,
      SecretKey ky, String input, String output) throws Exception {
      Cipher cf = Cipher.getInstance(algorithm);
      if (mode.equalsIgnoreCase("encrypt")) 
         cf.init(Cipher.ENCRYPT_MODE,ky);
      else if (mode.equalsIgnoreCase("decrypt"))
         cf.init(Cipher.DECRYPT_MODE,ky);
      else
         throw new Exception("Invalid mode: "+mode);
      System.out.println();
      System.out.println("Cipher Object Info: ");
      System.out.println("Block Size = "+cf.getBlockSize());
      System.out.println("Algorithm = "+cf.getAlgorithm());
      System.out.println("Provider = "+cf.getProvider());
      System.out.println("toString = "+cf.toString());

      FileInputStream fis = new FileInputStream(input);
      FileOutputStream fos = new FileOutputStream(output);
      int bufSize = 1024;
      byte[] buf = new byte[bufSize];
      int n = fis.read(buf,0,bufSize);
      int fisSize = 0;
      int fosSize = 0;
      while (n!=-1) {
         fisSize += n;
         byte[] out = cf.update(buf,0,n);
         fosSize += out.length;
         fos.write(out);
         n = fis.read(buf,0,bufSize);
      }
      byte[] out = cf.doFinal();
      fosSize += out.length;
      fos.write(out);
      fis.close();
      fos.close();
      System.out.println();
      System.out.println("Cipher Process Info: ");
      System.out.println("Input Size = "+fisSize);
      System.out.println("Output Size = "+fosSize);
   }
}

Here is the result of my first test. It is done with JDK 1.4.1.

java -cp . JceSecretCipher Blowfish encrypt bfish.key 
   JceSecretCipher.java jce.cph

KeySpec Object Info:
Saved File = bfish.key
Length = 7
toString = javax.crypto.spec.SecretKeySpec@2685020e

SecretKey Object Info:
Algorithm = Blowfish
toString = javax.crypto.spec.SecretKeySpec@2685020e

Cipher Object Info:
Block Size = 8
Algorithm = Blowfish
Provider = SunJCE version 1.4
toString = javax.crypto.Cipher@106082

Cipher Process Info:
Input Size = 3684
Output Size = 3688

java -cp . JceSecretCipher Blowfish decrypt bfish.key jce.cph jce.clr

KeySpec Object Info:
Saved File = bfish.key
Length = 7
toString = javax.crypto.spec.SecretKeySpec@2685020e

SecretKey Object Info:
Algorithm = Blowfish
toString = javax.crypto.spec.SecretKeySpec@2685020e

Cipher Object Info:
Block Size = 8
Algorithm = Blowfish
Provider = SunJCE version 1.4
toString = javax.crypto.Cipher@106082

Cipher Process Info:
Input Size = 3688
Output Size = 3684

comp JceSecretCipher.java jce.clr
Comparing JceSecretCipher.java and jce.clr...
Files compare OK

Note that:

  • bfish.key is the key file generated by KeyGenerator with the Blowfish algorithm.
  • The last command confirms that the encryption process and the decryption process work correctly.
  • The block size of Blowfish encryption algorithm is 8 bytes.

You can do more tests with DES and HmacMD5 keys.

(Continued on next part...)

Part:   1  2  3 

Dr. Herong Yang, updated in 2007
Cryptography Tutorials - Herong's Tutorial Notes - JCE - Cipher for Encryption and Decryption