|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package black.door.crypto; |
| 5 | + |
| 6 | +import java.io.Serializable; |
| 7 | +import java.io.UnsupportedEncodingException; |
| 8 | +import java.security.AlgorithmParameters; |
| 9 | +import java.security.NoSuchAlgorithmException; |
| 10 | +import java.security.SecureRandom; |
| 11 | +import java.security.spec.InvalidKeySpecException; |
| 12 | +import java.security.spec.KeySpec; |
| 13 | + |
| 14 | +import javax.crypto.Cipher; |
| 15 | +import javax.crypto.SecretKey; |
| 16 | +import javax.crypto.SecretKeyFactory; |
| 17 | +import javax.crypto.spec.IvParameterSpec; |
| 18 | +import javax.crypto.spec.PBEKeySpec; |
| 19 | +import javax.crypto.spec.SecretKeySpec; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author kAG0 |
| 23 | + * dead simple Hashing and Crypto |
| 24 | + * AES credit to erickson (stackoverflow.com/users/3474/erickson) |
| 25 | + */ |
| 26 | +public class Crypto { |
| 27 | + private byte[] secretPlain; |
| 28 | + private byte[] secretCipher = null; |
| 29 | + private KeySpec spec; |
| 30 | + private SecretKey secretKey; |
| 31 | + private Cipher cipher; |
| 32 | + private byte[] password; |
| 33 | + private byte[] salt = null; |
| 34 | + private byte[] iv; |
| 35 | + private boolean initAESDone; |
| 36 | + private int strength; |
| 37 | + |
| 38 | + public Crypto(){ |
| 39 | + clearPlain(); |
| 40 | + initAESDone = false; |
| 41 | + strength = 256; |
| 42 | + } |
| 43 | + |
| 44 | + public Crypto(byte[] input){ |
| 45 | + clearPlain(); |
| 46 | + initAESDone = false; |
| 47 | + this.secretPlain = input; |
| 48 | + strength = 256; |
| 49 | + } |
| 50 | + |
| 51 | + public void updateSecret(byte[] input){ |
| 52 | + this.secretPlain = input; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @return the strength |
| 57 | + */ |
| 58 | + public int getStrength() { |
| 59 | + return strength; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param strength the strength to set |
| 64 | + * @throws InvalidKeyLengthException |
| 65 | + */ |
| 66 | + public void setStrength(int strength) throws InvalidKeyLengthException { |
| 67 | + if(!(strength == 128 ||strength == 192||strength == 256)){ |
| 68 | + throw new InvalidKeyLengthException(strength + " bit keys are not supported."); |
| 69 | + } |
| 70 | + this.strength = strength; |
| 71 | + } |
| 72 | + |
| 73 | + public byte[] getIV(){ |
| 74 | + return iv; |
| 75 | + } |
| 76 | + |
| 77 | + public void setIV(byte[] iv){ |
| 78 | + this.iv=iv; |
| 79 | + } |
| 80 | + public void setSalt(byte[] salt){ |
| 81 | + this.salt=salt; |
| 82 | + } |
| 83 | + |
| 84 | + public byte[] getSalt(){ |
| 85 | + return salt; |
| 86 | + } |
| 87 | + |
| 88 | + public byte[] getSecretCipher(){ |
| 89 | + return secretCipher; |
| 90 | + } |
| 91 | + |
| 92 | + public void initAES() throws Exception{ |
| 93 | + if(password == null) |
| 94 | + throw new Exception("password is null"); |
| 95 | + SecretKeyFactory factory = null; |
| 96 | + try { |
| 97 | + factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 98 | + } catch (NoSuchAlgorithmException e) { |
| 99 | + // TODO Auto-generated catch block |
| 100 | + e.printStackTrace(); |
| 101 | + } |
| 102 | + if(salt == null) |
| 103 | + salt = SecureRandom.getSeed(8); |
| 104 | + String pass=null; |
| 105 | + try { |
| 106 | + pass = new String(password, "UTF-16"); |
| 107 | + } catch (UnsupportedEncodingException e) { |
| 108 | + System.err.println("for some reason UTF-16 is not a valid encoding"); |
| 109 | + e.printStackTrace(); |
| 110 | + } |
| 111 | + try { |
| 112 | + int maxBits = Cipher.getMaxAllowedKeyLength("AES"); |
| 113 | + if(maxBits < strength){ |
| 114 | + System.out.println("This system only supports up to " + maxBits + " bit encryption, using that strength."); |
| 115 | + strength = maxBits; |
| 116 | + } |
| 117 | + spec = new PBEKeySpec(pass.toCharArray(), salt, 65536, strength); |
| 118 | + } catch (NoSuchAlgorithmException e1) { |
| 119 | + e1.printStackTrace(); |
| 120 | + } |
| 121 | + SecretKey tmp = null; |
| 122 | + try { |
| 123 | + tmp = factory.generateSecret(spec); |
| 124 | + } catch (InvalidKeySpecException e) { |
| 125 | + // TODO Auto-generated catch block |
| 126 | + e.printStackTrace(); |
| 127 | + } |
| 128 | + secretKey = new SecretKeySpec(tmp.getEncoded(), "AES"); |
| 129 | + initAESDone = true; |
| 130 | + } |
| 131 | + |
| 132 | + public void initAES(byte[] password){ |
| 133 | + this.password = password; |
| 134 | + SecretKeyFactory factory = null; |
| 135 | + try { |
| 136 | + factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 137 | + } catch (NoSuchAlgorithmException e) { |
| 138 | + // TODO Auto-generated catch block |
| 139 | + e.printStackTrace(); |
| 140 | + } |
| 141 | + if(salt == null) |
| 142 | + salt = SecureRandom.getSeed(8); |
| 143 | + String pass=null; |
| 144 | + try { |
| 145 | + pass = new String(password, "UTF-16"); |
| 146 | + } catch (UnsupportedEncodingException e) { |
| 147 | + System.err.println("for some reason UTF-16 is not a valid encoding"); |
| 148 | + e.printStackTrace(); |
| 149 | + } |
| 150 | + try { |
| 151 | + int maxBits = Cipher.getMaxAllowedKeyLength("AES"); |
| 152 | + if(maxBits < strength){ |
| 153 | + System.out.println("This system only supports up to " + maxBits + " bit encryption, using that strength."); |
| 154 | + strength = maxBits; |
| 155 | + } |
| 156 | + spec = new PBEKeySpec(pass.toCharArray(), salt, 65536, strength); |
| 157 | + } catch (NoSuchAlgorithmException e1) { |
| 158 | + e1.printStackTrace(); |
| 159 | + } |
| 160 | + SecretKey tmp = null; |
| 161 | + try { |
| 162 | + tmp = factory.generateSecret(spec); |
| 163 | + } catch (InvalidKeySpecException e) { |
| 164 | + // TODO Auto-generated catch block |
| 165 | + e.printStackTrace(); |
| 166 | + } |
| 167 | + secretKey = new SecretKeySpec(tmp.getEncoded(), "AES"); |
| 168 | + initAESDone = true; |
| 169 | + } |
| 170 | + /** |
| 171 | + * clears all plaintext passwords and secrets. password, secret and initAES must all be set before re-using |
| 172 | + * @throws Exception |
| 173 | + */ |
| 174 | + public void doAESEncryption() throws Exception{ |
| 175 | + if(!initAESDone) |
| 176 | + initAES(); |
| 177 | + cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 178 | + //System.out.println(secretKey.getEncoded()); |
| 179 | + cipher.init(Cipher.ENCRYPT_MODE, secretKey); |
| 180 | + AlgorithmParameters params = cipher.getParameters(); |
| 181 | + iv = params.getParameterSpec(IvParameterSpec.class).getIV(); |
| 182 | + secretCipher = cipher.doFinal(secretPlain); |
| 183 | + clearPlain(); |
| 184 | + } |
| 185 | + |
| 186 | + public void doAESDecryption(byte[] salt, byte[] iv) throws Exception{ |
| 187 | + setSalt(salt); |
| 188 | + if(!initAESDone) |
| 189 | + initAES(); |
| 190 | + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 191 | + cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); |
| 192 | + secretPlain = cipher.doFinal(secretCipher); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @return the secretPlain |
| 197 | + */ |
| 198 | + public byte[] getSecretPlain() { |
| 199 | + return secretPlain; |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * @param secretPlain the secretPlain to set |
| 204 | + */ |
| 205 | + public void setSecretPlain(byte[] secretPlain) { |
| 206 | + this.secretPlain = secretPlain; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * @param secretCipher the secretCipher to set |
| 211 | + */ |
| 212 | + public void setSecretCipher(byte[] secretCipher) { |
| 213 | + this.secretCipher = secretCipher; |
| 214 | + } |
| 215 | + |
| 216 | + public static EncryptionResult getAESEncryption(byte[] secret, byte[] password, int strength) throws InvalidKeyLengthException{ |
| 217 | + Crypto agent = new Crypto(secret); |
| 218 | + agent.setStrength(strength); |
| 219 | + agent.initAES(password); |
| 220 | + try { |
| 221 | + agent.doAESEncryption(); |
| 222 | + } catch (Exception e) { |
| 223 | + e.printStackTrace(); |
| 224 | + } |
| 225 | + EncryptionResult result = new EncryptionResult(agent.getSecretCipher(), agent.getIV(), agent.getSalt()); |
| 226 | + return result; |
| 227 | + } |
| 228 | + |
| 229 | + public static byte[] getAESDecryption(byte[] secret, byte[] password, byte[] salt, byte[] iv, int strength) throws InvalidKeyLengthException{ |
| 230 | + Crypto agent = new Crypto(); |
| 231 | + agent.setStrength(strength); |
| 232 | + agent.setSalt(salt); |
| 233 | + agent.setSecretCipher(secret); |
| 234 | + agent.initAES(password); |
| 235 | + try { |
| 236 | + agent.doAESDecryption(salt, iv); |
| 237 | + } catch (Exception e) { |
| 238 | + // TODO Auto-generated catch block |
| 239 | + e.printStackTrace(); |
| 240 | + } |
| 241 | + byte[] secretResult = agent.getSecretPlain(); |
| 242 | + agent.clearPlain(); |
| 243 | + return secretResult; |
| 244 | + } |
| 245 | + |
| 246 | + public void clearPlain(){ |
| 247 | + secretPlain = null; |
| 248 | + password = null; |
| 249 | + spec = null; |
| 250 | + secretKey = null; |
| 251 | + cipher = null; |
| 252 | + } |
| 253 | + |
| 254 | + public static class EncryptionResult implements Serializable{ |
| 255 | + private byte[] output; |
| 256 | + private byte[] iv; |
| 257 | + private byte[] salt; |
| 258 | + /** |
| 259 | + * @param output |
| 260 | + * @param iv |
| 261 | + */ |
| 262 | + public EncryptionResult(byte[] output, byte[] iv, byte[] salt) { |
| 263 | + super(); |
| 264 | + this.output = output; |
| 265 | + this.iv = iv; |
| 266 | + this.salt = salt; |
| 267 | + } |
| 268 | + |
| 269 | + public EncryptionResult(byte[] simpleSerial){ |
| 270 | + int ivLength = simpleSerial[0]; |
| 271 | + int outputLength = simpleSerial.length - ivLength; |
| 272 | + iv = new byte[ivLength]; |
| 273 | + output = new byte[outputLength]; |
| 274 | + System.arraycopy(simpleSerial, 1, iv, 0, ivLength); |
| 275 | + System.arraycopy(simpleSerial, ivLength, output, 0, outputLength); |
| 276 | + } |
| 277 | + |
| 278 | + /** |
| 279 | + * needs testing |
| 280 | + * @return the encryption result as a byte array in the form (ivLength|iv|ciphertext) |
| 281 | + */ |
| 282 | + public byte[] simpleSerial(){ |
| 283 | + byte[] out = new byte[output.length + iv.length]; |
| 284 | + out[0] = (byte) iv.length; |
| 285 | + System.arraycopy(iv, 0, out, 1, iv.length); |
| 286 | + System.arraycopy(output, 0, out, iv.length, output.length); |
| 287 | + return out; |
| 288 | + } |
| 289 | + |
| 290 | + |
| 291 | + /** |
| 292 | + * @return the cipherText |
| 293 | + */ |
| 294 | + public byte[] getOutput() { |
| 295 | + return output; |
| 296 | + } |
| 297 | + |
| 298 | + /** |
| 299 | + * @return the iv |
| 300 | + */ |
| 301 | + public byte[] getIv() { |
| 302 | + return iv; |
| 303 | + } |
| 304 | + |
| 305 | + public byte[] getSalt(){ |
| 306 | + return salt; |
| 307 | + } |
| 308 | + } |
| 309 | + public class InvalidKeyLengthException extends Exception{ |
| 310 | + |
| 311 | + /** |
| 312 | + * |
| 313 | + */ |
| 314 | + private static final long serialVersionUID = 1L; |
| 315 | + |
| 316 | + /** |
| 317 | + * |
| 318 | + */ |
| 319 | + public InvalidKeyLengthException() { |
| 320 | + super(); |
| 321 | + // TODO Auto-generated constructor stub |
| 322 | + } |
| 323 | + |
| 324 | + /** |
| 325 | + * @param message |
| 326 | + * @param cause |
| 327 | + * @param enableSuppression |
| 328 | + * @param writableStackTrace |
| 329 | + */ |
| 330 | + public InvalidKeyLengthException(String message, Throwable cause, |
| 331 | + boolean enableSuppression, boolean writableStackTrace) { |
| 332 | + super(message, cause, enableSuppression, writableStackTrace); |
| 333 | + // TODO Auto-generated constructor stub |
| 334 | + } |
| 335 | + |
| 336 | + /** |
| 337 | + * @param message |
| 338 | + * @param cause |
| 339 | + */ |
| 340 | + public InvalidKeyLengthException(String message, Throwable cause) { |
| 341 | + super(message, cause); |
| 342 | + // TODO Auto-generated constructor stub |
| 343 | + } |
| 344 | + |
| 345 | + /** |
| 346 | + * @param message |
| 347 | + */ |
| 348 | + public InvalidKeyLengthException(String message) { |
| 349 | + super(message); |
| 350 | + // TODO Auto-generated constructor stub |
| 351 | + } |
| 352 | + |
| 353 | + /** |
| 354 | + * @param cause |
| 355 | + */ |
| 356 | + public InvalidKeyLengthException(Throwable cause) { |
| 357 | + super(cause); |
| 358 | + // TODO Auto-generated constructor stub |
| 359 | + } |
| 360 | + |
| 361 | + } |
| 362 | +} |
0 commit comments