2012-09-12 49 views
1

數據使用OpenSSL加密在PHP中,我想解密Java,但得到錯誤在java中在Java中使用私有密鑰(.private)如何對數據進行解密

代碼加密在PHP的

在Java的
public function getEncryptedString($cardNumber,$key_id){ 
       $encryptedCardNumber = ''; 
       $key_name = "key_{$key_id}"; 
       $pub_key_path =$key_name.".public"; 
       $fp=fopen ($pub_key_path,"r"); //Open the public key (key_8.public) 
       $pub_key = fread($fp,8192); //Read public key key (key_8.public) into 
       fclose($fp); 
       openssl_public_encrypt($cardNumber,$encryptedCardNumber,$pub_key); 
       if($key_id > 4) return rawurlencode(base64_encode($encryptedCardNumber)); 
       else return addslashes($encryptedCardNumber);   

    } 

代碼解密

public static String getDecryptedValue(int keyId,String encryptedCCNumber ,String passPhrase){ 
       String result=""; 

       String privateKeyFileName="key_8.private"; 
       String privateKeyLocation= PropertiesUtil.getProperty("PUBLIC_PRIVATE_KEY_LOCATION"); 
      String privateKeyFileNameLocation=privateKeyLocation+privateKeyFileName; 
       String decryptedValue= getDecryptedMessage(privateKeyFileNameLocation,encryptedCCNumber,passPhrase); 
       return result; 

     } 


     public static String getDecryptedMessage(String privateKeyFileNameLocation, String encryptedCCNumber,String passPhrase) 
       { 
       byte[] decodedBytesCCNumber= Base64.decodeBase64(encryptedCCNumber.getBytes()); 
      byte[] decryptedMessage=null; 
      try { 
       Cipher cipher = Cipher.getInstance("RSA"); 

       PrivateKey privateKey = getPrivateKey(privateKeyFileNameLocation,passPhrase); 
       cipher.init(Cipher.DECRYPT_MODE, privateKey); 
       decryptedMessage = cipher.doFinal(decodedBytesCCNumber); 

      } catch (Throwable t) { 
       t.printStackTrace(); 
      } 

      System.out.println("new String(decryptedMessage)"+new String(decryptedMessage)); 
      return new String(decryptedMessage); 

     } 

     private static PrivateKey getPrivateKey(String privateKeyFileNameLocation,String passPhrase) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableEntryException { 
       KeyStore ks = KeyStore.getInstance("PKCS12"); 
       ks.load(new FileInputStream(privateKeyFileNameLocation), passPhrase.toCharArray()); 
       String alias = (String) ks.aliases().nextElement(); 
       KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry(alias, new KeyStore.PasswordProtection(passPhrase.toCharArray())); 
       return keyEntry.getPrivateKey(); 
      } 

Java代碼是給下面ERR要麼。

java.io.IOException: toDerInputStream rejects tag type 45 
    at sun.security.util.DerValue.toDerInputStream(DerValue.java:847) 
    at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1221) 
    at java.security.KeyStore.load(KeyStore.java:1214) 

回答

2

你是URL編碼的密文的Base64編碼,但你只是破譯它的base64解碼。要麼丟失URL編碼,要麼在接收器解碼。

+0

但錯誤是發生內部方法在執行私有靜態專用密鑰getPrivateKey(字符串privateKeyFileNameLocation,字符串密碼){ks.load(新的FileInputStream(privateKeyFileNameLocation),passPhrase.toCharArray());} – user1665785

+0

@ user1665785因此,在這種情況下,你問題不在解密中,正如它在標題中所說的那樣,它與你的keyStore文件的格式有關。你是如何創建它的? – EJP

+0

使用pem格式的openssl在服務器中生成私鑰。 – user1665785

相關問題