首页 新闻 搜索 专区 学院

java的RSA签名验签怎么转化成c#

0
悬赏园豆:20 [已解决问题] 解决于 2017-05-27 17:14

银行给了,公钥私钥指数和模数,如下是java的签名验签的方法:

公钥指数:String publicExponent = “00010001”

私钥指数:String privateExponent = “”

模数:String modulus = “”

public String sign(final String plainData) throws Exception {
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(new BigInteger(modulus, 16),
            new BigInteger(privateExponent, 16));
    PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
    Signature signature = Signature.getInstance("SHA1withRSA");
    signature.initSign(privateKey);
    signature.update(plainData.getBytes("UTF-8"));
    return Base64.getEncoder().encodeToString(signature.sign());
}

public boolean verify(final String plainData, final String sign) throws Exception {
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(new BigInteger(modulus, 16),
            new BigInteger(publicExponent, 16));
    PublicKey publicKey = keyFactory.generatePublic(keySpec);
    final Signature signature = Signature.getInstance("SHA1withRSA");
    signature.initVerify(publicKey);
    signature.update(plainData.getBytes("UTF-8"));
    return signature.verify(Base64.getDecoder().decode(sign));
}

 

怎么转化成c#,我现在卡在怎么将公私钥指数+模数,生成公私钥,然后签名,我试了好几种方式,都不行。。有知道的大神求问

EApple的主页 EApple | 初学一级 | 园豆:139
提问于:2017-05-26 14:38
< >
分享
最佳答案
0

java test

static String publicExponent = "10001";
    static String privateExponent = "78bf10f3b7274ddd04da85a2b737dc524f717e765c16515f25479cf7db4627b116da3198fd54ef3732795cb9164029acd8e6c76d6bfa27bd9f433ad88ee7c2bd";
    static String modulus = "bddf972718f690b4d9e85de3a3a854ed4b72640914fb1073d07319687dbc8fc2b7b5701cce4cd47bdffd343a0e5b2762e572be07aa5c8d1201a035cc440c77a5";
    static
    {
//         generateKeyPair(); 
    }

    /**
     * 生成一对密钥
     */
    static void generateKeyPair()
    {
        try
        {
            KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
            gen.initialize(512);
            KeyPair pair = gen.generateKeyPair();
            // rsa生成一对公私钥
            RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic();
            RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate();
             
            BigInteger md = publicKey.getModulus();
            BigInteger pe = publicKey.getPublicExponent();
            BigInteger _md = privateKey.getModulus();
            BigInteger re = privateKey.getPrivateExponent();  
            assertEquals(md,_md);  
            publicExponent = pe.toString(16);
            privateExponent = re.toString(16);
            modulus = md.toString(16);
            System.out.println("publicExponent:" + publicExponent);
            System.out.println("privateExponent:" + privateExponent);
            System.out.println("modulus:" + modulus);

        } catch (Exception igr)
        {
            igr.printStackTrace();
        }
    }

    public static String sign(final String plainData) throws Exception
    {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(new BigInteger(modulus, 16),
                new BigInteger(privateExponent, 16));
        PrivateKey privateKey =   keyFactory.generatePrivate(keySpec);  
        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initSign(privateKey);
        signature.update(plainData.getBytes("UTF-8"));
        return Base64.getEncoder().encodeToString(signature.sign());
    }

    public static boolean verify(final String plainData, final String sign) throws Exception
    {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(new BigInteger(modulus, 16),
                new BigInteger(publicExponent, 16));
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        final Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initVerify(publicKey);
        signature.update(plainData.getBytes("UTF-8"));
        return signature.verify(Base64.getDecoder().decode(sign));
    }

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception
    {
        String plainData = "ni hao ,今天天气不错";
        String sign = sign(plainData);
        System.out.println(sign);//ENRI/kCR/KCrQGyh9zf70Ce5RQtOg/9CJonPohvUgMUAOXeffVwHQCMZN/FxMf8+iTQUdOiDnm3RS9OWTwlUtQ==
        System.out.println(verify(plainData, sign));
     
    }

c# Using

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;

c# Test

static String publicExponent = "10001";
        static String privateExponent = "78bf10f3b7274ddd04da85a2b737dc524f717e765c16515f25479cf7db4627b116da3198fd54ef3732795cb9164029acd8e6c76d6bfa27bd9f433ad88ee7c2bd";
        static String modulus = "bddf972718f690b4d9e85de3a3a854ed4b72640914fb1073d07319687dbc8fc2b7b5701cce4cd47bdffd343a0e5b2762e572be07aa5c8d1201a035cc440c77a5";

        static void Main(string[] args)
        {
            string plainData = "ni hao ,今天天气不错"; 
            string signText = sign(plainData);
            Console.WriteLine(signText);
            Console.WriteLine(verify(plainData, signText));//ENRI/kCR/KCrQGyh9zf70Ce5RQtOg/9CJonPohvUgMUAOXeffVwHQCMZN/FxMf8+iTQUdOiDnm3RS9OWTwlUtQ==
            //
            Console.WriteLine();

        }
          static string sign( string plainData)  
        {
            ISigner signer = SignerUtilities.GetSigner("SHA1withRSA");
            RsaKeyParameters key = new RsaKeyParameters(true, new BigInteger(modulus,16), new BigInteger(privateExponent,16));
            signer.Init(true, key);
            byte[] plainBytes = Encoding.UTF8.GetBytes(plainData);
            signer.BlockUpdate(plainBytes, 0, plainBytes.Length);
            byte[] signBytes = signer.GenerateSignature();  
            return Convert.ToBase64String(signBytes);
        }
        static bool verify(string plainData, string sign)
        {
            ISigner signer = SignerUtilities.GetSigner("SHA1withRSA");
            RsaKeyParameters key = new RsaKeyParameters(false, new BigInteger(modulus,16), new BigInteger(publicExponent,16));
            signer.Init(false, key);
            byte[] signBytes = Convert.FromBase64String(sign);
            byte[] plainBytes = Encoding.UTF8.GetBytes(plainData);
            signer.BlockUpdate(plainBytes, 0, plainBytes.Length);
            bool ret = signer.VerifySignature(signBytes);
            return ret;
        }
收获园豆:20
TCG2008 | 小虾三级 |园豆:1145 | 2017-05-26 23:06

需要引用什么dll呢

EApple | 园豆:139 (初学一级) | 2017-05-27 16:40

@EApple: 已经知道了,十分感谢,我纠结了2天了。。。好多方法都试过了,分不多,真的十分感谢

【引用的:

http://www.bouncycastle.org/csharp/index.html

下载bin的zip文件。

工程添加引用“BouncyCastle.Crypto.dll”】

EApple | 园豆:139 (初学一级) | 2017-05-27 17:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册