跳到主要内容

密钥与凭证

生成RSA密钥对

  • 签名使用RSA算法。通信双方生成一组RSA密钥并交换公钥。请求时,私钥用于签名,接收请求的一方使用公钥进行验证。请求则需要按照以下方法生成一套私钥,然后将公钥上传给CodePay.
  • 使用RSA加密,只需要使用CodePay的公钥,因此不需要生成密钥。

如果使用Java语言,私钥的格式为PKCS8,而其他语言使用PKCS1。密钥的长度为2048。公钥的格式通常是PEM,一些语言(如C#)需要DER格式的公钥,这些公钥可能需要转换。

  • 如果您不知道如何生成RSA2密钥,我们建议您使用以下工具,只需单击一下即可生成密钥、生成签名和验证签名:CodePay Key Tool
使用CodePay Key Tool生成rsa密钥对
  • 您也可以使用openssl手动生成RSA2密钥,如下所示:
# 1. Generating the private key
openssl genrsa -out client_private_key_php_dotnet.pem

# 2. If you are a Java developer, convert the private key to PKCS8 format, other development languages use the PKCS1 format
openssl pkcs8 -topk8 -inform PEM -in client_private_key_php_dotnet.pem -outform PEM -nocrypt -out client_private_key_pkcs8.pem

# 3. Generate the public key
openssl rsa -in client_private_key_php_dotnet.pem -pubout -out client_public_key_php_dotnet.pem

# 4. Generate the private key that can be used in Java
cat client_private_key_pkcs8.pem | grep -v "^\-" | tr -d "\n" | sed 's/%$//' > client_private_key_java.pem

# 5. Generate the public key that can be used in Java
cat client_public_key_php_dotnet.pem | grep -v "^\-" | tr -d "\n" | sed 's/%$//' > client_public_key_java.pem