· 7 years ago · Apr 04, 2018, 12:44 PM
1package mkri;
2
3import java.math.BigInteger;
4import java.security.InvalidKeyException;
5import java.security.KeyFactory;
6import java.security.KeyPair;
7import java.security.KeyPairGenerator;
8import java.security.KeyStore.PrivateKeyEntry;
9import java.security.MessageDigest;
10import java.security.NoSuchAlgorithmException;
11import java.security.PrivateKey;
12import java.security.Provider;
13import java.security.PublicKey;
14import java.security.SecureRandom;
15import java.security.Signature;
16import java.security.SignatureException;
17import java.security.spec.InvalidKeySpecException;
18import java.security.spec.KeySpec;
19import java.security.spec.X509EncodedKeySpec;
20import java.util.Base64;
21import java.util.Base64.*;
22
23import javax.crypto.Cipher;
24import javax.crypto.KeyAgreement;
25import javax.crypto.KeyGenerator;
26import javax.crypto.NoSuchPaddingException;
27import javax.crypto.SecretKey;
28import javax.crypto.interfaces.DHPublicKey;
29import javax.crypto.spec.DHParameterSpec;
30import javax.crypto.spec.DHPublicKeySpec;
31import javax.crypto.spec.SecretKeySpec;
32
33public class ProtocolSec {
34
35 private Signature sign;
36 private KeyPair keyPair;
37
38 private KeyAgreement aKeyAgree;
39 private byte[] pass;
40
41 private Decoder dec = Base64.getDecoder();
42 private Encoder enc = Base64.getEncoder();
43
44
45 /**
46 * Generuje RSA
47 * @throws NoSuchAlgorithmException
48 * @throws InvalidKeyException
49 */
50 public void genRSA() throws NoSuchAlgorithmException, InvalidKeyException{
51
52 //--------------- Generovani a inicializace RSA algoritmu pro podpis, verze 2048 b, SHA512----
53
54
55 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
56 keyGen.initialize(2048, new SecureRandom());
57
58 keyPair = keyGen.generateKeyPair();
59
60 sign = Signature.getInstance("SHA512withRSA");
61
62 sign.initSign(keyPair.getPrivate());
63
64
65 //---------------------------------------------
66 }
67
68 public String getPublicKey(){
69
70 return enc.encodeToString(keyPair.getPublic().getEncoded());
71 }
72
73 /**
74 * Podepisuje data
75 * Vstup i vystup kodovan v BASE64!
76 * @param data
77 * @return
78 * @throws SignatureException
79 */
80 public String sign(String data) throws SignatureException{
81
82 //--------------- RSA podepsani, stup: data----
83 sign.update(dec.decode(data));
84 byte[] signature = sign.sign();
85 //---------------------------------------------
86 return enc.encodeToString(signature);
87 }
88
89 /**
90 * generovani Diffie Hellman
91 * Vraci verejny klic v BASE64!
92 */
93 public String genDH() throws Exception{
94
95 byte[] pubA = null;
96 //--------------- Diffie helman, inicializace a vraceni verejneho klice----
97 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
98 aKeyAgree = KeyAgreement.getInstance("DH");
99 KeyPair aPair = keyGen.generateKeyPair();
100
101 aKeyAgree.init(aPair.getPrivate());
102
103 pubA = aPair.getPublic().getEncoded();
104
105 //-------------------------------------------------------------------------
106 return enc.encodeToString(pubA);
107 }
108
109 /**
110 * inicializuje algoritmus DH klicem PK protistrany,
111 * vypocte sdilene tajemstvi
112 * vzpocte hash a vrati jej
113 * Vstup kodovan BASE64!
114 * @param pk
115 * @throws Exception
116 */
117 public void procDH(String pk) throws Exception {
118
119
120 byte[] bpk = dec.decode(pk.getBytes());
121 //--------------- Diffie Hellman , hash SHA1 -----------------------
122
123 PublicKey publicKey = KeyFactory.getInstance("DH").generatePublic(new X509EncodedKeySpec(bpk));
124 aKeyAgree.doPhase(publicKey, true);
125 MessageDigest hash = MessageDigest.getInstance("SHA1");
126 pass = hash.digest(hash.digest(aKeyAgree.generateSecret()));
127 //Hash ulozit do pass - pouziva se na sifrovani
128
129 //-------------------------------------------------------------------
130 }
131
132 /**
133 * SifrovánÃ, vstup nenà kódován BASE64!
134 * Výstup kódován BASE64!
135 * @param data
136 * @return
137 * @throws Exception
138 */
139 public String encrypt(String data) throws Exception{
140
141 byte[] encAes = null;
142
143 //KeyGenerator kg = KeyGenerator.getInstance("AES");
144 //kg.init(128);
145 //SecretKey key = kg.generateKey();
146
147 String passw = "pass";
148 byte[] key = passw.getBytes();
149
150 SecretKeySpec sks = new SecretKeySpec(key, "AES");
151
152 //--------------- inicializace, sifrovani AES-128, mod ECB, padding PKCS5Padding----
153 Cipher a1 = Cipher.getInstance("AES/ECB/PKCS5Padding");
154
155 a1.init(Cipher.ENCRYPT_MODE, sks);
156
157 encAes = a1.doFinal(data.getBytes());
158 //-------------------------------------------------------------------
159 return enc.encodeToString(encAes);
160 }
161
162 /**
163 * DesifrovánÃ, vstup je kódován BASE64!
164 * Výstup kódován BASE64!
165 * @param data
166 * @return
167 * @throws Exception
168 */
169 public String decrypt(String data) throws Exception{
170
171 byte[] decAes = null;
172
173 String passww = "pass";
174 byte[] key = passww.getBytes();
175
176 SecretKeySpec sks1 = new SecretKeySpec(key, "AES");
177 //--------------- inicializace, desifrovani AES-128, mod ECB, padding PKCS5Padding---
178 Cipher a2 = Cipher.getInstance("AES/ECB/PKCS5Padding");
179
180
181
182 a2.init(Cipher.DECRYPT_MODE, sks1);
183 decAes = a2.doFinal(dec.decode(data));
184
185 //-------------------------------------------------------------------
186 return new String(decAes);
187 }
188
189
190
191}