|
| 1 | +/* |
| 2 | + * Copyright 2021 Cloudera, Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.cloudera.utils; |
| 18 | + |
| 19 | +import javax.crypto.BadPaddingException; |
| 20 | +import javax.crypto.Cipher; |
| 21 | +import javax.crypto.IllegalBlockSizeException; |
| 22 | +import javax.crypto.NoSuchPaddingException; |
| 23 | +import javax.crypto.spec.SecretKeySpec; |
| 24 | +import javax.xml.bind.DatatypeConverter; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.security.InvalidAlgorithmParameterException; |
| 27 | +import java.security.InvalidKeyException; |
| 28 | +import java.security.NoSuchAlgorithmException; |
| 29 | + |
| 30 | +public class Protect { |
| 31 | + |
| 32 | + |
| 33 | +// KeyGenerator keyGenerator = null; |
| 34 | + String key = null; |
| 35 | + SecretKeySpec keySpec = null; |
| 36 | +// SecretKey secretKey = null; |
| 37 | + Cipher cipher = null; |
| 38 | + final String initialVector = "ae280ckq"; |
| 39 | + |
| 40 | + final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); |
| 41 | + |
| 42 | + // Converts byte array to hex string |
| 43 | + // From: http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java |
| 44 | + public static String bytesToHex(byte[] bytes) { |
| 45 | + char[] hexChars = new char[bytes.length * 2]; |
| 46 | + for ( int j = 0; j < bytes.length; j++ ) { |
| 47 | + int v = bytes[j] & 0xFF; |
| 48 | + hexChars[j * 2] = hexArray[v >>> 4]; |
| 49 | + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; |
| 50 | + } |
| 51 | + return new String(hexChars); |
| 52 | + } |
| 53 | + |
| 54 | + public Protect(String key) { |
| 55 | + this.key = key; |
| 56 | + try { |
| 57 | + /* |
| 58 | + Create key spec seeded with a user defined key. |
| 59 | + */ |
| 60 | + keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Blowfish"); |
| 61 | + |
| 62 | + /** |
| 63 | + * Create an instance of cipher mentioning the name of algorithm |
| 64 | + * - Blowfish |
| 65 | + */ |
| 66 | + cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding"); |
| 67 | + } catch (NoSuchPaddingException ex) { |
| 68 | + System.out.println(ex); |
| 69 | + } catch (NoSuchAlgorithmException ex) { |
| 70 | + System.out.println(ex); |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param plainText |
| 77 | + * @return cipherBytes |
| 78 | + */ |
| 79 | + public String encrypt(String plainText) { |
| 80 | + |
| 81 | + String rtn = null; |
| 82 | + byte[] encoding = new byte[0]; |
| 83 | + try { |
| 84 | + cipher.init(Cipher.ENCRYPT_MODE, keySpec, new javax.crypto.spec.IvParameterSpec(initialVector.getBytes())); |
| 85 | + encoding = cipher.doFinal(plainText.getBytes()); |
| 86 | + } catch (InvalidKeyException|InvalidAlgorithmParameterException|IllegalBlockSizeException|BadPaddingException e) { |
| 87 | + e.printStackTrace(); |
| 88 | + throw new RuntimeException(e); |
| 89 | + } |
| 90 | + |
| 91 | + rtn = DatatypeConverter.printBase64Binary(encoding); |
| 92 | + |
| 93 | +// System.out.println("-- Encrypted -----------"); |
| 94 | +// System.out.println("Base64:\t " + rtn); |
| 95 | +// System.out.println("HEX:\t " + bytesToHex(encoding)); |
| 96 | + |
| 97 | + return rtn; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param text |
| 102 | + * @return plainText |
| 103 | + */ |
| 104 | + public String decrypt(String text) throws RuntimeException { |
| 105 | + String plainText = null; |
| 106 | + byte[] ciphertext = DatatypeConverter.parseBase64Binary(text); |
| 107 | + |
| 108 | + // Decrypt |
| 109 | + try { |
| 110 | + cipher.init(Cipher.DECRYPT_MODE, keySpec, new javax.crypto.spec.IvParameterSpec(initialVector.getBytes())); |
| 111 | + } catch (InvalidKeyException e) { |
| 112 | + e.printStackTrace(); |
| 113 | + } catch (InvalidAlgorithmParameterException e) { |
| 114 | + e.printStackTrace(); |
| 115 | + } |
| 116 | + byte[] message = new byte[0]; |
| 117 | + try { |
| 118 | + message = cipher.doFinal(ciphertext); |
| 119 | + } catch (IllegalBlockSizeException e) { |
| 120 | + throw new RuntimeException(e); |
| 121 | + } catch (BadPaddingException e) { |
| 122 | + throw new RuntimeException(e); |
| 123 | + } |
| 124 | + plainText = new String(message); |
| 125 | + |
| 126 | +// System.out.println("-- Decrypted -----------"); |
| 127 | +// System.out.println("HEX:\t " + bytesToHex(message)); |
| 128 | +// System.out.println("PLAIN:\t " + plainText); |
| 129 | + |
| 130 | + return plainText; |
| 131 | + } |
| 132 | + |
| 133 | + public static void main(String[] args) { |
| 134 | + Protect blowfishAlgorithm = new Protect("hello2"); |
| 135 | + String textToEncrypt = "Blowfish Algorithm"; |
| 136 | + System.out.println("Text before Encryption: " + textToEncrypt); |
| 137 | + String cipherText = blowfishAlgorithm.encrypt(textToEncrypt); |
| 138 | + System.out.println("Cipher Text: " + cipherText); |
| 139 | + System.out.println("Text after Decryption: " + blowfishAlgorithm.decrypt(cipherText)); |
| 140 | + } |
| 141 | + |
| 142 | +} |
| 143 | + |
0 commit comments