import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.util.Base64; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class Coder { public static String encrypt(String clearText, Key key) { String result = ""; try { result = tryEncrypt(clearText, key); } catch (NoSuchAlgorithmException e) { System.err.println("Hiba! Nincs ilyen algoritmus!"); }catch(NoSuchPaddingException e) { System.err.println("Hiba! Nincs ilyen kitöltő!"); }catch(InvalidKeyException e) { System.err.println("Hiba! Érvénytelen kulcs!"); }catch(UnsupportedEncodingException e) { System.err.println("Hiba! Nem támogatott kódolás!"); }catch(IllegalBlockSizeException e) { System.out.println("Hiba! Illegális blokkméret!"); }catch(BadPaddingException e) { System.err.println("Hiba! Rossz kitöltő!"); } return result; } public static String tryEncrypt(String clearText, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] plainTextByteArray = clearText.getBytes("UTF-8"); byte[] secretTextByteArray = cipher.doFinal(plainTextByteArray); String secretText = new String(Base64.getEncoder().encode(secretTextByteArray)); return secretText; } public static String decrypt(String secretText, Key key) { String result = ""; try { result = tryDecrypt(secretText, key); } catch (NoSuchAlgorithmException e) { System.err.println("Hiba! Nincs ilyen algoritmus!"); }catch(NoSuchPaddingException e) { System.err.println("Hiba! Nincs ilyen kitöltő!"); }catch(InvalidKeyException e) { System.err.println("Hiba! Érvénytelen kulcs!"); }catch(UnsupportedEncodingException e) { System.err.println("Hiba! Nem támogatott kódolás!"); }catch(IllegalBlockSizeException e) { System.out.println("Hiba! Illegális blokkméret!"); }catch(BadPaddingException e) { System.err.println("Hiba! Rossz kitöltő!"); } return result; } public static String tryDecrypt(String secretText, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] encryptedByteArray = Base64.getDecoder().decode(secretText); byte[] decryptedByteArray = cipher.doFinal(encryptedByteArray); String clearText = new String(decryptedByteArray, "UTF-8"); return clearText; } public static SecretKeySpec generateKey() throws NoSuchAlgorithmException { SecretKeySpec key; String algoritmus = "AES"; int kulcsmeret = 128; KeyGenerator keyGenerator = KeyGenerator.getInstance(algoritmus); keyGenerator.init(kulcsmeret); SecretKey secretKey = keyGenerator.generateKey(); byte[] raw = secretKey.getEncoded(); key = new SecretKeySpec(raw, algoritmus); return key; } }