Last active
August 29, 2015 14:22
-
-
Save kedarmhaswade/7398a49fe182d196282a to your computer and use it in GitHub Desktop.
symmetric encryption in Java using 1.8
This file contains hidden or 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
// based on http://syntx.io/basic-symmetric-encryption-example-with-java/ | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.security.InvalidAlgorithmParameterException; | |
import java.security.InvalidKeyException; | |
import java.security.Key; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.sql.SQLException; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.spec.IvParameterSpec; | |
import java.util.Base64; | |
public class SymmetricEncryptionUtility { | |
public final static Key generateKey() throws NoSuchAlgorithmException{ | |
KeyGenerator kg = KeyGenerator.getInstance("AES"); | |
SecureRandom random = new SecureRandom(); | |
kg.init(random); | |
return kg.generateKey(); | |
} | |
public static final String encrypt(final String message, final Key key, final IvParameterSpec iv) throws IllegalBlockSizeException, | |
BadPaddingException, NoSuchAlgorithmException, | |
NoSuchPaddingException, InvalidKeyException, | |
UnsupportedEncodingException, InvalidAlgorithmParameterException { | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
cipher.init(Cipher.ENCRYPT_MODE,key,iv); | |
byte[] stringBytes = message.getBytes(); | |
byte[] raw = cipher.doFinal(stringBytes); | |
return Base64.getEncoder().encodeToString(raw); | |
} | |
public static final String decrypt(final String encrypted,final Key key, final IvParameterSpec iv) throws InvalidKeyException, | |
NoSuchAlgorithmException, NoSuchPaddingException, | |
IllegalBlockSizeException, BadPaddingException, IOException, InvalidAlgorithmParameterException { | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
cipher.init(Cipher.DECRYPT_MODE, key,iv); | |
byte[] raw = Base64.getDecoder().decode(encrypted); | |
byte[] stringBytes = cipher.doFinal(raw); | |
String clearText = new String(stringBytes, "UTF8"); | |
return clearText; | |
} | |
public static void main(String[] args) throws Exception{ | |
Key k = SymmetricEncryptionUtility.generateKey(); | |
SecureRandom random = new SecureRandom(); | |
IvParameterSpec iv = new IvParameterSpec(random.generateSeed(16)); | |
String clearText = "hello world 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789"; | |
System.out.println("Clear Text Length:" + clearText.length()); | |
String encryptedString = encrypt(clearText,k,iv); | |
System.out.println("Encrypted String:" + encryptedString); | |
System.out.println("enc string length: " + encryptedString.length()); | |
System.out.println("Decrypted String:"+decrypt(encryptedString,k,iv)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment