Encryption and hashing algorythms
This article shows three examples:
- AES encryption and decryption
- MD5 checksum hash
- SHA1 hash
AES encryption and decryption
public class Aes {
private static final String ALGO = "AES";
private static String keyString = "Q3aw7NxkWd9n3jD2";
public static void main(String[] args) {
String password = "my-password-is-secret";
String passwordEnc = null;
String passwordDec = null;
try {
passwordEnc = Aes.encrypt(password);
passwordDec = Aes.decrypt(passwordEnc);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Plain Text : " + password);
System.out.println("Encrypted Text : " + passwordEnc);
System.out.println("Decrypted Text : " + passwordDec);
}
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
// Key key = new SecretKeySpec(keyValue, ALGO);
Key key = new SecretKeySpec(keyString.getBytes(), ALGO);
return key;
}
}
MD5 checksum hash
public static String getMd5(String file) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream(file);
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
byte[] mdbytes = md.digest();
//convert the byte to hex format
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
// +0x100 is used for nicer representation
// b + 0 = b --toString--> "b"
// b + 100 = 10b --toString--> "10b" --substring--> "0b"
System.out.println("Digester:getMd5(): file: " + file + " MD5=" + sb.toString());
// System.out.println("Digest(in hex format):: " + sb.toString());
fis.close();
return sb.toString();
}
SHA1 hash
public static String getSha1(String file) {
FileInputStream fis;
StringBuffer sb;
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
fis = new FileInputStream(file);
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
byte[] mdbytes = md.digest();
sb = new StringBuffer("");
//convert the byte to hex format
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println("Digester:getSha1(): file: " + file + " SHA1=" + sb.toString());
fis.close();
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// System.out.println("Digest(in hex format):: " + sb.toString());
return "0";
}