Last active
August 26, 2018 14:55
-
-
Save jay7793/5f790e71c33834fba62c274d22d5fcbf to your computer and use it in GitHub Desktop.
Revisions
-
jay7793 revised this gist
Aug 26, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -109,7 +109,7 @@ public static String decrypt(String strToDecrypt) { public static void main(String args[]) { final String strToEncrypt = "My text to encrypt"; final String strPssword = "secret key"; AES.setKey(strPssword); AES.encrypt(strToEncrypt.trim()); -
jay7793 created this gist
Aug 26, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,128 @@ import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.AlgorithmParameterSpec; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; import javax.print.DocFlavor.STRING; import org.apache.commons.codec.binary.Base64; public class AES { private static SecretKeySpec secretKey; private static byte[] key; private static String decryptedString; private static String encryptedString; public static void setKey(String myKey) { MessageDigest sha = null; try { key = myKey.getBytes("UTF-8"); System.out.println(key.length); sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); // use only first 128 bit System.out.println(key.length); System.out.println(new String(key, "UTF-8")); secretKey = new SecretKeySpec(key, "AES"); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String getDecryptedString() { return decryptedString; } public static void setDecryptedString(String decryptedString) { AES.decryptedString = decryptedString; } public static String getEncryptedString() { return encryptedString; } public static void setEncryptedString(String encryptedString) { AES.encryptedString = encryptedString; } public static String encrypt(String strToEncrypt) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8")))); } catch (Exception e) { System.out.println("Error while encrypting: " + e.toString()); } return null; } public static String decrypt(String strToDecrypt) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, secretKey); setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)))); } catch (Exception e) { System.out.println("Error while decrypting: " + e.toString()); } return null; } public static void main(String args[]) { final String strToEncrypt = "My text to encrypt"; final String strPssword = "encryptor key"; AES.setKey(strPssword); AES.encrypt(strToEncrypt.trim()); System.out.println("String to Encrypt: " + strToEncrypt); System.out.println("Encrypted: " + AES.getEncryptedString()); final String strToDecrypt = AES.getEncryptedString(); AES.decrypt(strToDecrypt.trim()); System.out.println("String To Decrypt : " + strToDecrypt); System.out.println("Decrypted : " + AES.getDecryptedString()); } }