Java tutorial
/* * Project: root * * File Created at 2014-05-22 * * Copyright 2012 Greenline.com Corporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Greenline Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Greenline.com. */ package com.anteam.demo.codec.cipher.symmetric; import org.apache.commons.codec.binary.Base64; import org.testng.annotations.Test; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec; import java.security.spec.AlgorithmParameterSpec; public class DESTest { @Test public void testDES() throws Exception { SecretKey key = KeyGenerator.getInstance("DES").generateKey(); // for CBC; must be 8 bytes byte[] initVector = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C }; AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector); Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding"); Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding"); m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec); m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec); byte[] clearText = "?????;(*)*$(R%*PDSJF>XJIPUFIWE(*#*&$)@#*" .getBytes(); System.out.println(clearText.length); byte[] encryptedText = m_encrypter.doFinal(clearText); System.out.println(encryptedText.length); byte[] decryptedText = m_decrypter.doFinal(encryptedText); System.out.println(decryptedText.length); System.out.println(new String(clearText)); System.out.println(new String(encryptedText)); System.out.println(new String(decryptedText)); } public static String bytesToString(byte[] bytes) { char[] chars = new char[bytes.length]; for (int i = 0; i < bytes.length; i++) { chars[i] = (char) bytes[i]; } return new String(chars); } public static byte[] stringToBytes(String string) { char[] chars = string.toCharArray(); byte[] bytes = new byte[chars.length]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) chars[i]; } return bytes; } @Test public void testDESede() { System.out.println("##############################################"); byte[] plainBytes = ("???fasdddddddddddddddddddddddddddddd" + "fasdfasdfaseui\t\n\b\r" + "\\?iupiufafsadkjfljdjfoiejwapiru???;(*)*$(R%*PDSJF>XJIPUFIWE(*#*&$)@#*") .getBytes(); byte[] encryptedBytes = testDESedeEn(new String(plainBytes)); System.out.println("##############################################"); String bts = DESTest.bytesToString(encryptedBytes); System.out.println("bts:" + bts); System.out.println("##############################################"); byte[] stb = DESTest.stringToBytes(bts); System.out.println(stb.length); System.out.println("##############################################"); byte[] decryptedBytes = testDESedeDe(encryptedBytes); System.out.println("stb?:" + new String(decryptedBytes)); System.out.println("##############################################"); String encryptedText = Base64.encodeBase64String(encryptedBytes); System.out.println("Base64:" + encryptedText); encryptedBytes = Base64.decodeBase64(encryptedText); System.out.println("##############################################"); decryptedBytes = testDESedeDe(encryptedBytes); System.out.println("B64?:" + new String(decryptedBytes)); System.out.println("##############################################"); } public byte[] testDESedeEn(String plainText) { try { // Create an array to hold the key byte[] encryptKey = "This is a test DESede key".getBytes(); // Create a DESede key spec from the key DESedeKeySpec spec = new DESedeKeySpec(encryptKey); // Get the secret key factor for generating DESede keys SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); // Generate a DESede SecretKey object SecretKey theKey = keyFactory.generateSecret(spec); // Create a DESede Cipher Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); // Create an initialization vector (necessary for CBC mode) IvParameterSpec IvParameters = new IvParameterSpec( new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C }); // Initialize the cipher and put it into encrypt mode cipher.init(Cipher.ENCRYPT_MODE, theKey, IvParameters); return cipher.doFinal(plainText.getBytes()); } catch (Exception exc) { exc.printStackTrace(); } return null; } public byte[] testDESedeDe(byte[] encryptedText) { try { // Create an array to hold the key byte[] encryptKey = "This is a test DESede key".getBytes(); // Create a DESede key spec from the key DESedeKeySpec spec = new DESedeKeySpec(encryptKey); // Get the secret key factor for generating DESede keys SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); // Generate a DESede SecretKey object SecretKey theKey = keyFactory.generateSecret(spec); // Create a DESede Cipher Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); // Create an initialization vector (necessary for CBC mode) IvParameterSpec IvParameters = new IvParameterSpec( new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C }); // Initialize the cipher and put it into encrypt mode cipher.init(Cipher.DECRYPT_MODE, theKey, IvParameters); return cipher.doFinal(encryptedText); } catch (Exception exc) { exc.printStackTrace(); } return null; } }