利用Bouncy Castle实施E投票系统

时间:2014-04-05 19:38:18

标签: c# cryptography rsa bouncycastle

我正在研究基于Foo92协议的E投票系统。我是Bouncy Castle图书馆的新手。但我必须告诉这个系统基于盲签名和RSA算法。这个方案(FOO92)有一个图表,我已经为你上传了它。我想知道如何盲目签署并在Bouncy Castle图书馆验证它。请帮我解决我的问题。 谢谢你们。   enter image description here

请注意*运算符是盲目运算符。和/运算符是unblinding运算符。

1 个答案:

答案 0 :(得分:1)

最后,我为FOO92电子投票协议编写了​​Bouncy Castle的代码。 这是班级:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Org.BouncyCastle;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Signers;
using Org.BouncyCastle.Crypto.Digests;
using System.Windows.Forms;

namespace FooTest
{
    class FooImplementing
    {
        private RsaBlindingEngine rsaBlindingEngine = new RsaBlindingEngine();
        private RsaBlindingFactorGenerator blindingFactorGenerator = new RsaBlindingFactorGenerator();
        private RsaBlindingParameters blindingParameteres;
        private RsaKeyPairGenerator aliceRsaKeyGenerator = new RsaKeyPairGenerator();
        private AsymmetricCipherKeyPair aliceKeyPair;
        private RsaKeyPairGenerator bobRsaKeyGenerator = new RsaKeyPairGenerator();
        private AsymmetricCipherKeyPair bobKeyPair;
        private byte[] inputMessage;
        public FooImplementing(string message)
        {
            inputMessage = getBytes(message);
            aliceRsaKeyGenerator.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
            aliceKeyPair = aliceRsaKeyGenerator.GenerateKeyPair();
            //******************************************************************************
            bobRsaKeyGenerator.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
            bobKeyPair = bobRsaKeyGenerator.GenerateKeyPair();
            //******************************************************************************
            blindingFactorGenerator.Init(bobKeyPair.Public);
            blindingParameteres = new RsaBlindingParameters((RsaKeyParameters)bobKeyPair.Public, blindingFactorGenerator.GenerateBlindingFactor());
        }

        public byte[] getBytes(string input)
        {
            byte[] bytes = new byte[input.Length * sizeof(char)];
            System.Buffer.BlockCopy(input.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }

        public string GetString(byte[] bytes)
        {
            char[] chars = new char[bytes.Length / sizeof(char)];
            System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
            return new string(chars);
        }

        public byte[] blindTheMessage(TextBox t1)
        {
            for (int i = 0; i < inputMessage.Length; i++)
            {
                t1.Text += inputMessage[i].ToString();
            }
            PssSigner messageBlinder = new PssSigner(rsaBlindingEngine, new Sha1Digest(), 15);
            messageBlinder.Init(true, blindingParameteres);
            messageBlinder.BlockUpdate(inputMessage, 0, inputMessage.Length);
            byte[] blindedMessage = messageBlinder.GenerateSignature();
            return blindedMessage;
        }

        public byte[] blindSignature(byte[] input)
        {
            RsaEngine rsaEngine = new RsaEngine();
            rsaEngine.Init(true, bobKeyPair.Private);
            byte[] blindSignedMessage = rsaEngine.ProcessBlock(input, 0, input.Length);
            return blindSignedMessage;
        }

        public byte[] unblindeTheSignedData(byte[] input)
        {
            rsaBlindingEngine.Init(false, blindingParameteres);
            byte[] messageForSending = rsaBlindingEngine.ProcessBlock(input, 0, input.Length);
            return messageForSending;
        }

        public bool verifyBlindSignature(byte[] input, TextBox t1)
        {            
            PssSigner verifier = new PssSigner(new RsaEngine(), new Sha1Digest(), 15);
            verifier.Init(false, bobKeyPair.Public);
            verifier.BlockUpdate(inputMessage, 0, inputMessage.Length);
            for (int i = 0; i < inputMessage.Length; i++)
            {
                t1.Text += inputMessage[i].ToString();
            }
            return verifier.VerifySignature(input);
        }

        public byte[] signedWithRsa(byte[] input)
        {
            ISigner signer = SignerUtilities.GetSigner("SHA1withRSA");
            signer.Init(true, aliceKeyPair.Private);
            signer.BlockUpdate(input, 0, input.Length);
            byte[] signedData = signer.GenerateSignature();
            return signedData;
        }

        public bool verifyRsaSignedData(byte[] input, byte[] signature)
        {
            ISigner verifier = SignerUtilities.GetSigner("SHA1withRSA");
            verifier.Init(false, aliceKeyPair.Public);
            verifier.BlockUpdate(input, 0, input.Length);
            return verifier.VerifySignature(signature);
        }

    }
}

这是运行foo协议的步骤。当然它没有一些步骤,比如id发送,但它的功能和Foo协议一样真实

FooImplementing foo = new FooImplementing("Behzad");
var blindedMessage = foo.blindTheMessage(textBox2);
var userSignature = foo.signedWithRsa(blindedMessage);
if (foo.verifyRsaSignedData(blindedMessage, userSignature))
{
     var signedMessage = foo.blindSignature(blindedMessage);
     var unblindedMessage = foo.unblindeTheSignedData(signedMessage);
     MessageBox.Show(foo.verifyBlindSignature(unblindedMessage, textBox3).ToString());
 }

请注意,文本框用于收集结果,而不是我的程序的一部分。 谢谢大家。