C# Sign Data with RSA using BouncyCastle

前端 未结 2 1031
北荒
北荒 2020-12-23 15:28

Does anyone know of a simple tutorial or sample code of how to sign data in c# using bouncy castle. In Java there are tons of tutorials and samples. I can\'t find a single e

2条回答
  •  无人及你
    2020-12-23 16:00

    Look at Bouncy Castle web site. There is archive with sources and examples. http://www.bouncycastle.org/csharp/download/bccrypto-net-1.7-src-ext.zip

    As a examples there are a lot of NUnit tests. Below is code of method to encrypt data byte array using RSA algorithm as a sample, but in Bouncy Castle sources and tests you can find more samples.

        public static byte[] Encrypt(byte[] data, AsymmetricKeyParameter key)
        {
            RsaEngine e = new RsaEngine();
            e.Init(true, key);
            int blockSize = e.GetInputBlockSize();
            List output = new List();
    
            for (int chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize)
            {
                int chunkSize = Math.Min(blockSize, data.Length - (chunkPosition * blockSize));
                output.AddRange(e.ProcessBlock(data, chunkPosition, chunkSize));
            }
            return output.ToArray();
        }
    

提交回复
热议问题