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 | ||
oktatas:programozas:java:java_titkositas [2023/02/27 22:17] admin [Coder osztály készítése] |
oktatas:programozas:java:java_titkositas [2023/08/24 20:22] (aktuális) admin [Titkosítás Java nyelven] |
||
---|---|---|---|
Sor 4: | Sor 4: | ||
* **Szerző:** Sallai András | * **Szerző:** Sallai András | ||
- | * Copyright (c) Sallai András, 2014 | + | * Copyright (c) 2014, Sallai András |
- | * Licenc: GNU Free Documentation License 1.3 | + | * Szerkesztve: 2014, 2023 |
- | * Web: http://szit.hu | + | * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]] |
+ | * Web: https://szit.hu | ||
===== Szöveg titkosítása ===== | ===== Szöveg titkosítása ===== | ||
Sor 192: | Sor 194: | ||
} | } | ||
} | } | ||
+ | </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); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
</code> | </code> | ||