Note that there are some explanatory texts on larger screens.

plurals
  1. POWrite public/private key to the SharedPreferences and use it
    text
    copied!<p>I am trying to encrypt and decrypt a text with public/private keys.First of all, I create my keys and store them in sharedpreferences with this code : </p> <pre><code>SharedPreferences SP; SharedPreferences.Editor SPE; KeyPairGenerator keyGen; KeyPair keypair; PublicKey publicKey; PrivateKey privateKey; keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); keypair = keyGen.genKeyPair(); privateKey = keypair.getPrivate(); publicKey = keypair.getPublic(); SPE = SP.edit(); SPE.putString("PublicKey", publicKey.toString()); SPE.putString("PrivateKey", privateKey.toString()); SPE.commit(); </code></pre> <p>In my SharedPreferences file, keys are written like this :</p> <blockquote> <p>PublicKey: RSA Public Key</p> <p>modulus: d07b8f32968cf65301fd710f9d6d036feac01d7b98c92ff979cd324d252cb257ff48d6630b33f0f68bd0ee81c3a83502a0abf0b263dc96c2b86940f7ec19ab1865626383e55cf5a37e25ef4eb6ca88a39f31becb6065434bc2236177aa5b35266fe0379164faea6ef7a92812e7aa3ef5fc488c70ab085f5564f09c0f6e927b49</p> <p>public exponent: 10001</p> <p>PrivateKey: RSA Private CRT Key</p> <p>modulus: d07b8f32968cf65301fd710f9d6d036feac01d7b98c92ff979cd324d252cb257ff48d6630b33f0f68bd0ee81c3a83502a0abf0b263dc96c2b86940f7ec19ab1865626383e55cf5a37e25ef4eb6ca88a39f31becb6065434bc2236177aa5b35266fe0379164faea6ef7a92812e7aa3ef5fc488c70ab085f5564f09c0f6e927b49</p> <p>public exponent: 10001<br> private exponent: 67ebef696c1a3fff0892e8f4bba8477a562e05844298a6cd58a5ac59401a939bc1a8f114d5d4c25c633d766640bd6c0f2f4005ef265022e6553e4220531448702e4bbf4322b9d5cf444d16eea151e5d565412b49208a73d9236607475d201affa21d374e3186f14b651b08565be4725f89fc6797a79c8433c4dd089589284a01</p> <p>primeP: ee4ad1a56f4ee3b12c198d09b08a92c349f94cc79a6143ca7140fa64c919f2d9c24c29d3b413fdc4039000b6b5feac5a764ce436db4a4a382d8ceecbc768e0d1</p> <p>primeQ: dff9a761807440b4a5a4fb04ebaa22849f6543f33168bd6e83b3c549b346661124d7879e168c1009e97c01b3fdcd7088eebd9c989b64d7c4b81ea46f9e06d0f9</p> <p>primeExponentP: 2ce01e371f8d25c819dbfdf9932ba593ed7c6b7f338d99aca8436a644c92fc6f11ee31fa5271695adea8e1d986d09d38b40aaaf7c1b86dddc28645fa4e656be1</p> <p>primeExponentQ: 21904af9fc82ef5362e3474ea4763978005eef80d92da5fd92b4f4e2a77fec39b378acf50ed1ec715fd0da7c7b9336c2fe6be1b4a8ccc2dcd2ee9c9bb165ba19</p> <p>crtCoefficient: d8ccccb874ec4c2d464e84829547507e1ebf78e506caa77950b04329957b8713e80553874b825bf5c90b214984b4657b64965867460d87aab135f43930db48ec</p> </blockquote> <p>And with this code i am trying to read this keys :</p> <pre><code>private PublicKey getPublicKey() throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException{ byte[] keyBytes = Base64.decodeBase64(SP.getString("PublicKey", "default value").getBytes("utf-8")); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey key = keyFactory.generatePublic(spec); return key;} </code></pre> <p>But something goes wrong.It gives InvalidKeySpecException. I thing this cannot read the keys from file.How can i fix this problem? Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload