Ola, a todos,
Estou tentando fazer um método que verifica se o texto que tem a assinatura gerada está certo, estou usando o seguinte código abaixo:
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.bouncycastle.bcpg.ArmoredInputStream;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureList;
import org.bouncycastle.openpgp.PGPUtil;
public class FileVerify {
public static void main(String[] args) throws Exception {
new FileVerify ().verifyFile(new FileInputStream("C:\\Users\\helder\\Desktop\\assinado.txt"), readPublicKey(new FileInputStream("C:\\Users\\helder\\Desktop\\public.key")));
}
private static PGPPublicKey readPublicKey(InputStream in)
throws IOException, PGPException {
in = PGPUtil.getDecoderStream(in);
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in);
Iterator rIt = pgpPub.getKeyRings();
while (rIt.hasNext()) {
PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next();
Iterator kIt = kRing.getPublicKeys();
while (kIt.hasNext()) {
PGPPublicKey k = (PGPPublicKey) kIt.next();
if (k.isEncryptionKey()) {
return k;
}
}
}
throw new IllegalArgumentException(
"Can't find encryption key in key ring.");
}
public boolean verifyFile(InputStream fileStreamIn, PGPPublicKey keyIn)
throws Exception {
boolean verify = false;
ArmoredInputStream aIn = new ArmoredInputStream(fileStreamIn, true);
//
// read the input, making sure we ingore the last newline.
//
int ch;
boolean newLine = false;
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
while ((ch = aIn.read()) >= 0 && aIn.isClearText()) {
if (newLine) {
bOut.write((byte) 'n');
newLine = false;
}
if (ch == 'n') {
newLine = true;
continue;
}
bOut.write((byte) ch);
}
PGPObjectFactory pgpFact = new PGPObjectFactory(aIn);
Object o = pgpFact.nextObject();
if (o instanceof PGPSignatureList) {
PGPSignatureList list = (PGPSignatureList) o;
if (list.size() > 0) {
PGPSignature sig = list.get(0);
sig.initVerify(keyIn, "BC");
sig.update(bOut.toByteArray());
verify = sig.verify();
}
}
return verify;
}
}
O problema é que ao rodar o código abaixo dá o seguinte erro:
Exception in thread "main" java.security.NoSuchProviderException: provider BC not found.
at org.bouncycastle.openpgp.PGPUtil.getProvider(Unknown Source)
at org.bouncycastle.openpgp.PGPSignature.initVerify(Unknown Source)
at test3.FileVerify .verifyFile(ClearSignedFileVerify.java:154)
at test3.FileVerify .main(ClearSignedFileVerify.java:37)
Alguem tem idéia de como fazer isso???
Estou enviando os arquivos que tem a chave privada e a chave pública, para poder verificar se o arquivo assinado está correto.