This time I want to share Java source code in cryptography.
Hash string and file using MD5 :
While hash () method is used to convert string into MD5, hashFile () method is used to convert file into MD5. There are also instance to call them.
Hash string and file using SHA-1, SHA-256, SHA-512 :
HashSHA () methode is used to convert string into SHA, and hashFile () is used to convert file into SHA. SHA hashes that is used are SHA-1, SHA-256, SHA-512.
Encrypt and decrypt string with symmetric cryptography :
The example below is using DES encryption to encrypt and decrypt string message. We use two classes in the same package, SymmetricApp is the main class, and StringUtils class is the supporting class for converting cipherText in Hexadecimal.
Encrypt and decrypt file with symmetric cryptography :
In generateKey method, we generate DES key with the length of 56 to file. Key object is saved by using FileOutputStream to keyFile and the value is generated by getEncoded.
In readKeyFile method, we receive key object in file opened by FileInputStream and read by read method. The length of byte read is the same of length of key object.
In Encrypt method, we encrypt message with key object in array byte. We still use StringUtils class from above for converting encrypted message in Hexadecimal.
PC Media Magazine February 2014