title: Storing public key in sharedPreferences, and Retrieving it
body: <p>I am trying to use encrypt data using RSA. So far everything is fine. I can generate Private-Public keys, I can encrypt and Decrypt string successfully. 
  Now i want stored Public key in SharedPreference. I can store it as string. I can retrieve it as string. I need to convert it to Key, to pass to cipher. Conversion from String to original format is not happening.</p>
  
  <p>This is what i tried</p>
  
  <pre><code>        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); //generate key using RSA
          KeyPair keypair=keyPairGenerator.generateKeyPair(); //get generated key
          Cipher cipher =Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
          SharedPreferences sharedPreferences=context.getSharedPreferences("rsakey", MODE_PRIVATE);//Initializing SharedPerference 
  
  
  
          SharedPreferences.Editor editor=sharedPreferences.edit();
          editor.putString("public",keypair.getPublic().toString());
          editor.putString("private",keypair.getPrivate().toString());
          editor.commit();//store key in sharedpreference
          final String sampletext="abcde";
  
         //getting stored key
          String publicKey = sharedPreferences.getString("public", null);
          String privateKey = sharedPreferences.getString("private", null);
  
  
  
          //publicKey must of type "KEY", so i need to convert publicKey to KEY, But its not happening
          cipher.init(Cipher.ENCRYPT_MODE,publicKey);
          byte[] encryptedtext=cipher.doFinal(sampletext.getBytes());
          String encrypted_text=new String(Base64.encode(encryptedtext,Base64.NO_WRAP));
  
  
         //privateKey is string, it supposed to be of type KEY
           cipher.init(Cipher.DECRYPT_MODE,privateKey);
           encryptedtext=Base64.decode(encrypted_text.getBytes(), Base64.NO_WRAP);
           encryptedtext=cipher.doFinal(encryptedtext);
  
           String  decrypted_text=new String(encryptedtext);
  </code></pre>
  
  <p>Here, i am facing issue in    cipher.init(Cipher.ENCRYPT_MODE,publicKey);
  publicKey contains stored PublicKey, extracted from SahredPreferences. It is of type String! How to convert it to Key?</p>
  
  <p>PS:This is just sample code, in real life situation i will be storing private key in server and Public key will be issued to users.</p>