C#盲签名unblind消息

时间:2014-12-12 18:49:32

标签: c# cryptography signature voting-system blind

我发现在java中使用bouncy castle实现了盲签名,我已经在我的c#项目中实现了它。但是我需要在获得其他方签名后解除消息。

这是我的代码:

class Program
{

    public static AsymmetricCipherKeyPair generateKeys(int keySize)
    {
        RsaKeyPairGenerator r = new RsaKeyPairGenerator();

        r.Init(new RsaKeyGenerationParameters(new BigInteger("10001", 16), new SecureRandom(),
                   keySize, 80));

        AsymmetricCipherKeyPair keys = r.GenerateKeyPair();

        return keys;
    }

    public static BigInteger generateBlindingFactor(ICipherParameters pubKey)
    {
        RsaBlindingFactorGenerator gen = new RsaBlindingFactorGenerator();

        gen.Init(pubKey);

        return gen.GenerateBlindingFactor();
    }

    public static byte[] blind(ICipherParameters key, BigInteger factor, byte[] msg) {
    RsaBlindingEngine eng = new RsaBlindingEngine();

    RsaBlindingParameters param = new RsaBlindingParameters((RsaKeyParameters) key, factor);
    PssSigner blindSigner = new PssSigner(eng, new Sha1Digest(), 15);
    blindSigner.Init(true, param);

    blindSigner.BlockUpdate(msg, 0, msg.Length);

    byte[] blinded = null;
    try {
        blinded = blindSigner.GenerateSignature();
    } catch (Exception ex) {
        Console.WriteLine(" ");
    }

    return blinded;
}

public static byte[] unblind(ICipherParameters key, BigInteger factor, byte[] msg) {
    RsaBlindingEngine eng = new RsaBlindingEngine();

    RsaBlindingParameters param = new RsaBlindingParameters((RsaKeyParameters) key,factor);
    eng.Init(false, param);

    return eng.ProcessBlock(msg, 0, msg.Length);
}

    public static byte[] sign(ICipherParameters key, byte[] toSign)
    {
        Sha1Digest dig = new Sha1Digest();
        RsaEngine eng = new RsaEngine();

        PssSigner signer = new PssSigner(eng, dig, 15);
        signer.Init(true, key);
        signer.BlockUpdate(toSign, 0, toSign.Length);

        byte[] sig = null;

        try
        {
            sig = signer.GenerateSignature();
        }
        catch (Exception ex)
        {
            Console.WriteLine(" ");
        }

        return sig;
    }

    public static bool verify(ICipherParameters key, byte[] msg, byte[] sig)
    {
        PssSigner signer = new PssSigner(new RsaEngine(), new Sha1Digest(), 15);
        signer.Init(false, key);

        signer.BlockUpdate(msg, 0, msg.Length);

        return signer.VerifySignature(sig);
    }

    public static byte[] signBlinded(ICipherParameters key, byte[] msg)
    {
        RsaEngine signer = new RsaEngine();
        signer.Init(true, key);
        return signer.ProcessBlock(msg, 0, msg.Length);
    }



    static void Main(string[] args)
    {

        AsymmetricCipherKeyPair bob_keyPair = generateKeys(1024);
        AsymmetricCipherKeyPair alice_keyPair = generateKeys(1024);

    try {
        byte[] msg = Encoding.ASCII.GetBytes("OK");

        //----------- Bob: Generating blinding factor based on Alice's public key -----------//
        BigInteger blindingFactor = generateBlindingFactor(alice_keyPair.Public);

        //----------------- Bob: Blinding message with Alice's public key -----------------//
        byte[] blinded_msg =
                blind(alice_keyPair.Public, blindingFactor, msg);

        byte[] unblinded_msg =
                unblind(alice_keyPair.Private, blindingFactor, blinded_msg);

        //------------- Bob: Signing blinded message with Bob's private key -------------//
        byte[] sig = sign(bob_keyPair.Private, blinded_msg);

        //------------- Alice: Verifying Bob's signature -------------//
        if (verify(bob_keyPair.Public, blinded_msg, sig)) {

            //---------- Alice: Signing blinded message with Alice's private key ----------//
            byte[] sigByAlice =
                    signBlinded(alice_keyPair.Private, blinded_msg);

            //------------------- Bob: Unblinding Alice's signature -------------------//
            byte[] unblindedSigByAlice =
                    unblind(alice_keyPair.Public, blindingFactor, sigByAlice);

            //---------------- Bob: Verifying Alice's unblinded signature ----------------//
            Console.WriteLine(verify(alice_keyPair.Public, msg,
                    unblindedSigByAlice));
           // Now Bob has Alice's signature for the original message
            //Console.WriteLine(Encoding.ASCII.GetString(unblindedSigByAlice));
        }



        Console.WriteLine(Encoding.ASCII.GetString(unblinded_msg));
    } catch (Exception e) {

    }

    Console.ReadLine();




    }
}

有谁知道如何解开这个消息? 我正在寻找你的回音!

0 个答案:

没有答案