package com.common.test;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class CryptoTest {
private static String strPlain = "Test Message";
/**
* Convert byte array to hexadecimal
*
* @param buffer
* Buffer with bytes
* @return String with hexadecimal data
*/
public static String ByteArrayToHexString(byte[] ba) {
return ByteArrayToHexString(ba, "");
}
public static String ByteArrayToHexString(byte[] ba, String split) {
if (ba == null || ba.length == 0) {
return null;
}
StringBuilder sb = new StringBuilder();
for (final byte b : ba) {
sb.append(String.format("%02X", b & 0xff));
sb.append(split);
}
return sb.toString().substring(0, sb.toString().length()).toUpperCase();
}
/**
* Convert hexadecimal string to byte array
*
* @param String
* with hexadecimal data
* @return byte array
*/
public static byte[] HexStringToByteArray(String hex) {
if (hex == null || hex.length() == 0) {
return null;
}
byte[] ba = new byte[hex.length() / 2];
for (int i = 0; i < ba.length; i++) {
ba[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
}
return ba;
}
/* AES START -------------------------------------------------------------------------------- */
private static byte [] aesEncrypt(byte [] key, byte [] plain, IvParameterSpec iv) {
byte [] result = null;
try {
Cipher encCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec encSecretKey = new SecretKeySpec(key, 0, key.length, "AES");
encCipher.init(Cipher.ENCRYPT_MODE, encSecretKey, iv);
result = encCipher.doFinal(plain);
} catch(Exception ex) {
ex.printStackTrace();
}
return result;
}
private static byte [] aesDecrypt(byte [] key, byte [] plain, IvParameterSpec iv) {
byte [] result = null;
try {
Cipher decCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec encSecretKey = new SecretKeySpec(key, 0, key.length, "AES");
decCipher.init(Cipher.DECRYPT_MODE, encSecretKey, iv);
result = decCipher.doFinal(plain);
} catch(Exception ex) {
ex.printStackTrace();
}
return result;
}
private static void AesTest() {
try {
//byte [] key = new byte[16];
//byte [] key = new byte[24];
byte [] key = new byte[24];
byte [] byteAesIv = new byte[16];
byte [] encrypted = null;
String hexEncrypted = null;
byte [] decrypted = null;
byte [] plain = strPlain.getBytes();
String strResult = null;
IvParameterSpec aesIv = new IvParameterSpec(byteAesIv);
encrypted = aesEncrypt(key, plain, aesIv);
hexEncrypted = ByteArrayToHexString(encrypted);
System.out.println(hexEncrypted);
decrypted = aesDecrypt(key, encrypted, aesIv);
strResult = new String(decrypted);
System.out.println(strResult);
} catch(Exception ex) {
ex.printStackTrace();
}
}
/* AES END -------------------------------------------------------------------------------- */
/* DESede START -------------------------------------------------------------------------------- */
private static byte [] desEdeEncrypt(byte [] key, byte [] plain, IvParameterSpec iv) {
byte [] result = null;
try {
Cipher encCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec encSecretKey = new SecretKeySpec(key, 0, key.length, "DESede");
encCipher.init(Cipher.ENCRYPT_MODE, encSecretKey, iv);
result = encCipher.doFinal(plain);
} catch(Exception ex) {
ex.printStackTrace();
}
return result;
}
private static byte [] desEdeDecrypt(byte [] key, byte [] plain, IvParameterSpec iv) {
byte [] result = null;
try {
Cipher decCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec encSecretKey = new SecretKeySpec(key, 0, key.length, "DESede");
decCipher.init(Cipher.DECRYPT_MODE, encSecretKey, iv);
result = decCipher.doFinal(plain);
} catch(Exception ex) {
ex.printStackTrace();
}
return result;
}
private static void DesEdeTest() {
try {
byte [] key = new byte[24];
byte [] byteDesEdeIv = new byte[8];
byte [] encrypted = null;
String hexEncrypted = null;
byte [] decrypted = null;
byte [] plain = strPlain.getBytes();
String strResult = null;
IvParameterSpec desEdeIv = new IvParameterSpec(byteDesEdeIv);
encrypted = desEdeEncrypt(key, plain, desEdeIv);
hexEncrypted = ByteArrayToHexString(encrypted);
System.out.println(hexEncrypted);
decrypted = desEdeDecrypt(key, encrypted, desEdeIv);
strResult = new String(decrypted);
System.out.println(strResult);
} catch(Exception ex) {
ex.printStackTrace();
}
}
/* DESede END -------------------------------------------------------------------------------- */
private static void HashTest() {
try {
// MD2, MD5
// SHA-1, SHA-256, SHA-384, SHA-512
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(strPlain.getBytes());
byte [] byteDigest = md.digest();
String hexDigest = ByteArrayToHexString(byteDigest);
System.out.println(hexDigest);
} catch(Exception ex) {
ex.printStackTrace();
}
}
private static void HmacTest() {
try {
byte [] hmacKey = new byte[16];
SecretKeySpec macSecretKey = new SecretKeySpec(hmacKey, 0, hmacKey.length, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(macSecretKey);
mac.update(strPlain.getBytes());
byte [] byteMac = mac.doFinal();
String hexDigest = ByteArrayToHexString(byteMac);
System.out.println(hexDigest);
} catch(Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
try {
AesTest();
DesEdeTest();
HashTest();
HmacTest();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}