This program tests the AES cipher : Encryption « Security « Java






This program tests the AES cipher

   


/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

/**
 * This program tests the AES cipher. Usage:<br>
 * java AESTest -genkey keyfile<br>
 * java AESTest -encrypt plaintext encrypted keyfile<br>
 * java AESTest -decrypt encrypted decrypted keyfile<br>
 * @author Cay Horstmann
 * @version 1.0 2004-09-14
 */
public class AESTest
{
   public static void main(String[] args)
   {
      try
      {
         if (args[0].equals("-genkey"))
         {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            SecureRandom random = new SecureRandom();
            keygen.init(random);
            SecretKey key = keygen.generateKey();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
            out.writeObject(key);
            out.close();
         }
         else
         {
            int mode;
            if (args[0].equals("-encrypt")) mode = Cipher.ENCRYPT_MODE;
            else mode = Cipher.DECRYPT_MODE;

            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            Key key = (Key) keyIn.readObject();
            keyIn.close();

            InputStream in = new FileInputStream(args[1]);
            OutputStream out = new FileOutputStream(args[2]);
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(mode, key);

            crypt(in, out, cipher);
            in.close();
            out.close();
         }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
      catch (GeneralSecurityException e)
      {
         e.printStackTrace();
      }
      catch (ClassNotFoundException e)
      {
         e.printStackTrace();
      }
   }

   /**
    * Uses a cipher to transform the bytes in an input stream and sends the transformed bytes to an
    * output stream.
    * @param in the input stream
    * @param out the output stream
    * @param cipher the cipher that transforms the bytes
    */
   public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException,
         GeneralSecurityException
   {
      int blockSize = cipher.getBlockSize();
      int outputSize = cipher.getOutputSize(blockSize);
      byte[] inBytes = new byte[blockSize];
      byte[] outBytes = new byte[outputSize];

      int inLength = 0;
      boolean more = true;
      while (more)
      {
         inLength = in.read(inBytes);
         if (inLength == blockSize)
         {
            int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
            out.write(outBytes, 0, outLength);
         }
         else more = false;
      }
      if (inLength > 0) outBytes = cipher.doFinal(inBytes, 0, inLength);
      else outBytes = cipher.doFinal();
      out.write(outBytes);
   }
}

   
    
    
  








Related examples in the same category

1.Basic symmetric encryption example
2.Encryption and decryption with AES/ECB/PKCS7Padding
3.Cipher with AESECBPKCS7Padding BC
4.Basic symmetric encryption example with CTR using DES
5.Basic symmetric encryption example with padding and CBC using DES
6.Basic symmetric encryption example with padding and ECB using DES
7.CBC using DES with an IV based on a nonce: a hypothetical message number
8.Example of using PBE with a PBEParameterSpec
9.Get Cipher Instance Blowfish
10.Message without tampering with MAC (DES), encryption AES in CTR mode
11.Example of using PBE without using a PBEParameterSpec
12.Getting the Bytes of a Generated Symmetric Key
13.Encryption and Decryption using Symmetric Keys
14.Encrypt a password
15.Cryptography Streams: True Mirror
16.Get the formats of the encoded bytes
17.Create an encrypted string for password
18.Cryptography Streams: URLDigest
19.Easy Blowfish encryption
20.This program tests the RSA cipher
21.Crypt demo
22.Encrypt User name
23.Crypt Utils
24.Encode a string using algorithm specified in web.xml and return the resulting encrypted password.
25.Encrypts the string along with salt, Decrypts the string and removes the salt