デジタル署名を.txtファイルに適用しようとしています。デジタル署名は正常に適用されますが、検証するたびに検証済み:偽と表示されます。SHA1withRSAを使用したJavaデジタル署名
public void signData(){
Signature rsa = Signature.getInstance("SHA1withRSA");
rsa.initSign(privateKey);
File f= new File(path);
//read from file
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[(int) f.length()];
fis.read(buffer);
fis.close();
rsa.update(buffer);
//write to file
byte[] toWrite=rsa.sign();
String signPath;
signPath="Signed-"+f.getName();
File output=new File(signPath);
FileOutputStream fos = new FileOutputStream(output);
fos.write(toWrite);
fos.flush();
fos.close();
System.out.printf("File: %s is now signed in: %s\n\n",path,signPath);
}
リーディングと検証:エラーが示されていない
public void verify(){
Signature sig = Signature.getInstance("SHA1withRSA");
sig.initVerify(publicKey);
File f= new File(path);
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[(int) f.length()];
fis.read(buffer);
fis.close();
sig.update(buffer);
System.out.println("Verified: "+sig.verify(buffer));
}
は、ここに私の署名のためのコードです。使用されるKeyPairアルゴリズムは「RSA」です。 ありがとうございます。
あなたのファイルは完全に間違っています。 – Kayaman
しかし、なぜ?私は暗号と鍵のペアを使って同じ方法で暗号化と復号化を行い、うまくいきます。 – Alb
あなたはそれを間違って書きました。あなたの 'fis.read(バッファ)'の結果を表示し、あなたが読んでいる量を確認してください。これはコードにある唯一の問題ではないかもしれませんが、最も明白で基本的なものです。 – Kayaman