kiss727 2017-02-14 08:00 采纳率: 0%
浏览 1198

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

各位大神帮帮忙:
有下面两段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# 实现 ,有什么好的思路吗?真心求教!

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-02-14 19:42
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧