简体   繁体   中英

How to encrypt data with RSA private key in C#

I'm working with a third party API which developed in Java. It requires to encrypt data with given RSA private key to generate a signature. But ' RSACryptoServiceProvider' in C# only allow encrypt by a public key.

So far I have tried to use 'BouncyCastle' to encrypt data with a private key. But the API response with an error. It says, 'verify signature failed' .

Hot to fix this, any ideas?

BTW: I use below code to convert Java private key to C# xml private key. Does this correct?

RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(priKey));
return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
    Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
    Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
    Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
    Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
    Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
    Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
    Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
    Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));

I suppose you have to use:

  • a Private Key to decrypt encrypted data or sign data, and
  • a Public Key to encrypt data or verify a signature.

In asymmetric cryptography, using private key to encrypts acts as a signature: every one can verifies that you had signed using your public key but only you can sign using your private key (see https://en.wikipedia.org/wiki/Public-key_cryptography#Digital_signatures ). Obviously you have to keep a pair of keys to be used exclusively for this purpose.

With BouncyCastle library, you can achieve this result using RsaEngine:

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;    

public void Test()
{
    RsaEngine engine;
    AsymmetricKeyParameter key;
    bool forEncryption;
    int chunkPosition = 0;
    int i = 0;
    int blockSize;
    int chunkSize;
    List<byte> output = new List<byte>();
    byte[] byteMessageArray;


    // Initialize key variable with your public or private key
    // Initialize byteMessageArray with your message to be encrypted or decrypted
    // Set forEncryption variable value 


    engine = new RsaEngine();
    engine.Init(forEncryption, key);
    blockSize = engine.GetInputBlockSize();

    while ((chunkPosition < byteMessageArray.Length))
    {
        chunkSize = Math.Min(blockSize, byteMessageArray.Length - (i * blockSize));
        output.AddRange(engine.ProcessBlock(byteMessageArray, chunkPosition, chunkSize));
        chunkPosition = (chunkPosition + blockSize);
        i += 1;
    }

    //Now in output you have messagge encrypted or decrypted with your private or public key
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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