2015-08-24 105 views
-1

我在我的應用程序中實現了所有安全措施,以防止黑客入侵,而且我陷入了一個問題。 如您所知,Google在開發人員控制檯中提供了一個Base64公鑰,以檢查付費應用中的許可。 黑客可以破解許可證檢查,所有許可證都說有許可證,所以我想在檢查自己的許可證之前,檢查是否必須說沒有許可證的虛假許可證,如果有許可證,它會讓黑客離開。然後我想知道如何製作該PublickKey對象並像Google一樣對其進行加密,並獲得一個Base64公鑰,該公鑰不會給我解密上的錯誤。 感謝和抱歉我的英語不好,希望你能理解我的探究!android - 創建RSA公鑰

回答

0

嘗試這個類爲我工作了一段時間後

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

就像一個魅力! :D謝謝! – Unixcision