Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.bouncycastle.openssl.PEMParser;
- import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
- import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
- import java.io.StringReader;
- import java.util.Base64;
- import java.security.interfaces.RSAPublicKey;
- import java.security.Signature;
- import java.security.spec.PSSParameterSpec;
- import java.security.spec.MGF1ParameterSpec;
- import java.security.MessageDigest;
- public class MyTest
- {
- public static void main(String[] args) throws Exception
- {
- String plaintext = "zzz";
- String signature = "JwqW/Xhh1hFxP5pGJAKkdVM+6WZ5FtQuPdwlmDq+pmJXknIybW4f31w7lJiBvc2VL8fNXg1DllwuwyCnErKRSygDGwdzkHJ/chvrjUequhiqoPhgKe3vQCFvJdlbeUEkF2Ho2qK5xU0VI3ViS1htDuQXJvCHm30wO+zgW9kshCE=";
- String key = "-----BEGIN RSA PUBLIC KEY-----\n" +
- "MIGJAoGBAM5iHBWEep6wz0o6PrD0MdmjuO2SJivi0Ik01eFZn3GuyEpUvMI1eLtH\n" +
- "77wFORzI2eQTc2sGYWctEZk4k/Im91TFW0ahYyeB2m1XQ/cSY8RO9nyrWiGPJjzI\n" +
- "FuePuh8dqWHT2hGDfD9CmMmz7Zb+fltmSZ3siF9XbWyUTnemQpOtAgMBAAE=\n" +
- "-----END RSA PUBLIC KEY-----\n";
- // load the public key
- PEMParser pemParser = new PEMParser(new StringReader(key));
- JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
- SubjectPublicKeyInfo pub = SubjectPublicKeyInfo.getInstance(pemParser.readObject());
- RSAPublicKey publicKey = (RSAPublicKey) converter.getPublicKey(pub);
- // load the plaintext
- byte[] signatureBytes = Base64.getDecoder().decode(signature);
- // perform the actual signature verification
- Signature sig = Signature.getInstance("RSASSA-PSS");
- PSSParameterSpec pssParams = new PSSParameterSpec(
- "SHA-256",
- "MGF1",
- new MGF1ParameterSpec("SHA-1"),
- MessageDigest.getInstance("SHA-256").getDigestLength(),
- PSSParameterSpec.TRAILER_FIELD_BC
- );
- sig.setParameter(pssParams);
- sig.initVerify(publicKey);
- sig.update(plaintext.getBytes());
- System.out.println(sig.verify(signatureBytes) ? "good" : "bad");
- }
- }
Add Comment
Please, Sign In to add comment