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");