Last active
July 28, 2017 10:23
-
-
Save bitsnaps/b42c618eae5377dfe307895f680e207f to your computer and use it in GitHub Desktop.
Simple AES/DES Encryption & Decryption with no extra library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Separate file for simple AES encrypt/decrypt | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import javax.xml.bind.DatatypeConverter; | |
public class Encryptor { | |
public static String encrypt(String key, String initVector, String value) { | |
try { | |
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8")); | |
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); | |
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); | |
byte[] encrypted = cipher.doFinal(value.getBytes()); | |
System.out.println("encrypted string: " | |
+ DatatypeConverter.printBase64Binary(encrypted)); | |
return DatatypeConverter.printBase64Binary(encrypted); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return null; | |
} | |
public static String decrypt(String key, String initVector, String encrypted) { | |
try { | |
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8")); | |
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); | |
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); | |
// byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted)); | |
byte[] original = cipher.doFinal(DatatypeConverter.parseBase64Binary(encrypted)); | |
return new String(original); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return null; | |
} | |
public static void main(String[] args) { | |
String key = "Bar12345Bar12345"; // 128 bit key | |
String initVector = "RandomInitVector"; // 16 bytes IV | |
System.out.println(decrypt(key, initVector, | |
encrypt(key, initVector, "Hello World"))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.crypto.Cipher; | |
import javax.crypto.SecretKey; | |
import javax.crypto.KeyGenerator; | |
import javax.xml.bind.DatatypeConverter; | |
class SimpleEncryption { | |
private Cipher cipher; | |
private SecretKey secretKey; | |
private byte[] cipherBytes; | |
public SimpleEncryption(String algo, int size) throws Exception{ | |
KeyGenerator keyGenerator = KeyGenerator.getInstance(algo); | |
keyGenerator.init(size); | |
secretKey = keyGenerator.generateKey(); | |
cipher = Cipher.getInstance(algo); | |
} | |
public String encrypt(String clearText) throws Exception{ | |
byte[] clearTextBytes = clearText.getBytes("UTF8"); | |
cipher.init(Cipher.ENCRYPT_MODE, secretKey); | |
cipherBytes = cipher.doFinal(clearTextBytes); | |
return new String(cipherBytes, "UTF8"); | |
} | |
public String decrypt(String text) throws Exception{ | |
cipher.init(Cipher.DECRYPT_MODE, secretKey); | |
byte[] decryptedBytes = cipher.doFinal(cipherBytes); | |
return new String(decryptedBytes, "UTF8"); | |
} | |
public String getCipherString(){ | |
return DatatypeConverter.printBase64Binary(cipherBytes); | |
} | |
private static void printAlgo(String clearText, String algo, int size) throws Exception{ | |
SimpleEncryption des = new SimpleEncryption(algo, size); | |
System.out.println( algo+": "); | |
String cipherText = des.encrypt(clearText); | |
String decryptedText = des.decrypt(cipherText); | |
System.out.println( "Before encryption: " + clearText); | |
System.out.println( "After encryption:" + des.getCipherString()); | |
System.out.println( "After decryption: " + decryptedText); | |
} | |
public static void main(String[] args) throws Exception{ | |
String clearText = "text to encrypt"; | |
printAlgo(clearText, "DES", 56); | |
printAlgo(clearText, "AES", 128); | |
printAlgo(clearText, "DESede", 168); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment