I load the key from the phone’s memory, decode it from base64 to byte , get the key and then work with it. But when you start the program gives an error

unknown key type passed to RSA.

What am I doing wrong?

 void main1() throws LoginException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { String test = "hello"; Cipher cipher2 = Cipher.getInstance("RSA"); //key byte [] decodedPublicKey = Base64.decode(myPublickKey, 4); byte[] decodedPrivateKey = Base64.decode(myPrivateKey,4); SecretKey originalPublicKey = new SecretKeySpec(decodedPublicKey, 0, decodedPublicKey.length, "RSA"); SecretKeySpec originalPrivateKey = new SecretKeySpec(decodedPrivateKey, 0, decodedPrivateKey.length, "RSA"); cipher2.init(Cipher.ENCRYPT_MODE, originalPublicKey); byte[] bytes2 = cipher2.doFinal(test.getBytes()); Cipher decript2 = Cipher.getInstance("RSA"); decript2.init(Cipher.DECRYPT_MODE,originalPrivateKey); byte[] decriptedBytes2 = decript2.doFinal(bytes2); for (byte b : decriptedBytes2)kodMess+=(char) b; Toast.makeText(ChatActivity.this, kodMess, Toast.LENGTH_SHORT).show(); } 

I code so

 //генерирую ключ KeyPairGenerator pairGenerator = KeyPairGenerator.getInstance("RSA"); KeyPair keyPair = pairGenerator.generateKeyPair(); Key publicKey = keyPair.getPublic(); Key privateKey = keyPair.getPrivate(); //ключи в строку final String encodedPublicKey = Base64.encodeToString(publicKey.getEncoded(),0); final String encodedPrivateKey = Base64.encodeToString(privateKey.getEncoded(),0); //обратно в байты byte [] decodedPublicKey = Base64.decode(encodedPublicKey, 0); byte[] decodedPrivateKey = Base64.decode(encodedPrivateKey,0); //из байт в ключи SecretKey originalPublicKey = new SecretKeySpec(decodedPublicKey, 0, decodedPublicKey.length, "RSA"); SecretKey originalPrivateKey = new SecretKeySpec(decodedPrivateKey, 0, decodedPrivateKey.length, "RSA"); 
  • And you do not confuse private and public keys, an hour? - Vladimir Martyanov
  • I tried to swap them, the result did not change - KTA
  • So, incomprehensible key format :-) - Vladimir Martyanov
  • I did about the same with the AES encryption method, it worked - KTA
  • The key for AES is about 4+ times shorter and simpler than the key for RSA. - Vladimir Martyanov

1 answer 1

Try as in the code below. The fact is that the SecretKeySpec constructors SecretKeySpec not know anything about the internal format of the keys. To do this, use the X509EncodedKeySpec and PKCS8EncodedKeySpec :

 KeyPairGenerator pairGenerator = KeyPairGenerator.getInstance("RSA"); KeyPair keyPair = pairGenerator.generateKeyPair(); Key publicKey = keyPair.getPublic(); Key privateKey = keyPair.getPrivate(); final byte[] encodedPublicKeyBytes = Base64.getEncoder().encode(publicKey.getEncoded()); final byte[] encodedPrivateKeyBytes = Base64.getEncoder().encode(privateKey.getEncoded()); final String encPubKey = new String(encodedPublicKeyBytes); final String encPrivKey = new String(encodedPrivateKeyBytes); System.out.println(encPubKey); System.out.println(encPrivKey); Cipher cipher = Cipher.getInstance("RSA"); byte[] decPubKey = Base64.getDecoder().decode(encodedPublicKeyBytes); X509EncodedKeySpec X509pubKey = new X509EncodedKeySpec(decPubKey); PublicKey pubKey = KeyFactory.getInstance("RSA").generatePublic(X509pubKey); cipher.init(Cipher.ENCRYPT_MODE, pubKey); byte[] rsaEncrypted = cipher.doFinal("test-test-test".getBytes()); cipher = Cipher.getInstance("RSA"); byte[] decPrivKey = Base64.getDecoder().decode(encodedPrivateKeyBytes); PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(decPrivKey); PrivateKey privKey = KeyFactory.getInstance("RSA").generatePrivate(encodedKeySpec); cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] rsaDecrypted = cipher.doFinal(rsaEncrypted); System.out.println(new String(rsaDecrypted)); 
  • Thank you so much - KTA