JceSecretCipher.java - Secret Key Encryption Sample Program

This section provides a tutorial example on how to write a secret key encryption and description program using the javax.crypto.Cipher class.

The following sample program shows you how to do encryption and decryption with a secret key using the javax.crypto.Cipher class:

/* JceSecretCipher.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
import java.io.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class JceSecretCipher {
   public static void main(String[] a) {
      if (a.length<5) {
         System.out.println("java JceSecretCipher algorithm mode keyFile"
            +" input output");
      String algorithm = a[0];
      String mode = a[1];
      String keyFile = a[2];
      String input = a[3];
      String output = a[4];
      try {
         SecretKey ky = readKey(keyFile,algorithm);
         secretCipher(algorithm, mode, ky, input, output);
      } catch (Exception e) {
         System.out.println("Exception: "+e);
   private static SecretKey readKey(String input, String algorithm) 
      throws Exception {
      String fl = input;
      FileInputStream fis = new FileInputStream(fl);
      int kl = fis.available();
      byte[] kb = new byte[kl];
      KeySpec ks = null;
      SecretKey ky = null;
      SecretKeyFactory kf = null;
      if (algorithm.equalsIgnoreCase("DES")) {
      	 ks = new DESKeySpec(kb);
         kf = SecretKeyFactory.getInstance("DES");
         ky = kf.generateSecret(ks);
      } else if (algorithm.equalsIgnoreCase("DESede")) {
      	 ks = new DESedeKeySpec(kb);
         kf = SecretKeyFactory.getInstance("DESede");
         ky = kf.generateSecret(ks);
      } else {
         ks = new SecretKeySpec(kb,algorithm);
         ky = new SecretKeySpec(kb,algorithm);
      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("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")) 
      else if (mode.equalsIgnoreCase("decrypt"))
         throw new Exception("Invalid mode: "+mode);
      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;
         n = fis.read(buf,0,bufSize);
      byte[] out = cf.doFinal();
      fosSize += out.length;
      System.out.println("Cipher Process Info: ");
      System.out.println("Input Size = "+fisSize);
      System.out.println("Output Size = "+fosSize);

Note that:

See the next section for test result of this sample program.

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

Cipher - Secret Key Encryption and Decryption

 javax.crypto.Cipher - The Secret Key Encryption Class

JceSecretCipher.java - Secret Key Encryption Sample Program

 Blowfish and DES Secret Key Encryption Tests

 JcePublicCipher.java - Private and Public Key Encryption

 DSA Public Key Encryption Tests

 RSA Public Key Encryption Tests

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version