使用PHP

时间:2017-05-02 11:26:23

标签: php encryption openssl php-openssl

我发送加密的base64编码的JSON对象

N4m4EBDjdCfq5V1JqjMXdO6PEjNEh1fAMaHPn....

到一个读取并返回响应的API,我在尝试解密响应时遇到了麻烦。我从API获得的响应是​​

� �Gֲ:й�@���BJ)3ࣗ�%���wW��u~��J�1��(�/�ik�A���Ԥ& ��g��yi�ُ�3��qxQ������4iM���k�o��k����k\�6�x9 

我的解密代码

$private_key = file_get_contents('privateKey.key');

    $res = openssl_pkey_get_private($private_key);

    $a_key = openssl_pkey_get_details($res);

    // Decrypt the data in the small chunks
    $chunkSize = ceil($a_key['bits'] / 8);
    $output = '';

    while ($response)
    {
        $chunk = substr($response, 0, $chunkSize);
        $response = substr($response, $chunkSize);
        $decrypted = '';
        if (!openssl_private_decrypt($chunk, $decrypted, $res, OPENSSL_NO_PADDING))
        {
            while ($msg = openssl_error_string())
         echo $msg . "<br />\n";
        }
        $output .= $decrypted;
    }

    openssl_free_key($res);

    echo '<br /><br /> Unencrypted Data: ' .$output;

解密后的输出是

�E�X,�/�၏�����2#>�o�����    1f�=�*�O���г���18}Yi�,�_�ڪ�|

但我期待一个可读的纯文本回复。

编辑:我现在明白加密是在Java的一端发生的,加密的示例代码是

The code for using .pem file below: 
public static String encrypt(String rawText, PublicKey publicKey) throws 
 IOException, GeneralSecurityException {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    return Base64.encodeBase64String(cipher.doFinal(rawText.getBytes("UTF-
8")));
}

public static RSAPublicKey getPublicKey(String filename) throws IOException, 
GeneralSecurityException {
    String publicKeyPEM = getKey(filename);
    return getPublicKeyFromString(publicKeyPEM);
}

public static RSAPublicKey getPublicKeyFromString(String key) throws 
IOException, GeneralSecurityException {
    String publicKeyPEM = key;
    publicKeyPEM = publicKeyPEM.replace("-----BEGIN PUBLIC KEY-----\n", "");
    publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", "");
    byte[] encoded = Base64.decodeBase64(publicKeyPEM);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(new 
X509EncodedKeySpec(encoded));
    return pubKey;
}


private static String getKey(String filename) throws IOException {
    // Read key from file
    String strKeyPEM = "";
    BufferedReader br = new BufferedReader(new FileReader(filename));
    String line;
    while ((line = br.readLine()) != null) {
        strKeyPEM += line + "\n";
    }
    br.close();
    return strKeyPEM;
}

public static void main(String[] args) {
RSA necryptionService = new RSA();
try
   {
String file = "D:/public.pem";
                String data = "Hello Test"
       PublicKey publicKey = necryptionService.getPublicKey(file);
      String encryptedData = necryptionService.encrypt(data,publicKey);
       System.out.println(encryptedData); 
   }
   catch (Exception e)
   {
       e.printStackTrace();
   }
}

Where the string gets converted in the 3rd method, try this additional piece 
of code


    CertificateFactory fact = CertificateFactory.getInstance("X.509");
    FileInputStream is = new FileInputStream(path);
    Certificate cer = fact.generateCertificate(is);

    logger.info("Public Key:::::::::::loaded"+cer.getPublicKey());

我使用cURL将请求发送到API并返回java加密响应。

加密的请求是

Zxh5xmxS6pteaq + aWWorF7WRGPn6MB5SlcUR16ncYbUPaNfhOiHskYPuxY + 6I3KoHLGWpZ3IndTkaS0CxOSNwFFIsLcAWj9Jv9B6rnk8Y0Tt54d4fygfpY0FGUJdxlgY7DN0ll00OBMqlcReWlBoVxWP06nHLuUmNke9SH0ToJo + V / S7ntQYTBYV + DVbFLJF + / U7woM6xHY8pSoBfKFC1r443ftOP05adkUcrPVIrvswIfk + 4TmRvF5Th1tKaHQyGCQ6MJnOZt37RIiMFnfyqgC7RUQHnz6CfnfXKqe3RCUiuZ0wuRaPPLLzmbBhh0Qi7egJqmvFWl6Mx177a3cseVSikhI50QxRnl2zQjcUcUBWXvLf2av + ggBrSb2rLr4 + 3B2MGUV / 5831LH4jDYuLhhYteeq36vODiEE6io / WXOR / V8LCDe44kOoJiKIe0agghGWBVmsEaMAKHLnDKrA885RAEf1ogtXwPlKaMWH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