7

I am using in my app RSA cryptography. To store generated public key I convert it to String and then save it in database.

    Key publicKey=null;
    Key privateKey=null;

    KeyPair keyPair=RsaCrypto.getKeyPairRSA(1024);
    publicKey=keyPair.getPublic();
    privateKey=keyPair.getPrivate();



    String publicK=Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT);
    String privateK=Base64.encodeToString(privateKey.getEncoded(), Base64.DEFAULT);

I save Strings publicK and privateK. My problem is, when I want to encrypt/decrypt text with RSA and use my saved Key in String format I don't know how to convert it to Key.

public static String encrypt(Key publicKey, String inputText){
    byte[]encodedBytes=null;
    String encryptedText="";
    try {
        Cipher cipher=Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        encodedBytes=cipher.doFinal(inputText.getBytes());
    } catch (Exception e) {Log.e("Error", "RSA encryption error");  }

    encryptedText=Base64.encodeToString(encodedBytes, Base64.DEFAULT);
    return encryptedText;
}

Do you have any idea? Thanks a lot

xav
  • 5,452
  • 7
  • 48
  • 57
rgreso
  • 496
  • 1
  • 7
  • 19
  • Please, please, please do not store unencrypted private keys in your database. If you have to store private keys that's known as escrowing and you should be encrypting them with a symmetric key only known to your application. – KyleM Apr 06 '14 at 22:27
  • Thank you, yes I am thinking about it. Could you give me an advice, what is the best way to store a key in app?thanks – rgreso Apr 06 '14 at 22:56
  • use a password protected Java keystore. Look up Java keytool... – KyleM Apr 07 '14 at 00:56

2 Answers2

13

To convert publicK(String) to Public Key do as below :

byte[] keyBytes = Base64.decode(publicK.getBytes("utf-8"));
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey key = keyFactory.generatePublic(spec);

To convert privateK(String) to Private Key do as below :

byte[] keyBytes = Base64.decode(privateK.getBytes("utf-8"));
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey priv = fact.generatePrivate(keySpec);
Mark W
  • 5,824
  • 15
  • 59
  • 97
baldguy
  • 2,090
  • 1
  • 16
  • 25
  • thanks for answer. Eclipse shows me an error: The method decodeBase64(byte[]) is undefined for the type Base64. Could you help me with it please? thanks a lot – rgreso Apr 06 '14 at 22:43
  • Thanks for answer but it still shows an error, this time it wants another parameter: The method decode(byte[], int) in the type Base64 is not applicable for the arguments (byte[]) . Do you have any idea? Thanks a lot for your time – rgreso Apr 07 '14 at 06:22
  • You can use Base64.encodeBase64String(byte[]) from Apache Commons Codec project. – txedo Oct 01 '14 at 11:28
  • I'm trying to do the same thing but PrivateKey priv = fact.generatePrivate(keySpec); gets InvalidDeySpecException saying I must use RSAPublicKeySpec but that won't take a byte[] as a parm – Dean Blakely Oct 01 '15 at 21:37
  • try to change byte[] keyBytes = Base64.decode(privateK.getBytes("utf-8")); with byte[] keyBytes = Base64.decode(privateK, Base64.DEFAULT); – Danylo.Vus Jan 24 '18 at 19:18
0

I can only assume the line:

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear);

with "clear" was meant to be "keyBytes"

Robin Kanters
  • 5,018
  • 2
  • 20
  • 36
user3005339
  • 49
  • 2
  • 5