Android Open Source

 

Open Source Java App

RSA Crypto

In the previous post, there was a joke about the fact that encryption keys in PCA cannot be written to a file, sent, or converted. For those whose nerves are well tuned, let's continue. Encryption keys can be saved to a file and restored from a file. Although this is not the most secure way to store them.

byte[] pubKeyBytes = new byte[pubKeyLength];
FileInputStream privIn = new FileInputStream(privateKey);
FileInputStream pubIn = new FileInputStream(publicKey);
privIn.read(privKeyBytes);
pubIn.read(pubKeyBytes);
pubIn.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privKeyBytes);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubKeyBytes);
crypto.keyPair = new KeyPair(keyFactory.generatePublic(publicKeySpec),
    keyFactory.generatePrivate(privateKeySpec));
crypto.base64 = base64; That's enough to get it all done. Of course, there are a few "nails".In a real application, the code is more complicated. And working.
We will publish it a little later.In Android 10, the PKCS#8 standard is available. Although the following versions are used in serious systems. EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privKeyBytes);  

Open Source Java App

   

 

Comments

Popular posts from this blog

Android Open Source

Android PRNG

Android Open Source