GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — SecureCompatibleEncryptionExamples/Java/SCEE.java at master · luke-park/SecureCompatibleEncryptionExamples Expand file tree/ Copy pathSCEE.javaMore file actions 103 lines (84 loc) · 5.19 KB / Copy pathSCEE.javaFile metadata and controls103 lines (84 loc) · 5.19 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 package scee; import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import java.util.Base64; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; public class SecureCompatibleEncryptionExamples { private final static String ALGORITHM_NAME = "AES/GCM/NoPadding"; private final static int ALGORITHM_NONCE_SIZE = 12; private final static int ALGORITHM_TAG_SIZE = 128; private final static int ALGORITHM_KEY_SIZE = 128; private final static String PBKDF2_NAME = "PBKDF2WithHmacSHA256"; private final static int PBKDF2_SALT_SIZE = 16; private final static int PBKDF2_ITERATIONS = 32767; public static String encryptString(String plaintext, String password) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { // Generate a 128-bit salt using a CSPRNG. SecureRandom rand = new SecureRandom(); byte[] salt = new byte[PBKDF2_SALT_SIZE]; rand.nextBytes(salt); // Create an instance of PBKDF2 and derive a key. PBEKeySpec pwSpec = new PBEKeySpec(password.toCharArray(), salt, PBKDF2_ITERATIONS, ALGORITHM_KEY_SIZE); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBKDF2_NAME); byte[] key = keyFactory.generateSecret(pwSpec).getEncoded(); // Encrypt and prepend salt. byte[] ciphertextAndNonce = encrypt(plaintext.getBytes(StandardCharsets.UTF_8), key); byte[] ciphertextAndNonceAndSalt = new byte[salt.length + ciphertextAndNonce.length]; System.arraycopy(salt, 0, ciphertextAndNonceAndSalt, 0, salt.length); System.arraycopy(ciphertextAndNonce, 0, ciphertextAndNonceAndSalt, salt.length, ciphertextAndNonce.length); // Return as base64 string. return Base64.getEncoder().encodeToString(ciphertextAndNonceAndSalt); } public static String decryptString(String base64CiphertextAndNonceAndSalt, String password) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException { // Decode the base64. byte[] ciphertextAndNonceAndSalt = Base64.getDecoder().decode(base64CiphertextAndNonceAndSalt); // Retrieve the salt and ciphertextAndNonce. byte[] salt = new byte[PBKDF2_SALT_SIZE]; byte[] ciphertextAndNonce = new byte[ciphertextAndNonceAndSalt.length - PBKDF2_SALT_SIZE]; System.arraycopy(ciphertextAndNonceAndSalt, 0, salt, 0, salt.length); System.arraycopy(ciphertextAndNonceAndSalt, salt.length, ciphertextAndNonce, 0, ciphertextAndNonce.length); // Create an instance of PBKDF2 and derive the key. PBEKeySpec pwSpec = new PBEKeySpec(password.toCharArray(), salt, PBKDF2_ITERATIONS, ALGORITHM_KEY_SIZE); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBKDF2_NAME); byte[] key = keyFactory.generateSecret(pwSpec).getEncoded(); // Decrypt and return result. return new String(decrypt(ciphertextAndNonce, key), StandardCharsets.UTF_8); } public static byte[] encrypt(byte[] plaintext, byte[] key) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { // Generate a 96-bit nonce using a CSPRNG. SecureRandom rand = new SecureRandom(); byte[] nonce = new byte[ALGORITHM_NONCE_SIZE]; rand.nextBytes(nonce); // Create the cipher instance and initialize. Cipher cipher = Cipher.getInstance(ALGORITHM_NAME); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(ALGORITHM_TAG_SIZE, nonce)); // Encrypt and prepend nonce. byte[] ciphertext = cipher.doFinal(plaintext); byte[] ciphertextAndNonce = new byte[nonce.length + ciphertext.length]; System.arraycopy(nonce, 0, ciphertextAndNonce, 0, nonce.length); System.arraycopy(ciphertext, 0, ciphertextAndNonce, nonce.length, ciphertext.length); return ciphertextAndNonce; } public static byte[] decrypt(byte[] ciphertextAndNonce, byte[] key) throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException { // Retrieve the nonce and ciphertext. byte[] nonce = new byte[ALGORITHM_NONCE_SIZE]; byte[] ciphertext = new byte[ciphertextAndNonce.length - ALGORITHM_NONCE_SIZE]; System.arraycopy(ciphertextAndNonce, 0, nonce, 0, nonce.length); System.arraycopy(ciphertextAndNonce, nonce.length, ciphertext, 0, ciphertext.length); // Create the cipher instance and initialize. Cipher cipher = Cipher.getInstance(ALGORITHM_NAME); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(ALGORITHM_TAG_SIZE, nonce)); // Decrypt and return result. return cipher.doFinal(ciphertext); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |