· 5 years ago · Mar 31, 2020, 01:04 AM
1import org.bouncycastle.jce.provider.BouncyCastleProvider;
2
3import javax.crypto.*;
4import javax.crypto.spec.IvParameterSpec;
5import javax.crypto.spec.SecretKeySpec;
6import java.io.FileInputStream;
7import java.io.FileNotFoundException;
8import java.io.IOException;
9import java.security.*;
10import java.security.cert.CertificateException;
11import java.security.cert.CertificateFactory;
12import java.security.cert.X509Certificate;
13
14public class Server {
15 public static void main(String args[]){
16 //Security.setProperty("crypto.policy", "unlimited");
17
18 /*try {
19 Security.addProvider(new BouncyCastleProvider());
20 CertificateFactory certFactory = CertificateFactory.getInstance("X.509", "BC");
21
22 X509Certificate certificate = (X509Certificate) certFactory.generateCertificate(new FileInputStream("Baeldung.cer"));
23
24 char[] keystorePassword = "password".toCharArray();
25 char[] keyPassword = "password".toCharArray();
26
27 KeyStore keystore = KeyStore.getInstance("PKCS12");
28 keystore.load(new FileInputStream("Baeldung.p12"), keystorePassword);
29 PrivateKey key = (PrivateKey) keystore.getKey("baeldung", keyPassword);
30 }
31 catch (CertificateException e){
32 System.out.println(e);
33 } catch (FileNotFoundException e) {
34 e.printStackTrace();
35 } catch (IOException e) {
36 e.printStackTrace();
37 } catch (UnrecoverableKeyException e) {
38 e.printStackTrace();
39 } catch (NoSuchProviderException e) {
40 e.printStackTrace();
41 } catch (KeyStoreException e) {
42 e.printStackTrace();
43 }*/
44
45 try {
46 byte [] key = {1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4};
47 SecretKey secretKey = new SecretKeySpec(key, 0, key.length, "AES");
48
49 //nonce
50 byte[] bytesForIV=new byte[16];
51 SecureRandom sr = new SecureRandom();
52 sr.nextBytes(bytesForIV);
53 IvParameterSpec iv = new IvParameterSpec(bytesForIV);
54
55 Cipher c = Cipher.getInstance("AES/CTR/PKCS5Padding");
56 } catch (NoSuchPaddingException e) {
57 e.printStackTrace();
58 } catch (NoSuchAlgorithmException e) {
59 e.printStackTrace();
60 }
61 }
62
63 public byte[] encrypt(byte[] plaintext, Cipher c, SecretKey secretKey, IvParameterSpec iv){
64 byte [] result = null;
65 try {
66 c.init(Cipher.ENCRYPT_MODE, secretKey, iv);
67 result = c.doFinal(plaintext);
68 } catch (InvalidAlgorithmParameterException e) {
69 e.printStackTrace();
70 } catch (InvalidKeyException e) {
71 e.printStackTrace();
72 } catch (BadPaddingException e) {
73 e.printStackTrace();
74 } catch (IllegalBlockSizeException e) {
75 e.printStackTrace();
76 }
77 /*OutputStream ot = out;
78 CipherOutputStream cos = new CipherOutputStream(ot, c);
79 int text;
80 while((text=System.in.read())!=-1){
81 cos.write((byte)text);
82 cos.flush();
83 }*/
84
85 return result;
86 }
87
88 public byte[] decrypt(byte[] ciphertext, Cipher c, SecretKey secretKey, IvParameterSpec iv){
89 byte [] result = null;
90 try {
91 c.init(Cipher.DECRYPT_MODE, secretKey, iv);
92 result = c.doFinal(ciphertext );
93 } catch (InvalidAlgorithmParameterException e) {
94 e.printStackTrace();
95 } catch (InvalidKeyException e) {
96 e.printStackTrace();
97 } catch (BadPaddingException e) {
98 e.printStackTrace();
99 } catch (IllegalBlockSizeException e) {
100 e.printStackTrace();
101 }
102 /*CipherInputStream cis = new CipherInputStream(is, c);
103 int text;
104 while((text=cis.read())!=-1) {
105 System.out.print((char)text);
106 }*/
107 return result;
108 }
109
110}