A kiválasztott változat és az aktuális verzió közötti különbségek a következők.
Előző változat mindkét oldalon Előző változat Következő változat | Előző változat Következő változat Következő változat mindkét oldalon | ||
oktatas:programozas:java:java_titkositas [2023/02/27 22:15] admin [Függvények] |
oktatas:programozas:java:java_titkositas [2023/02/27 22:29] admin [Coder osztály készítése] |
||
---|---|---|---|
Sor 169: | Sor 169: | ||
} | } | ||
| | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | |||
+ | Használat: | ||
+ | |||
+ | <code java App.java> | ||
+ | import javax.crypto.spec.SecretKeySpec; | ||
+ | |||
+ | public class App { | ||
+ | public static void main(String[] args) throws Exception { | ||
+ | System.out.println("--Titkostás--"); | ||
+ | |||
+ | SecretKeySpec key = Coder.generateKey(); | ||
+ | |||
+ | String secretText = Coder.encrypt("titok", key); | ||
+ | System.out.println(secretText); | ||
+ | |||
+ | String clearText = Coder.decrypt(secretText, key); | ||
+ | System.out.println(clearText); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ==== Kulcsgenerálás másként ==== | ||
+ | |||
+ | <code java Coder.java> | ||
+ | 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; | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Használat: | ||
+ | |||
+ | <code java App.java> | ||
+ | import java.security.Key; | ||
+ | |||
+ | import javax.crypto.spec.SecretKeySpec; | ||
+ | |||
+ | public class App { | ||
+ | public static void main(String[] args) throws Exception { | ||
+ | System.out.println("--Titkostás--"); | ||
+ | |||
+ | String key = "1234567890123456"; // 128 bites kulcs | ||
+ | Key aeskey = new SecretKeySpec(key.getBytes(), "AES"); | ||
+ | |||
+ | |||
+ | String secretText = Coder.encrypt("titok", aeskey); | ||
+ | System.out.println(secretText); | ||
+ | |||
+ | String clearText = Coder.decrypt(secretText, aeskey); | ||
+ | System.out.println(clearText); | ||
+ | |||
+ | } | ||
} | } | ||