The RSA public key consists of a module and an exponent, which are specified as BigInteger , respectively, for serialization, you need to remove the module and the exponent, save them somewhere, and then, for deserialization, generate the public key back from the module and exponents, such as:
//ΠΈΠ·Π²Π»Π΅ΡΠ΅Π½ΠΈΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠΉ ΠΊΠ»ΡΡΠ° RSAPublicKey publicKey; BigInteger modulus=publicKey.getModulus(); BigInteger exponent=publicKey.getPublicExponent(); //ΡΠ΅ΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΡ Π·Π½Π°ΡΠ΅Π½ΠΈΠΉ ΠΊΠ»ΡΡΠ° String modulusString=modulus.toString(); String exponentString=exponent.toString(); //Π²ΠΎΡΡΡΠ°Π½ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΊΠ»ΡΡΠ° RSAPublicKeySpec Spec = new RSAPublicKeySpec(new BigInteger(modulusString), new BigInteger(exponentString)); RSAPublicKey newPublicKey=KeyFactory.getInstance("RSA").generatePublic(Spec);