klebermo

Untitled

Jul 14th, 2023 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. package util;
  2.  
  3. import java.nio.charset.StandardCharsets;
  4. import java.security.PrivateKey;
  5. import java.security.PublicKey;
  6. import java.security.Signature;
  7. import java.util.Base64;
  8.  
  9. public class RSASignature {
  10.     public static byte[] sign(String plainText, PrivateKey privateKey) throws Exception {
  11.         Signature signature = Signature.getInstance("SHA256withRSA");
  12.         signature.initSign(privateKey);
  13.         signature.update(plainText.getBytes());
  14.         return signature.sign();
  15.     }
  16.  
  17.     public static boolean verify(String message, String cipherText, PublicKey publicKey) throws Exception {
  18.         byte[] signature =  Base64.getDecoder().decode(cipherText);
  19.         Signature verifier = Signature.getInstance("SHA256withRSA");
  20.         verifier.initVerify(publicKey);
  21.         verifier.update(message.getBytes(StandardCharsets.UTF_8));
  22.         return verifier.verify(signature);
  23.     }    
  24. }
  25.  
Add Comment
Please, Sign In to add comment