PublicKey for String

0

I have a server that uses RSA and an Android app. I want the server to pass its PublicKey to the Android application. I'm trying to pass PublicKey as a string, but when I send something encrypted with this key to the server and try to decipher it with your private key, a javax.crypto.BadPaddingException: Decryption error is thrown.

Any tips on how to send the server's Public Key to the Android app?

Server-side code:

PublicKey publicKey = RSA.getPublicKey();
KeyFactory fact = KeyFactory.getInstance("RSA");
X509EncodedKeySpec spec = fact.getKeySpec(publicKey,X509EncodedKeySpec.class);
String x = Base64.encode(spec.getEncoded());
return x;

Android app side code:

byte[] data = Base64.decode(x,Base64.DEFAULT);
X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey publicKey = fact.generatePublic(spec);

Code to encrypt data with PublicKey on the Android app side

byte[] cipherText = null;

    try {
        final Cipher cipher = Cipher.getInstance("RSA");
        // Criptografa o texto puro usando a chave Púlica
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        cipherText = cipher.doFinal(texto.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return Base64.encodeToString(cipherText,Base64.DEFAULT);
    
asked by anonymous 10.04.2018 / 13:31

1 answer

1

Solution!

I came here and did well!

p>

Code to send PublicKey (SERVER SIDE):

PublicKey publicKey = RSA.getPublicKey();
KeyFactory fact = KeyFactory.getInstance("RSA");
X509EncodedKeySpec spec = fact.getKeySpec(publicKey,X509EncodedKeySpec.class);
String x = Base64.encode(spec.getEncoded());
return x; // retorna String

Code to create a PublicKey through a String (ANDROID APP SIDE)

 String publicKeyString = x;
 X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(Base64.decode(publicKeyString, Base64.DEFAULT));
 KeyFactory keyFactory = KeyFactory.getInstance("RSA");
 PublicKey publicKey = keyFactory.generatePublic(publicSpec);

Code to create a secret key with AES (ANDROID APP SIDE):

  KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
  keyGenerator.init(128); // AES is currently available in three key sizes: 128, 192 and 256 bits.The design and strength of all key lengths of the AES algorithm are sufficient to protect classified information up to the SECRET level
  SecretKey secretKey = keyGenerator.generateKey();

Code to encrypt the text with the secret key (ANDROID APP SIDE):

SecretKeySpec skeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return Base64.encodeToString(encrypted); //retorna String

Code to encrypt the secret key with the publicKey (ANDROID APP SIDE):

Cipher cipher2 = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
cipher2.init(Cipher.ENCRYPT_MODE, publicKey);
String encryptedSecretKey = Base64.encodeToString(cipher2.doFinal(secretKey.getEncoded()), Base64.DEFAULT);
return encryptedSecretKey;

-------------------------------- SEND ciphertext and encrypted key to the server ------ ----------------------

Code to decipher secret key (SERVER SIDE)

PrivateKey chavePrivada;
byte[] texto = Bse64.decode(texto_cifrado)
byte[] dectyptedSecKey = {};
try {
    //"RSA/ECB/OAEPWithSHA1AndMGF1Padding"
    Cipher cipher =Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
    // Decriptografa o texto puro usando a chave Privada
    cipher.init(Cipher.DECRYPT_MODE, chavePrivada);
    dectyptedSecKey = cipher.doFinal(texto);

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

return dectyptedSecKey;

Code to create a secret key by one byte [] (SERVER SIDE):

secretKey = new SecretKeySpec(dectyptedSecKey, 0, dectyptedSecKey.length, "AES");

Code to decrypt ciphertext with secret key (SERVER SIDE):

raw = secretKey.getEncoded();
encrypted = Base64.decode(dados);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return new String(decrypted); // retorna String TEXTO ORIGINAL
    
10.04.2018 / 14:56