1 package org.openkilda.security;
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;
13 import javax.crypto.Cipher;
15 import org.apache.log4j.Logger;
36 _log.info(
"ReadKeyFromFile called with keyFileName : " + keyFileName);
37 InputStream in = null;
38 ObjectInputStream oin = null;
40 in = Thread.currentThread().getContextClassLoader().getResourceAsStream(keyFileName);
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));
49 return fact.generatePrivate(
new RSAPrivateKeySpec(m, e));
54 }
catch (Exception e) {
55 _log.fatal(
"Exception in readKeyFromFile : " + e.getMessage());
56 throw new RuntimeException(
"Spurious serialisation error", e);
76 @SuppressWarnings(
"restriction")
77 public static String
rsaEncrypt(final byte[] text, final String file_des) throws Exception {
79 Cipher cipher = Cipher.getInstance(
"RSA");
80 cipher.init(Cipher.ENCRYPT_MODE, pubKey);
81 byte[]
data = cipher.doFinal(text);
87 return new sun.misc.BASE64Encoder().encode(
data);
100 public static String
rsaDecrypt(
final String text,
final String file_des)
throws Exception {
102 Cipher cipher = Cipher.getInstance(
"RSA");
103 cipher.init(Cipher.DECRYPT_MODE, priKey);
104 byte[]
data = cipher.doFinal(
new sun.misc.BASE64Decoder().decodeBuffer(text));
106 return new String(
data,
"UTF8");
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)