[ZT] Java, PHP, and C language RSA interoperability

Source: Internet
Author: User
Tags openssl rsa

Recently, there was a project involving cooperation with other websites. The authentication plan for communication between the two parties was implemented through RSA. Because it may involve different development environments, we need to study the support for RSA in different languages.

  1. By default, the Public Key File Created by OpenSSL is in PEM format, but the Java API import password can only be in der format. In particular, the key must be encoded using PKCS #8. This requires converting the files generated by OpenSSL.

    • OpenSSL RSA-inform PEM-In rs1_v. pem-outform der-pubout-out rsapub. Der
    • OpenSSL pkcs8-topk8-inform PEM-In rs1_v. pem-outform der-nocrypt-out rs1_v. Der
  2. The basic algorithm standards are OpenSSL: rsa_private_encrypt/rsa_public_decrypt, rsa_public_encrypt/rsa_private_decrypt, because the OpenSSL module of PHP only provides these four basic functions (do not fantasizing about using something outside of the OpenSSL module for RSA operations, such as the crypt_rsa of pear, which slows down to a terrible level)
  3. Note that in the above four functions, the padding parameter can be used only a few. Corresponding to cipher in Java. the getinstance () parameter can only use "RSA/NONE/pkcs1padding" or "RSA/NONE/nopadding" (or "RSA/NONE/oaeppadding" corresponds to rsa_pkcs1_oaep_padding, but I did not go into it ). The default padding in PHP is rsa_pkcs1_padding.
  4. About python... Well, just use ctypes.
  5. When using RSA signature, the text is first used as a one-way hash, and then the private key is used to encrypt the signature. The verification end obtains the signature and text, decrypts the signature with the public key, and compares whether the text hash is used. OpenSSL encapsulates rsa_sign/rsa_verify to do this.
    • However, several NID hashing algorithms of OpenSSL and the standard sha1/MD5 Algorithm seem to be different, and PHP is almost unable to interoperate with these two functions.
    • Signature. getinstance () can use "md2withrsa", "md5withrsa", or "sha1withrsa ", however, it is unclear whether it can correspond to nid_md2/digest, nid_md5/digest/nid_md5withrsa, nid_sha1/nid_sha1withrsaencryption/nid_sha1withrsa of OpenSSL, and it still takes time.

    To improve the interoperability of multiple languages, we do not use the encapsulated functions rsa_sign/rsa_verify.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.