2013-07-22 153 views
5

我試圖加密和解密文本與所有的公共/私人keys.First,創建我的鑰匙和他們sharedpreferences與此代碼存儲:寫公鑰/私鑰對SharedPreferences,並用它

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(); 

在我SharedPreferences文件,按鍵這樣寫的:

公鑰:RSA公鑰

模量:d07b8f32968cf65301fd710f9d6d036feac01d7b98c92ff979cd324d252cb257ff48d6630b33f0f68bd0ee8 1c3a83502a0abf0b263dc96c2b86940f7ec19ab1865626383e55cf5a37e25ef4eb6ca88a39f31becb6065434bc2236177aa5b35266fe0379164faea6ef7a92812e7aa3ef5fc488c70ab085f5564f09c0f6e927b49

公用指數:10001

專用密鑰:RSA私人CRT鍵

模量:d07b8f32968cf65301fd710f9d6d036feac01d7b98c92ff979cd324d252cb257ff48d6630b33f0f68bd0ee81c3a83502a0abf0b263dc96c2b86940f7ec19ab1865626383e55cf5a37e25ef4eb6ca88a39f31becb6065434bc2236177aa5b35266fe0379164faea6ef7a92812e7aa3ef5fc488c70ab085f5564f09c0f6e927b49

公用指數:1000 1個
私人指數:67ebef696c1a3fff0892e8f4bba8477a562e05844298a6cd58a5ac59401a939bc1a8f114d5d4c25c633d766640bd6c0f2f4005ef265022e6553e4220531448702e4bbf4322b9d5cf444d16eea151e5d565412b49208a73d9236607475d201affa21d374e3186f14b651b08565be4725f89fc6797a79c8433c4dd089589284a01

primeP:ee4ad1a56f4ee3b12c198d09b08a92c349f94cc79a6143ca7140fa64c919f2d9c24c29d3b413fdc4039000b6b5feac5a764ce436db4a4a382d8ceecbc768e0d1

primeQ:dff9a761807440b4a5a4fb04ebaa22849f6543f33168bd6e83b3c549b346661124d7879e168c1009e97c01b3fdcd7088eebd9c989b64d7c4b81ea46f9e06d0f9

primeExponentP:2ce01e 371f8d25c819dbfdf9932ba593ed7c6b7f338d99aca8436a644c92fc6f11ee31fa5271695adea8e1d986d09d38b40aaaf7c1b86dddc28645fa4e656be1

primeExponentQ:21904af9fc82ef5362e3474ea4763978005eef80d92da5fd92b4f4e2a77fec39b378acf50ed1ec715fd0da7c7b9336c2fe6be1b4a8ccc2dcd2ee9c9bb165ba19

crtCoefficient:d8ccccb874ec4c2d464e84829547507e1ebf78e506caa77950b04329957b8713e80553874b825bf5c90b214984b4657b64965867460d87aab135f43930db48ec

而與此代碼我想讀這個鍵:

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;} 

但是出了點問題,它給出了InvalidKeySpecException。我不能從文件中讀取密鑰。如何解決這個問題?謝謝。

回答

9

好的。我找到了一些解決方案並改變了一些東西這是公共/私人密鑰存儲的新類,並從存儲的字符串中再次獲取。這門課適合我!

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", "");  
    } 
} 
4

感謝上面接受的代碼(也許在下面)。但是,就我而言,'getPrivatekey()'會引發InvalidKeySpecException。它說'未知的KeySpc類型:java.secrity.spec.X509EncodedKeySpec'。我使用的解決方案是用PKCS8EncodedKeySpec替換X509EncodedKeySpec。然後它工作! 參考:https://stackoverflow.com/a/9755391/2481444