Domanda

I have a problem while encrypting/decrypting with RSA in spongycastle on android. enctext contains encrypted text while dectext contains the text after decryption. In debugger, dectext matches plain text message "test" but when passed to smssend(string,string) function as string, some unknown format is shown. here is code.

{

    byte[] enctext;
    byte[] dectext;
    String message="test";

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "SC");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SC");
    keyGen.initialize(1024, random);
    KeyPair pair = keyGen.generateKeyPair();
    PrivateKey priv = pair.getPrivate();
    PublicKey pub = pair.getPublic();


    Cipher  rsaCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding","SC");
    rsaCipher.init(Cipher.ENCRYPT_MODE, pub);
    enctext=rsaCipher.doFinal(message.getBytes());

    Cipher rsaCipher2 = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding","SC");
    rsaCipher2.init(Cipher.DECRYPT_MODE, priv);
    dectext=rsaCipher2.doFinal(enctext);
    sendSMS("5554",dectext.toString());// in debugger dectext shows equivalent "test" values in decimal but when passed as string to sendSMS func it shows some unknown format...

    }

    private void sendSMS(final String phoneNumber,String message)
    {
    }

Whats wrong with the code?? Is it some format issue?? Which format should I use??

È stato utile?

Soluzione

instead of

dectext.toString()

you want to use

new String(dectext)

Furthermore, you should specify an encoding explicitly when converting between bytes and String:

message.getBytes("UTF-8")
new String(dectext, "UTF-8")
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top