0
private static String decrypt(String cipherString, PrivateKey key) {
    byte[] dectyptedText = null;
    byte[] stringText = null;
    try {
        // get an RSA cipher object and print the provider
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        //chiper init in encrypt mode
        cipher.init(Cipher.ENCRYPT_MODE, key);
        //tried to get bytes out of encrypted string
        stringText = cipher.doFinal(cipherString.getBytes());
        // decrypt the text using the private key
        cipher.init(Cipher.DECRYPT_MODE, key);

        dectyptedText = cipher.doFinal(stringText);

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return new String(dectyptedText);
}

I want to convert the cipher text into bytes generated by the encryptor to a string and store in a database. Then get the string and decrypt it whenever its needed. Is there anyone that could help me solving the issue I'm having?

mhu
  • 17,720
  • 10
  • 62
  • 93
  • You can't do that; you need to encrypt a symmetric key and use that instead. – SLaks Mar 06 '14 at 21:26
  • 1
    Cryptography is **hard**. You need to learn far more about crypto if you want to use crypto primitives securely. Instead, you should use pre-built systems like NaCl / sodium. – SLaks Mar 06 '14 at 21:27
  • Finally, don't forget about the horrors of key management. http://blogs.msdn.com/b/ericlippert/archive/2011/09/27/keep-it-secret-keep-it-safe.aspx – SLaks Mar 06 '14 at 21:27
  • im new to this cryptography, my target is to achieve this by asymmetric – chaituG Mar 06 '14 at 21:37
  • can this be achieved any way ? – chaituG Mar 06 '14 at 21:38

2 Answers2

0

I does not make sense to convert the byte-Array to String. You have to either save the bytes directly (which would require an appropriate column in the database, for example BLOB), or you could encode the byte-Array, for example using Base64 (I would recommend the latter).

(If your problems are with the "public-crypto thingy", you may want to use the public key to encrypt, but the private key to decrypt. If you dont know what that means, check out some literature about public-key crypto, please.)

Community
  • 1
  • 1
Stefan
  • 171
  • 1
  • 13
0

Since your problem seems to be with your key, you possibly need a public key and a private key, not only a private key.

Have a look at this simple RSA demo:

encryptionAlgorithm = "RSA/ECB/PKCS1Padding";
algorithm = "RSA";

try {
    SecureRandom random = SecRandom.getDefault();

    // Since you are working with asymmetric crypto, you need a keypair:

    KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm);
    kpg.initialize(2048, random);
    KeyPair kp = kpg.generateKeyPair();

    // encrypting something with asymmetric crypto needs a public key:

    Cipher cipher1 = Cipher.getInstance(encryptionAlgorithm);
    cipher1.init(Cipher.ENCRYPT_MODE, kp.getPublic());

    byte[] text = "This is a test".getBytes("ASCII");

    System.out.println("text = " +(new String(text)));

    byte[] ciphertext = cipher1.doFinal(text);

    // here you could store & load your sipertext

    System.out.println("ciphertext = " + ciphertext);

    // decrypting something with asymmetric crypto needs a private key:

    Cipher cipher2 = Cipher.getInstance(encryptionAlgorithm);
    cipher2.init(Cipher.DECRYPT_MODE, kp.getPrivate());

    byte[] cleartext = cipher2.doFinal(ciphertext);

    System.out.println("cleartext = " +(new String(cleartext)));

} catch (Exception e) {
    e.printStackTrace();
}
Stefan
  • 171
  • 1
  • 13