· 8 years ago · Dec 20, 2017, 05:12 AM
1package com.lheinrich.test;
2
3import javax.crypto.Cipher;
4import javax.crypto.KeyGenerator;
5import javax.crypto.SecretKey;
6import java.io.*;
7import java.nio.charset.StandardCharsets;
8import java.security.*;
9import java.util.Arrays;
10import javax.crypto.KeyAgreement;
11import javax.crypto.spec.SecretKeySpec;
12
13/**
14 * Copyright (c) 2017 Lennart Heinrich (www.lheinrich.com)
15 */
16public class Crypter {
17
18 public static byte[] encrypt(Key key, byte[] value, String algorithm) {
19 try {
20 Cipher cipher = Cipher.getInstance(algorithm);
21 cipher.init(Cipher.ENCRYPT_MODE, key);
22 return cipher.doFinal(value);
23 } catch (Exception ex) {
24 ex.printStackTrace();
25 return null;
26 }
27 }
28
29 public static byte[] decrypt(Key key, byte[] value, String algorithm) {
30 try {
31 Cipher cipher = Cipher.getInstance(algorithm);
32 cipher.init(Cipher.DECRYPT_MODE, key);
33 return cipher.doFinal(value);
34 } catch (Exception ex) {
35 ex.printStackTrace();
36 return null;
37 }
38 }
39
40 public static SecretKey generateKey(int keysize) {
41 try {
42 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
43 keyGenerator.init(keysize);
44 return keyGenerator.generateKey();
45 } catch (NoSuchAlgorithmException ex) {
46 ex.printStackTrace();
47 return null;
48 }
49 }
50
51 public static KeyPair generateKeyPair(int keysize) {
52 try {
53 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
54 keyPairGenerator.initialize(keysize);
55 return keyPairGenerator.generateKeyPair();
56 } catch (NoSuchAlgorithmException ex) {
57 ex.printStackTrace();
58 return null;
59 }
60 }
61
62 public static KeyPair generateKeyPair(int keysize, String algorithm) {
63 try {
64 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
65 keyPairGenerator.initialize(keysize);
66 return keyPairGenerator.generateKeyPair();
67 } catch (NoSuchAlgorithmException ex) {
68 ex.printStackTrace();
69 return null;
70 }
71 }
72
73 public static SecretKey generateEC(PrivateKey privateKey, PublicKey publicKey, String hashAlgorithm, ECKeysize keysize) {
74 try {
75 KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH");
76 keyAgreement.init(privateKey);
77 keyAgreement.doPhase(publicKey, true);
78 byte[] hash = Crypter.hash(hashAlgorithm, keyAgreement.generateSecret());
79 byte[] rawKey = Arrays.copyOfRange(hash, hash.length - keysize.getSize(), hash.length);
80 return new SecretKeySpec(rawKey, 0, rawKey.length, "AES");
81 } catch (InvalidKeyException | NoSuchAlgorithmException ex) {
82 ex.printStackTrace();
83 return null;
84 }
85 }
86
87 public static byte[] toByteArray(Serializable object) throws IOException {
88 ByteArrayOutputStream bos = new ByteArrayOutputStream();
89 ObjectOutput out = new ObjectOutputStream(bos);
90 out.writeObject(object);
91 return bos.toByteArray();
92 }
93
94 public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
95 ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
96 ObjectInput in = new ObjectInputStream(bis);
97 return in.readObject();
98 }
99
100 public static String hash(String algorithm, String value) {
101 try {
102 MessageDigest md = MessageDigest.getInstance(algorithm);
103 md.update(value.getBytes(StandardCharsets.UTF_8));
104 byte[] digest = md.digest();
105 return String.format("%064x", new java.math.BigInteger(1, digest));
106 } catch (NoSuchAlgorithmException ex) {
107 ex.printStackTrace();
108 return null;
109 }
110 }
111
112 public static byte[] hash(String algorithm, byte[] value) {
113 try {
114 MessageDigest md = MessageDigest.getInstance(algorithm);
115 md.update(value);
116 return md.digest();
117 } catch (NoSuchAlgorithmException ex) {
118 ex.printStackTrace();
119 return null;
120 }
121 }
122
123 public enum ECKeysize {
124 LOW(16), NORMAL(24), HIGH(32);
125
126 private int size;
127
128 ECKeysize(int size) {
129 this.size = size;
130 }
131
132 public int getSize() {
133 return size;
134 }
135 }
136}