. NET Core RSA key XML, PKCS1, PKCS8 format conversion and JavaScript, Java and other languages for docking

Source: Internet
Author: User
Tags decrypt

Well known in. NET, the key generated by the RSA class is in XML format, while other languages such as Java generally use the PKCS8 format key, JavaScript generally uses the PKCS1 format. We in the development process is likely to encounter the need to interface with other languages developed API, if we encounter RSA encryption and decryption, we must ensure that key is the same, in order to ensure the correct processing of data, we definitely need to convert the key, below I will share my own experience to everyone.

PKCS1 and PKCS8 operate with open source projects Bouncycastle

Rsautil Project

The Rsautil project is the. NET core RSA algorithm using the Help tool, which supports encrypting, decrypting, signing and validating the data using the RSA algorithm, supports XML,PKCS1,PKCS8 three key formats, and supports the conversion of keys in these three formats. Finally, PEM formatting is also supported.

Using the Generate key

Use the "Rsakeygenerator" class. The returned result is a list of strings with two elements, element 1 is the private key, and element 2 is the public key.

Format: XML

var keyList = RsaKeyGenerator.XmlKey(2048);var privateKey = keyList [0];var publicKey = keyList [1];

Format: PKCS1

var keyList = RsaKeyGenerator.Pkcs1Key(2048);var privateKey = keyList [0];var publicKey = keyList [1];

Format: PKCS8

var keyList = RsaKeyGenerator.Pkcs8Key(2048);var privateKey = keyList [0];var publicKey = keyList [1];

RSA Key Conversion

Use the "Rsakeyconvert" class. It supports the key conversions in these three formats, namely: XML,PKCS1,PKCS8.

Xml-> PKCS1:
    • Private:RsaKeyConvert.PrivateKeyXmlToPkcs1()
    • Public:RsaKeyConvert.PublicKeyXmlToPem()
Xml-> PKCS8:
    • Private:RsaKeyConvert.PrivateKeyXmlToPkcs8()
    • Public:RsaKeyConvert.PublicKeyXmlToPem()
Pkcs1-> XML:
    • Private:RsaKeyConvert.PrivateKeyPkcs1ToXml()
    • Public:RsaKeyConvert.PublicKeyPemToXml()
Pkcs1-> PKCS8:
    • Private:RsaKeyConvert.PrivateKeyPkcs1ToPkcs8()
    • Public key: No conversion required
Pkcs8-> XML:
    • Private:RsaKeyConvert.PrivateKeyPkcs8ToXml()
    • Public:RsaKeyConvert.PublicKeyPemToXml()
Pkcs8-> PKCS1:
    • Private:RsaKeyConvert.PrivateKeyPkcs8ToPkcs1()
    • Public key: No conversion required
Encrypt, decrypt, sign, and verify signatures

XML,PKCS1,PKCS8 corresponding classes: RsaXmlUtil , RsaPkcs1Util , RsaPkcs8Util . They inherit from the abstract classRSAUtilBase

    • Encryption:RSAUtilBase.Encrypt()
    • Decrypt:RSAUtilBase.Decrypt()
    • Sign:RSAUtilBase.SignData()
    • Verify:RSAUtilBase.VerifyData()
PEM format

Use the class "Rsapemformathelper".

    • Format the private key of the PKCS1 format:RsaPemFormatHelper.Pkcs1PrivateKeyFormat()
    • To remove the PKCS1 format private key format:RsaPemFormatHelper.Pkcs1PrivateKeyFormatRemove()
    • Format the private key of the PKCS8 format:RsaPemFormatHelper.Pkcs8PrivateKeyFormat()
    • To delete the private key format in PKCS8 format:RsaPemFormatHelper.Pkcs8PrivateKeyFormatRemove()
Other Notes

This project is open source, if it is helpful to you, welcome to a Star:https://github.com/stulzq/rsautil

For ease of use already uploaded nuget:https://www.nuget.org/packages/xc.framework.security.rsautil/

. NET Core RSA key XML, PKCS1, PKCS8 format conversion and JavaScript, Java and other languages for docking

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.