Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 验证签名,验证返回值为false_Java_Dsa - Fatal编程技术网

Java 验证签名,验证返回值为false

Java 验证签名,验证返回值为false,java,dsa,Java,Dsa,我搜索了论坛和答案,但我无法找出我的问题,我试图验证签名,但它总是返回false,我做错了什么吗?我生成密钥,签名,然后验证它(字节数组不是空的) 您正在签名String data=“aa”因此签名验证将返回false 对数据进行数字签名以检查其完整性(这意味着不更改数据) 我希望这能有所帮助 public void Keygen() throws java.rmi.RemoteException, NoSuchAlgorithmException, IOException, Signatur

我搜索了论坛和答案,但我无法找出我的问题,我试图验证签名,但它总是返回false,我做错了什么吗?我生成密钥,签名,然后验证它(字节数组不是空的)


您正在签名
String data=“aa”String data2=“bb”时,在签名方法中使用code>
因此签名验证将返回false

对数据进行数字签名以检查其完整性(这意味着不更改数据)

我希望这能有所帮助

public void Keygen()  throws java.rmi.RemoteException, NoSuchAlgorithmException, IOException, SignatureException, NoSuchProviderException, InvalidKeyException {
    KeyPairGenerator Keygen = KeyPairGenerator.getInstance("DSA");
    Keygen.initialize(1024, random);

    KeyPair pair = Keygen.generateKeyPair();
    priv = pair.getPrivate();
    pub = pair.getPublic(); 
}

public byte [] sign (int k)throws java.rmi.RemoteException, NoSuchAlgorithmException, SignatureException, InvalidKeyException , NoSuchProviderException
    Signature dsa = Signature.getInstance("SHA1withDSA", "SUN"); 
    dsa.initSign(priv);
    String data = "aa";
    byte[] b = data.getBytes();
    dsa.update(b);
    realSig = dsa.sign();        
    key = pub.getEncoded();
    return realSig;     
}

public int Versig(byte [] sigkeys)  throws java.rmi.RemoteException, NoSuchAlgorithmException, IOException, SignatureException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException{
    byte [] pkb = getenckey();

    KeyFactory kf = KeyFactory.getInstance("DSA");
    PublicKey pubKey = kf.generatePublic(new X509EncodedKeySpec(pkb));              

    /* create a Signature object and initialize it with the public key */
    Signature sig = Signature.getInstance("SHA1withDSA", "SUN");
    sig.initVerify(pubKey);

    String data2 = "bb";
    byte[] c = data2.getBytes();
    sig.update(c);

    boolean verifies = sig.verify(sigkeys);
    System.out.println("1  " + verifies);
    if (verifies == true) {
        System.out.println(" 2 " + verifies);
        return (1);
    } else {
        return (2);
    }