简体   繁体   English

php:使用 RSA 私钥进行字符串加密(从 JAVA 转换为 php)

[英]php: string encryption using RSA private key (convert from JAVA to php)

I have this JAVA code & I need to write the same thing in php:我有这个 JAVA 代码 & 我需要在 php 中写同样的东西:

public static String signMsg(String msg, String privateKey)
throws Exception {
    byte[] bytes = Base64.getDecoder().decode(privateKey);
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    Signature ps = Signature.getInstance("SHA256withRSA");
    ps.initSign(kf.generatePrivate(spec));
    ps.update(msg.getBytes("UTF-8"));
    byte[] sigBytes = ps.sign();
    return Base64.getEncoder().encodeToString(sigBytes);
}
  • Any idea how to do that?知道怎么做吗?

Thanks in advance:)提前致谢:)

Regarding your first approach: A signature is created with the private key.关于您的第一种方法:使用私钥创建签名。 The public key is used to verify the signature.公钥用于验证签名。 Regarding your second approach: A HMAC is not the same as a signature.关于您的第二种方法:HMAC 与签名不同。

The Java code loads a private key in PKCS8 format, PEM encoded without header and footer. Java 代码加载 PKCS8 格式的私钥,PEM 编码没有 header 和页脚。 In the PHP code the key can be read in the same format and encoding.在 PHP 代码中,可以以相同的格式和编码读取密钥。 Alternatively, the key can be loaded in PKCS#1 format.或者,可以以 PKCS#1 格式加载密钥。 Regarding the encoding, a PEM or DER encoded key is also accepted.关于编码,也接受 PEM 或 DER 编码的密钥。

Additionally, it must be specified which algorithm is used for signing.此外,必须指定用于签名的算法。 The Java code applies RSA with PKCS#1 v1.5 padding and SHA-256 as digest. Java 代码应用带有 PKCS#1 v1.5 填充和 SHA-256 作为摘要的 RSA。 Furthermore, the generated signature is Base64 encoded.此外,生成的签名是 Base64 编码的。 In order for the PHP code to provide the same RSA signature, the same parameters must be used.为了让 PHP 代码提供相同的 RSA 签名,必须使用相同的参数。

Note that signing does not necessarily generate the same signature using the same message and the same key.请注意,签名不一定使用相同的消息和相同的密钥生成相同的签名。 It depends on the algorithm.这取决于算法。 However, in the case of RSA with PKCS#1 v1.5 padding, always the same signature is generated (deterministic).但是,对于带有 PKCS#1 v1.5 填充的 RSA,始终会生成相同的签名(确定性)。 For PSS, on the other hand, a different signature is generated each time (probabilistic).另一方面,对于 PSS,每次都会生成不同的签名(概率)。

The following PHP code uses the PHPSECLIB and generates the same signature as the Java code:以下 PHP 代码使用 PHPSECLIB 并生成与 Java 代码相同的签名:

use phpseclib3\Crypt\RSA;

$privateKey= 'MIIEvg...';
   
$signatureB64 = base64_encode(                              // Base64 encode signature
    RSA::load($privateKey)->                                // Choose RSA, load private PKCS8 key
    withHash('sha256')->                                    // Choose SHA-256 as digest
    withPadding(RSA::SIGNATURE_PKCS1)->                     // Choose PKCS#1 v1.5 padding
    sign('The quick brown fox jumps over the lazy dog')     // Sign messsage
);

print($signatureB64);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM