1

I want to encode a string with rsa/ecb/pkcs1 padding mode with a given public key (the public key is a string) in java.

I also want to present the results in UTF-8 Format how to do it?

Rick Hanlon II
  • 16,833
  • 7
  • 42
  • 52
Marzo
  • 43
  • 1
  • 9
  • There are a lot of examples out there. What's the problem you're having? Don't forget to encode the ciphertext with base64 or hex if you want to have it in a string. – Artjom B. May 23 '16 at 17:57
  • The results will be essentially random byte and not all such bytes are a valid UTF-8 encoding. If you need UTF-8 you need to use an encoding on the random output, Base64 and Hexadecimal are commonly used. – zaph May 23 '16 at 18:58

2 Answers2

1

i have done this code:

        String pub = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA4IJZLsjlx+o4RSvafaAcReoNnzrI0UXu7kZyXPe31ql32X9AvhC6QQIUmLkr1Evm0zP/SgVG9YX3DSqBUgPo04iv1I1/wNKwAf1/uH9EiiqdpczefyxxnzJiKUTcx2/4mA4E4QxCIL5JsZb78WoYZrd2kToW/WD01MnSFiCgSyjGdd812GY2EVzfvlv8kYuti3icMUyitEfHhtw8cAWI6/nVrRPNs0e5NsvtZJ0nfrXsfQDR0C7+ivQK+fQabi8oRGsbTZceAvVlqVE669zoIwIFLcB+eYXTxbka4E7veUMpaF9w//HdwVS2y/2jJiI+16qPStQQPIKQ4Cucoif7/UHfIBuVGVJ5MIVyK7NC7TV/lyoXmyo7ZcnVZnI7rZcw5/qZcqaZ0VCrzvHijwTK7100hOOjiarvRa2OJGXHLIeAUlbrHOXEXS6ah2glPhLDEg6Qzp/lKVSISolal7q73qyhF483P9jXn3hefSLA9J1/1LgeajWvuVkxuw+dy2Tlv7oUpNBkX47/TOho5qttr1y9K3hD5Q87RAJPdBtFdDbY8qUPxoiBsTbUWjVoEjJf2YAsLTJIIi2ZISkbD/VdrtZnS73QSJkJReOMNT9XYNGDJvwNIrRcNGFKlJcX6qq+ozGNsDkrt0ObxAD7YCTjAYQVTlbQOaTu5DbGxGDNCoMCAwEAAQ==";

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] keyBytes = Base64.getDecoder().decode(pub.getBytes("UTF-8"));  
        PKCS1EncodedKeySpec KeySpec = new PKCS1EncodedKeySpec(keyBytes);
        RSAPublicKey publicKey = (RSAPublicKey)keyFactory.generatePublic((java.security.spec.KeySpec) KeySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        byte[] cipherData = cipher.doFinal(text.getBytes("UTF-8"));


        return cipherData;

But it doesnot work.. it is said that Invalid DER: object is not integer

Marzo
  • 43
  • 1
  • 9
-1

Assuming you're using a valid RSA key, you'll need to:

  1. Convert your public key from a string to an actual public key object

    //This code is incorrect. You'll need bouncy castle for PKCS1
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    byte[] keyBytes = Base64().getDecoder.decode(publicKey.getBytes()); //assuming base64 encoded key
    PKCS1EncodedKeySpec KeySpec = new PKCS1EncodedKeySpec(keyBytes);
    RSAPublicKey publicKey = (RSAPublicKey)keyFactory.generatePublic(KeySpec);
    
  2. Get the bytes of your plain text

  3. Encrypt using your public key
  4. Encode to a readable format.

Check out this answer for steps 1-3: RSA Encrypt/Decrypt in Java. Remember to use the correct algorithm spec, in your case PKCS1

Chances are your cipher text will not use only UTF-8 characters so you'll probably want to use Base 64 encoded text to display your cipher text. Base 64 is able to display all those wonky characters as ascii values.

Simply use: Base64.getEncoder().encodeToString(cipherTextBytes)

Community
  • 1
  • 1
venture
  • 81
  • 10
  • My problem is that I don t want to generate a key. My public key is in this form : lyoXmyo7ZcnVZnI7rZcw5.... how can I use this public key in this form ? – Marzo May 23 '16 at 18:29
  • 1
    @Marzo You'll first have to figure out what find of format this public key is in. Ask the people you've got this from and if they don't know, ask for the code that generated it. You should be able to go from there. – Artjom B. May 23 '16 at 18:55
  • 1
    I have the modulus and the exponent of the key but i cannot initialize my key from my java code – Marzo May 23 '16 at 19:03
  • @Marzo In that case: https://docs.oracle.com/javase/8/docs/api/index.html?java/security/spec/RSAPublicKeySpec.html – Artjom B. May 23 '16 at 19:17
  • @venture I can t understand the line PKCS1EncodedKeySpec(res) what is res ? – Marzo May 23 '16 at 20:14
  • @venture sorry but PKCS1EncodedKeySpec is undefined as a type for me .. ! – Marzo May 23 '16 at 20:31
  • @Marzo Apologies, I didn't notice you were using PKCS1 rather than PKCS8. You'll need to use bouncy castle to convert your key into a useable RSA key. http://stackoverflow.com/questions/7611383/generating-rsa-keys-in-pkcs1-format-in-java – venture May 23 '16 at 20:51
  • @venture please look at the code in the second answer – Marzo May 23 '16 at 22:21
  • it throws an error "Invalid DER: object is not integer" – Dante Apr 11 '19 at 13:14