|
| 1 | +package com.laytonsmith.PureUtilities.Common; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.ObjectInputStream; |
| 7 | +import java.io.ObjectOutputStream; |
| 8 | +import java.security.InvalidKeyException; |
| 9 | +import java.security.Key; |
| 10 | +import java.security.KeyPair; |
| 11 | +import java.security.KeyPairGenerator; |
| 12 | +import java.security.NoSuchAlgorithmException; |
| 13 | +import java.security.PrivateKey; |
| 14 | +import java.security.PublicKey; |
| 15 | +import java.util.Objects; |
| 16 | +import javax.crypto.BadPaddingException; |
| 17 | +import javax.crypto.Cipher; |
| 18 | +import javax.crypto.IllegalBlockSizeException; |
| 19 | +import javax.crypto.NoSuchPaddingException; |
| 20 | +import org.apache.commons.codec.binary.Base64; |
| 21 | + |
| 22 | +/** |
| 23 | + * Given a public/private key pair, this class uses RSA to encrypt/decrypt data. |
| 24 | + */ |
| 25 | +public class RSAEncrypt { |
| 26 | + |
| 27 | + /** |
| 28 | + * The RSA algorithm key. |
| 29 | + */ |
| 30 | + private static final String ALGORITHM = "RSA"; |
| 31 | + |
| 32 | + /** |
| 33 | + * Generates a new key, and stores the value in the RSA |
| 34 | + * |
| 35 | + * @param label The label that will be associated with the public key |
| 36 | + * @return |
| 37 | + */ |
| 38 | + public static RSAEncrypt generateKey(String label) { |
| 39 | + KeyPairGenerator keyGen; |
| 40 | + try { |
| 41 | + keyGen = KeyPairGenerator.getInstance(ALGORITHM); |
| 42 | + } catch (NoSuchAlgorithmException ex) { |
| 43 | + throw new RuntimeException(ex); |
| 44 | + } |
| 45 | + keyGen.initialize(1024); |
| 46 | + KeyPair key = keyGen.generateKeyPair(); |
| 47 | + RSAEncrypt enc = new RSAEncrypt(toString(key.getPrivate()), toString(key.getPublic(), label)); |
| 48 | + return enc; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Given a public key and a label, produces an ssh compatible rsa public key string. |
| 53 | + * |
| 54 | + * @param key |
| 55 | + * @param label |
| 56 | + * @return |
| 57 | + */ |
| 58 | + public static String toString(PublicKey key, String label) { |
| 59 | + Objects.requireNonNull(label); |
| 60 | + ByteArrayOutputStream pubBOS = new ByteArrayOutputStream(); |
| 61 | + try { |
| 62 | + ObjectOutputStream publicKeyOS = new ObjectOutputStream(pubBOS); |
| 63 | + publicKeyOS.writeObject(key); |
| 64 | + } catch (IOException ex) { |
| 65 | + throw new RuntimeException(ex); |
| 66 | + } |
| 67 | + String publicKey = Base64.encodeBase64String(pubBOS.toByteArray()); |
| 68 | + publicKey = "ssh-rsa " + publicKey + " " + label; |
| 69 | + return publicKey; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Given a private key, produces an ssh compatible rsa private key string. |
| 74 | + * |
| 75 | + * @param key |
| 76 | + * @return |
| 77 | + */ |
| 78 | + private static String toString(PrivateKey key) { |
| 79 | + ByteArrayOutputStream privBOS = new ByteArrayOutputStream(); |
| 80 | + ObjectOutputStream privateKeyOS; |
| 81 | + try { |
| 82 | + privateKeyOS = new ObjectOutputStream(privBOS); |
| 83 | + privateKeyOS.writeObject(key); |
| 84 | + } catch (IOException ex) { |
| 85 | + throw new RuntimeException(ex); |
| 86 | + } |
| 87 | + String privateKey = Base64.encodeBase64String(privBOS.toByteArray()); |
| 88 | + |
| 89 | + StringBuilder privBuilder = new StringBuilder(); |
| 90 | + privBuilder.append("-----BEGIN RSA PRIVATE KEY-----"); |
| 91 | + for(int i = 0; i < privateKey.length(); i++) { |
| 92 | + if(i % 64 == 0) { |
| 93 | + privBuilder.append(StringUtils.nl()); |
| 94 | + } |
| 95 | + privBuilder.append(privateKey.charAt(i)); |
| 96 | + } |
| 97 | + privBuilder.append(StringUtils.nl()).append("-----END RSA PRIVATE KEY-----").append(StringUtils.nl()); |
| 98 | + privateKey = privBuilder.toString(); |
| 99 | + return privateKey; |
| 100 | + } |
| 101 | + |
| 102 | + private PublicKey publicKey; |
| 103 | + private PrivateKey privateKey; |
| 104 | + private String label; |
| 105 | + |
| 106 | + /** |
| 107 | + * Creates a new RSAEncrypt object, based on the ssh compatible private/public key pair. Only one key needs to be |
| 108 | + * provided. If so, only those methods for the key provided will work. |
| 109 | + * |
| 110 | + * @param privateKey |
| 111 | + * @param publicKey |
| 112 | + * @throws IllegalArgumentException If the keys are not the correct type. They must be ssh compatible. |
| 113 | + */ |
| 114 | + public RSAEncrypt(String privateKey, String publicKey) throws IllegalArgumentException { |
| 115 | + if(privateKey != null) { |
| 116 | + //private key processing |
| 117 | + //replace all newlines with nothing |
| 118 | + privateKey = privateKey.replaceAll("\r", ""); |
| 119 | + privateKey = privateKey.replaceAll("\n", ""); |
| 120 | + //Remove the BEGIN/END tags |
| 121 | + privateKey = privateKey.replace("-----BEGIN RSA PRIVATE KEY-----", ""); |
| 122 | + privateKey = privateKey.replace("-----END RSA PRIVATE KEY-----", ""); |
| 123 | + ObjectInputStream privOIS; |
| 124 | + try { |
| 125 | + privOIS = new ObjectInputStream(new ByteArrayInputStream(Base64.decodeBase64(privateKey))); |
| 126 | + this.privateKey = (PrivateKey) privOIS.readObject(); |
| 127 | + } catch (IOException | ClassNotFoundException ex) { |
| 128 | + throw new RuntimeException(ex); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if(publicKey != null) { |
| 133 | + //public key processing |
| 134 | + String[] split = publicKey.split(" "); |
| 135 | + if(split.length != 3) { |
| 136 | + throw new IllegalArgumentException("Invalid public key passed in."); |
| 137 | + } |
| 138 | + if(!"ssh-rsa".equals(split[0])) { |
| 139 | + throw new IllegalArgumentException("Invalid public key type. Expecting ssh-rsa, but found \"" + split[0] + "\""); |
| 140 | + } |
| 141 | + this.label = split[2]; |
| 142 | + ObjectInputStream pubOIS; |
| 143 | + try { |
| 144 | + pubOIS = new ObjectInputStream(new ByteArrayInputStream(Base64.decodeBase64(split[1]))); |
| 145 | + this.publicKey = (PublicKey) pubOIS.readObject(); |
| 146 | + } catch (IOException | ClassNotFoundException ex) { |
| 147 | + throw new RuntimeException(ex); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Encrypts the data with the public key, which can be decrypted with the private key. This is only valid if the |
| 154 | + * public key was provided. |
| 155 | + * |
| 156 | + * @param data |
| 157 | + * @return |
| 158 | + */ |
| 159 | + public byte[] encryptWithPublic(byte[] data) { |
| 160 | + Objects.requireNonNull(publicKey); |
| 161 | + return crypt(data, publicKey, Cipher.ENCRYPT_MODE); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Encrypts the data with the private key, which can be decrypted with the public key. This is only valid if the |
| 166 | + * private key was provided. |
| 167 | + * |
| 168 | + * @param data |
| 169 | + * @return |
| 170 | + * @throws InvalidKeyException |
| 171 | + */ |
| 172 | + public byte[] encryptWithPrivate(byte[] data) throws InvalidKeyException { |
| 173 | + Objects.requireNonNull(privateKey); |
| 174 | + return crypt(data, privateKey, Cipher.ENCRYPT_MODE); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Decrypts the data with the public key, which will have been encrypted with the private key. This is only valid if |
| 179 | + * the public key was provided. |
| 180 | + * |
| 181 | + * @param data |
| 182 | + * @return |
| 183 | + */ |
| 184 | + public byte[] decryptWithPublic(byte[] data) { |
| 185 | + Objects.requireNonNull(publicKey); |
| 186 | + return crypt(data, publicKey, Cipher.DECRYPT_MODE); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Decrypts the data with the private key, which will have been encrypted with the public key. This is only valid if |
| 191 | + * the private key was provided. |
| 192 | + * |
| 193 | + * @param data |
| 194 | + * @return |
| 195 | + */ |
| 196 | + public byte[] decryptWithPrivate(byte[] data) { |
| 197 | + Objects.requireNonNull(privateKey); |
| 198 | + return crypt(data, privateKey, Cipher.DECRYPT_MODE); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Utility method that actually does the de/encrypting. |
| 203 | + * |
| 204 | + * @param data |
| 205 | + * @param key |
| 206 | + * @param cryptMode |
| 207 | + * @return |
| 208 | + */ |
| 209 | + private byte[] crypt(byte[] data, Key key, int cryptMode) { |
| 210 | + byte[] cipherValue = null; |
| 211 | + Cipher cipher; |
| 212 | + try { |
| 213 | + cipher = Cipher.getInstance(ALGORITHM); |
| 214 | + cipher.init(cryptMode, key); |
| 215 | + cipherValue = cipher.doFinal(data); |
| 216 | + } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException ex) { |
| 217 | + throw new RuntimeException(ex); |
| 218 | + } |
| 219 | + return cipherValue; |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Returns the private key string. |
| 224 | + * |
| 225 | + * @return |
| 226 | + */ |
| 227 | + public String getPrivateKey() { |
| 228 | + return toString(privateKey); |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Returns the public key string. |
| 233 | + * |
| 234 | + * @return |
| 235 | + */ |
| 236 | + public String getPublicKey() { |
| 237 | + return toString(publicKey, label); |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * Returns the label on the public key. |
| 242 | + * |
| 243 | + * @return |
| 244 | + */ |
| 245 | + public String getLabel() { |
| 246 | + return label; |
| 247 | + } |
| 248 | + |
| 249 | +} |
0 commit comments