· 6 years ago · Aug 13, 2019, 03:34 PM
1import java.io.BufferedReader;
2import java.io.FileReader;
3import java.io.IOException;
4import java.security.GeneralSecurityException;
5import java.security.KeyFactory;
6import java.security.Security;
7import java.security.interfaces.RSAPublicKey;
8import java.security.spec.PKCS8EncodedKeySpec;
9import java.security.spec.X509EncodedKeySpec;
10
11import javax.crypto.Cipher;
12import javax.crypto.SecretKey;
13
14import org.apache.commons.codec.binary.Base64;
15import java.security.interfaces.RSAPrivateKey;
16import org.bouncycastle.util.encoders.Hex;
17
18
19public class BouncyCastleExample {
20 public static void main(String[] args) throws IOException, GeneralSecurityException {
21 Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
22
23 byte[] input = "test".getBytes();
24 Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
25
26 RSAPublicKey key = getPublicKey("C:\\Users\\gszymanowski\\Downloads\\OpenSSL\\bin\\public.pem");
27
28 cipher.init(Cipher.ENCRYPT_MODE, key);
29
30 byte[] plainText = cipher.doFinal(input);
31 System.out.println("plain : " + new String(plainText));
32 byte[] encoded = Hex.encode(plainText);
33 System.out.println(new String(encoded));
34
35 RSAPrivateKey priv = getPrivateKey("C:\\Users\\gszymanowski\\Downloads\\OpenSSL\\bin\\privkey.pem");
36
37 cipher.init(Cipher.DECRYPT_MODE, priv);
38
39 byte[] result = cipher.doFinal(encoded);
40
41 //System.out.println(new String(result));
42 }
43
44 private static String getKey(String filename) throws IOException {
45 // Read key from file
46 String strKeyPEM = "";
47 BufferedReader br = new BufferedReader(new FileReader(filename));
48 String line;
49 while ((line = br.readLine()) != null) {
50 strKeyPEM += line + "\n";
51 }
52 br.close();
53 return strKeyPEM;
54 }
55
56 public static RSAPublicKey getPublicKey(String filename) throws IOException, GeneralSecurityException {
57 String publicKeyPEM = getKey(filename);
58 return getPublicKeyFromString(publicKeyPEM);
59 }
60
61 public static RSAPublicKey getPublicKeyFromString(String key) throws IOException, GeneralSecurityException {
62 String publicKeyPEM = key;
63 publicKeyPEM = publicKeyPEM.replace("-----BEGIN PUBLIC KEY-----\n", "");
64 publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", "");
65 byte[] encoded = Base64.decodeBase64(publicKeyPEM);
66 KeyFactory kf = KeyFactory.getInstance("RSA");
67 RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(new X509EncodedKeySpec(encoded));
68 return pubKey;
69 }
70
71 public static byte[] encrypt(String message) throws IOException, GeneralSecurityException {
72 byte[] input = message.getBytes();
73 Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
74
75 RSAPublicKey key = getPublicKey("C:\\Users\\gszymanowski\\Downloads\\OpenSSL\\bin\\public.pem");
76
77 cipher.init(Cipher.ENCRYPT_MODE, key);
78
79 byte[] plainText = cipher.doFinal(input);
80 System.out.println("plain : " + new String(plainText));
81 return plainText;
82 }
83
84 public static String decrypt(String strToDecrypt, SecretKey key) {
85 try {
86 Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
87 cipher.init(Cipher.DECRYPT_MODE, key);
88
89 return new String(cipher.doFinal(org.apache.commons.codec.binary.Hex.decodeHex(strToDecrypt.toCharArray())));
90 } catch (Exception e) {
91 System.out.println("Error while decrypting: " + e.toString());
92 }
93 return null;
94 }
95
96 public static RSAPrivateKey getPrivateKey(String filename) throws IOException, GeneralSecurityException {
97 String privateKeyPEM = getKey(filename);
98 return getPrivateKeyFromString(privateKeyPEM);
99 }
100 public static RSAPrivateKey getPrivateKeyFromString(String key) throws IOException, GeneralSecurityException {
101 String privateKeyPEM = key;
102 privateKeyPEM = privateKeyPEM.replace("-----BEGIN PRIVATE KEY-----\n", "");
103 privateKeyPEM = privateKeyPEM.replace("-----END PRIVATE KEY-----", "");
104 byte[] encoded = Base64.decodeBase64(privateKeyPEM);
105 KeyFactory kf = KeyFactory.getInstance("RSA");
106 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
107 RSAPrivateKey privKey = (RSAPrivateKey) kf.generatePrivate(keySpec);
108 return privKey;
109 }
110
111
112
113}