首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在java中使用私钥(.private)解密数据

如何在java中使用私钥(.private)解密数据
EN

Stack Overflow用户
提问于 2012-09-12 13:00:49
回答 1查看 2.6K关注 0票数 1

数据在PHP中使用OpenSSL加密,我想解密java,但在java中得到错误。

PHP-加密的代码

代码语言:javascript
复制
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);          

    }

JAVA-中解密的代码

代码语言:javascript
复制
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代码给出了以下错误。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2012-09-12 13:15:21

您是URL-编码基本64编码的密文,但您只是破译一个基本64-解码它。要么丢失URL编码,要么在接收端解码它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12388779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档