首页 新闻 会员 周边 捐助

RSA疑问: Java私钥加密, C#公钥解密,有好的解决方案么?

0
悬赏园豆:5 [待解决问题]

各位大神帮帮忙:
有下面两段java代码:

证书签名方法(加密)
    1)将data(根据规范要求决定是否和timestamp拼接在一起)做hash
    2)使用用户私钥key对hash做加密。
    3)加密的结果用hex编码
其中data代表要签名的数据,timestamp代表时间戳,key代表证书的私钥
public class Sign implements ISign {
    public String sign(String data, long timestamp, PrivateKey key) throws Exception {
        return sign(data.getBytes("utf-8"), timestamp, key);
    }
    public String sign(String data, PrivateKey key) throws Exception{
        return sign(data.getBytes("utf-8"), 0, key);
    }
    public String sign(byte [] data, PrivateKey key) throws Exception {
        return sign(data, 0, key);
    }
    public String sign(byte [] data, long timestamp, PrivateKey key) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(data);
        if(timestamp > 0){
            md.update(EncodeUtil.toBE(timestamp));
        }

        byte[] hash = md.digest();
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encrypted = cipher.doFinal(hash);
        return HexBin.encode(encrypted);
    }
}

    EncodeUtil.toBE:
    public class EncodeUtil {
        public static byte[] toBE(long data) {
            String ts = String.valueOf(data);
            if (ts.length() >= 13){ 
                //平台http协议加密用,平台时间戳毫秒13位
                byte[] buffer = new byte[8];
                buffer[0] = (byte)(data >>> 56);
            buffer[1] = (byte)(data >>> 48);
                buffer[2] = (byte)(data >>> 40);
                buffer[3] = (byte)(data >>> 32);
                buffer[4] = (byte)(data >>> 24);
                buffer[5] = (byte)(data >>> 16);
                buffer[6] = (byte)(data >>>  8);
                buffer[7] = (byte)(data >>>  0);
            }else{ //终端tcp协议加密用,终端时间戳秒10位
                byte[] buffer = new byte[4];
                buffer[0] = (byte)(data >>> 24);
                buffer[1] = (byte)(data >>> 16);
                buffer[2] = (byte)(data >>> 8);
                buffer[3] = (byte)(data >>> 0);
            }
            return buffer;
        }
    }
验证签名方法(解密)
    1)将data和timestamp(如果有)拼接在一起做hash
    2)对encodedEncryptedStr做hex解码
    3)使用证书验证数据的有效性(比较hash)
其中data代表要被解密的数据,timestamp代表时间戳,encodedEncryptedStr代表签名之后的串,userCert代表用公钥生成的X509Certificate对象。
public class Verify implements IVerify {
    public boolean verify(String data, long timestamp, String encodedEncryptedStr,
            X509Certificate userCert) throws Exception 
    {
        return verify(data.getBytes("utf-8"), timestamp, encodedEncryptedStr, userCert);
    }
    public boolean verify(String data, String encodedEncryptedStr,
            X509Certificate userCert) throws Exception
    {
        return verify(data.getBytes("utf-8"), 0, encodedEncryptedStr, userCert);
    }
    public boolean verify(byte [] data, String encodedEncryptedStr,
            X509Certificate userCert) throws Exception{
        return verify(data, encodedEncryptedStr, userCert);
    }
    public boolean verify(byte [] data, long timestamp, String encodedEncryptedStr,
            X509Certificate userCert) throws Exception
    {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(data);
        if(timestamp > 0){
            md.update(EncodeUtil.toBE(timestamp));
        }

        byte[] hash = md.digest();
        byte[] encryptedStr = HexBin.decode(encodedEncryptedStr);

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, userCert);
        byte[] plain = cipher.doFinal(encryptedStr);
        boolean ok = Arrays.equals(hash, plain);

        return ok;
    }
}

现在需要将第二部分 解密用c# 实现 ,有什么好的思路吗?真心求教!

karo.smile的主页 karo.smile | 初学一级 | 园豆:197
提问于:2017-02-14 16:05
< >
分享
所有回答(1)
0

如果Java的加密是基于OpenSSL,可以试试这里的方法:C#中使用OpenSSL的公钥加密/私钥解密

dudu | 园豆:29689 (高人七级) | 2017-02-14 16:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册