Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok. I found some solution and change something. This is new class for public/private key storing and getting it again from stored string. This class works for me !</p> <pre><code>import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; import org.bouncycastle.util.encoders.Base64; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; public class KeyGenerator extends Activity{ SharedPreferences SP; SharedPreferences.Editor SPE; PublicKey pubKey; PrivateKey privKey; Context context; public KeyGenerator(Context context){ this.context = context; SP = context.getSharedPreferences("KeyPair", MODE_PRIVATE); } public void generateKeys(){ try { KeyPairGenerator generator; generator = KeyPairGenerator.getInstance("RSA", "BC"); generator.initialize(256, new SecureRandom()); KeyPair pair = generator.generateKeyPair(); pubKey = pair.getPublic(); privKey = pair.getPrivate(); byte[] publicKeyBytes = pubKey.getEncoded(); String pubKeyStr = new String(Base64.encode(publicKeyBytes)); byte[] privKeyBytes = privKey.getEncoded(); String privKeyStr = new String(Base64.encode(privKeyBytes)); SPE = SP.edit(); SPE.putString("PublicKey", pubKeyStr); SPE.putString("PrivateKey", privKeyStr); SPE.commit(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } } public PublicKey getPublicKey(){ String pubKeyStr = SP.getString("PublicKey", ""); byte[] sigBytes = Base64.decode(pubKeyStr); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(sigBytes); KeyFactory keyFact = null; try { keyFact = KeyFactory.getInstance("RSA", "BC"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } try { return keyFact.generatePublic(x509KeySpec); } catch (InvalidKeySpecException e) { e.printStackTrace(); } return null; } public String getPublicKeyAsString(){ return SP.getString("PublicKey", ""); } public PrivateKey getPrivateKey(){ String privKeyStr = SP.getString("PrivateKey", ""); byte[] sigBytes = Base64.decode(privKeyStr); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(sigBytes); KeyFactory keyFact = null; try { keyFact = KeyFactory.getInstance("RSA", "BC"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } try { return keyFact.generatePrivate(x509KeySpec); } catch (InvalidKeySpecException e) { e.printStackTrace(); } return null; } public String getPrivateKeyAsString(){ return SP.getString("PrivateKey", ""); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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