Open Kilda Java Documentation
RSAEncryptionDescription.java
Go to the documentation of this file.
1 package org.openkilda.security;
2 
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.ObjectInputStream;
7 import java.math.BigInteger;
8 import java.security.Key;
9 import java.security.KeyFactory;
10 import java.security.spec.RSAPrivateKeySpec;
11 import java.security.spec.RSAPublicKeySpec;
12 
13 import javax.crypto.Cipher;
14 
15 import org.apache.log4j.Logger;
16 
17 
22 
24  private static final Logger _log = Logger.getLogger(RSAEncryptionDescription.class);
25 
26 
34  // Return the saved key
35  public static Key readKeyFromFile(final String keyFileName) throws IOException {
36  _log.info("ReadKeyFromFile called with keyFileName : " + keyFileName);
37  InputStream in = null;
38  ObjectInputStream oin = null;
39  try {
40  in = Thread.currentThread().getContextClassLoader().getResourceAsStream(keyFileName);
41  if (in != null) {
42  oin = new ObjectInputStream(new BufferedInputStream(in));
43  BigInteger m = (BigInteger) oin.readObject();
44  BigInteger e = (BigInteger) oin.readObject();
45  KeyFactory fact = KeyFactory.getInstance("RSA");
46  if (keyFileName.startsWith("public")) {
47  return fact.generatePublic(new RSAPublicKeySpec(m, e));
48  } else {
49  return fact.generatePrivate(new RSAPrivateKeySpec(m, e));
50  }
51  } else {
52  return null;
53  }
54  } catch (Exception e) {
55  _log.fatal("Exception in readKeyFromFile : " + e.getMessage());
56  throw new RuntimeException("Spurious serialisation error", e);
57  } finally {
58  if (oin != null) {
59  oin.close();
60  }
61  if (in != null) {
62  in.close();
63  }
64  }
65  }
66 
75  // Use this PublicKey object to initialize a Cipher and encrypt some data
76  @SuppressWarnings("restriction")
77  public static String rsaEncrypt(final byte[] text, final String file_des) throws Exception {
78  Key pubKey = readKeyFromFile("public.key");
79  Cipher cipher = Cipher.getInstance("RSA");
80  cipher.init(Cipher.ENCRYPT_MODE, pubKey);
81  byte[] data = cipher.doFinal(text);
82  /*
83  * StringBuilder sb = new StringBuilder(); for (byte b : data) { sb.append((char)b); }
84  * return sb.toString();
85  */
86  // return new sun.misc.BASE64Encoder().encode(data);
87  return new sun.misc.BASE64Encoder().encode(data);
88 
89  }
90 
99  // Use this PublicKey object to initialize a Cipher and decrypt some data
100  public static String rsaDecrypt(final String text, final String file_des) throws Exception {
101  Key priKey = readKeyFromFile("private.key");
102  Cipher cipher = Cipher.getInstance("RSA");
103  cipher.init(Cipher.DECRYPT_MODE, priKey);
104  byte[] data = cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(text));
105  // return new String(data);
106  return new String(data, "UTF8");
107  }
108 }
109 
static Key readKeyFromFile(final String keyFileName)
static String rsaDecrypt(final String text, final String file_des)
static String rsaEncrypt(final byte[] text, final String file_des)