|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Dominik Schadow, [email protected] |
| 3 | + * |
| 4 | + * This file is part of the Java Security project. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | +
|
| 18 | + * Modified by PetrS by removal of external dependencies |
| 19 | + */ |
| 20 | +package buggy_java; |
| 21 | + |
| 22 | +import javax.crypto.BadPaddingException; |
| 23 | +import javax.crypto.Cipher; |
| 24 | +import javax.crypto.IllegalBlockSizeException; |
| 25 | +import javax.crypto.NoSuchPaddingException; |
| 26 | +import javax.crypto.spec.IvParameterSpec; |
| 27 | +import javax.crypto.spec.SecretKeySpec; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.io.UnsupportedEncodingException; |
| 31 | +import java.security.*; |
| 32 | +import java.security.cert.CertificateException; |
| 33 | + |
| 34 | +/** |
| 35 | + * Symmetric encryption sample with plain Java. Loads the AES key from the sample keystore, encrypts and decrypts |
| 36 | + * sample text with it. |
| 37 | + * Note that the <code>INITIALIZATION_VECTOR</code> is not stored. One possibility to store it is to prepend it to |
| 38 | + * the encrypted |
| 39 | + * message with a delimiter (all in Base64 encoding): <code>Base64(IV) + DELIMITER + Base64(ENCRYPTED MESSAGE)</code> |
| 40 | + * <p/> |
| 41 | + * Uses Google Guava to Base64 print the encrypted message as readable format. |
| 42 | + * |
| 43 | + * @author Dominik Schadow |
| 44 | + */ |
| 45 | +public class buggyj { |
| 46 | + private static final String ALGORITHM = "AES/CBC/PKCS5Padding"; |
| 47 | + private static final String KEYSTORE_PATH = "/samples.ks"; |
| 48 | + /** |
| 49 | + * Non-secret initialization vector with 16 bytes (publicly exchanged between participants), may be a random |
| 50 | + * number changed every time or a counter. |
| 51 | + */ |
| 52 | + private static final byte[] INITIALIZATION_VECTOR = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3}; |
| 53 | + |
| 54 | + public static void main(String[] args) { |
| 55 | + buggyj ses = new buggyj(); |
| 56 | + final String initialText = "AES encryption sample text"; |
| 57 | + final char[] keystorePassword = "samples".toCharArray(); |
| 58 | + final String keyAlias = "symmetric-sample"; |
| 59 | + final char[] keyPassword = "symmetric-sample".toCharArray(); |
| 60 | + final IvParameterSpec iv = new IvParameterSpec(INITIALIZATION_VECTOR); |
| 61 | + |
| 62 | + try { |
| 63 | + KeyStore ks = ses.loadKeystore(KEYSTORE_PATH, keystorePassword); |
| 64 | + Key key = ses.loadKey(ks, keyAlias, keyPassword); |
| 65 | + SecretKeySpec secretKeySpec = new SecretKeySpec(key.getEncoded(), "AES"); |
| 66 | + byte[] ciphertext = ses.encrypt(secretKeySpec, iv, initialText); |
| 67 | + byte[] plaintext = ses.decrypt(secretKeySpec, iv, ciphertext); |
| 68 | + |
| 69 | + ses.printReadableMessages(initialText, ciphertext, plaintext); |
| 70 | + |
| 71 | + byte[] secretKey = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 72 | + SecretKeySpec spec = new SecretKeySpec(secretKey, "AES"); |
| 73 | + Cipher aes = Cipher.getInstance("AES"); |
| 74 | + aes.init(Cipher.ENCRYPT_MODE, spec); |
| 75 | + byte[] encrypted = aes.doFinal(initialText.getBytes()); |
| 76 | + |
| 77 | + } catch (NoSuchPaddingException | NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException | |
| 78 | + KeyStoreException | CertificateException | UnrecoverableKeyException | |
| 79 | + InvalidAlgorithmParameterException | |
| 80 | + InvalidKeyException | IOException ex) { |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private KeyStore loadKeystore(String keystorePath, char[] keystorePassword) throws KeyStoreException, |
| 85 | + CertificateException, NoSuchAlgorithmException, IOException { |
| 86 | + InputStream keystoreStream = getClass().getResourceAsStream(keystorePath); |
| 87 | + |
| 88 | + KeyStore ks = KeyStore.getInstance("JCEKS"); |
| 89 | + ks.load(keystoreStream, keystorePassword); |
| 90 | + |
| 91 | + return ks; |
| 92 | + } |
| 93 | + |
| 94 | + private Key loadKey(KeyStore ks, String keyAlias, char[] keyPassword) throws KeyStoreException, |
| 95 | + UnrecoverableKeyException, NoSuchAlgorithmException { |
| 96 | + if (!ks.containsAlias(keyAlias)) { |
| 97 | + throw new UnrecoverableKeyException("Secret key " + keyAlias + " not found in keystore"); |
| 98 | + } |
| 99 | + |
| 100 | + return ks.getKey(keyAlias, keyPassword); |
| 101 | + } |
| 102 | + |
| 103 | + private byte[] encrypt(SecretKeySpec secretKeySpec, IvParameterSpec initialVector, String initialText) |
| 104 | + throws NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException, BadPaddingException, |
| 105 | + IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException { |
| 106 | + Cipher cipher = Cipher.getInstance(ALGORITHM); |
| 107 | + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, initialVector); |
| 108 | + return cipher.doFinal(initialText.getBytes("UTF-8")); |
| 109 | + } |
| 110 | + |
| 111 | + private byte[] decrypt(SecretKeySpec secretKeySpec, IvParameterSpec initialVector, byte[] ciphertext) |
| 112 | + throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, |
| 113 | + InvalidAlgorithmParameterException, InvalidKeyException { |
| 114 | + Cipher cipher = Cipher.getInstance(ALGORITHM); |
| 115 | + cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, initialVector); |
| 116 | + return cipher.doFinal(ciphertext); |
| 117 | + } |
| 118 | + |
| 119 | + private void printReadableMessages(String initialText, byte[] ciphertext, byte[] plaintext) { |
| 120 | + } |
| 121 | + |
| 122 | + private void desIsBetter() throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException { |
| 123 | + Cipher c = Cipher.getInstance("DESede/ECB/PKCS5Padding"); |
| 124 | + byte[] cipherText = c.doFinal("My plaintext".getBytes()); |
| 125 | + } |
| 126 | +} |
0 commit comments