· 7 years ago · Nov 26, 2018, 06:58 PM
1public class ECC {
2
3 public static byte[] iv = new SecureRandom().generateSeed(16);
4
5
6 public static KeyPair generateECKeys() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
7
8 ECGenParameterSpec ecParamSpec = new ECGenParameterSpec("prime256v1");
9 KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDH", "SC");
10 kpg.initialize(ecParamSpec);
11 return kpg.generateKeyPair();
12 }
13
14 public static SecretKey generateSharedSecret(PrivateKey privateKey,
15 PublicKey publicKey) {
16 try {
17 KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH", "SC");
18 keyAgreement.init(privateKey);
19 keyAgreement.doPhase(publicKey, true);
20
21 SecretKey key = keyAgreement.generateSecret("AES");
22 return key;
23 } catch (InvalidKeyException | NoSuchAlgorithmException
24 | NoSuchProviderException e) {
25
26 e.printStackTrace();
27 return null;
28 }
29 }
30
31 public static String encryptString(SecretKey key, String plainText) {
32 try {
33
34
35 IvParameterSpec ivSpec = new IvParameterSpec(iv);
36
37 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING","SC");
38
39 cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
40 byte[] encrypted = cipher.doFinal(plainText.getBytes());
41
42 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
43 outputStream.write(ivSpec.getIV());
44 outputStream.write(encrypted);
45
46 return bytesToHex(outputStream.toByteArray());
47
48
49 } catch (NoSuchAlgorithmException | NoSuchProviderException
50 | NoSuchPaddingException | InvalidKeyException
51 | UnsupportedEncodingException
52 | IllegalBlockSizeException | BadPaddingException e) {
53 e.printStackTrace();
54 return null;
55 } catch (InvalidAlgorithmParameterException e) {
56 e.printStackTrace();
57 return null;
58 } catch (IOException e) {
59 e.printStackTrace();
60 return null;
61 }
62 }
63
64
65 public static String bytesToHex(byte[] data, int length) {
66 String digits = "0123456789ABCDEF";
67 StringBuffer buffer = new StringBuffer();
68
69 for (int i = 0; i != length; i++) {
70 int v = data[i] & 0xff;
71
72 buffer.append(digits.charAt(v >> 4));
73 buffer.append(digits.charAt(v & 0xf));
74 }
75
76 return buffer.toString();
77 }
78
79 public static String bytesToHex(byte[] data) {
80 return bytesToHex(data, data.length);
81 }
82
83 public static byte[] hexToBytes(String string) {
84 int length = string.length();
85 byte[] data = new byte[length / 2];
86 for (int i = 0; i < length; i += 2) {
87 data[i / 2] = (byte) ((Character.digit(string.charAt(i), 16) << 4) + Character
88 .digit(string.charAt(i + 1), 16));
89 }
90 return data;
91 }
92
93
94 public static PublicKey loadPublicKey (byte [] data) throws Exception
95 {
96
97 ECParameterSpec params = ECNamedCurveTable.getParameterSpec("prime256v1");
98 ECPublicKeySpec pubKey = new ECPublicKeySpec(
99 params.getCurve().decodePoint(data), params);
100 KeyFactory kf = KeyFactory.getInstance("ECDH", "SC");
101 return kf.generatePublic(pubKey);
102 }
103}