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 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号