com.mycomm.itool.security.RSAVerify Maven / Gradle / Ivy
The newest version!
package com.mycomm.itool.security;
import com.mycomm.itool.SystemUtil;
import com.mycomm.itool.utils.Base58;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;
public class RSAVerify {
/**
*
log.info("signature:{}",signature);
try {
Signature sig = Signature.getInstance("MD5WithRSA");
sig.initVerify(cloudUserPublicKey);
sig.update(token.getBytes(StandardCharsets.UTF_8));
byte[]sigBytes = Base58.decode(signature);
// byte[]sigBytes = Base64.getDecoder().decode(signature);
verified = sig.verify(sigBytes);
} catch (SignatureException se) {
se.printStackTrace();
verified = false;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
verified = false;
} catch (InvalidKeyException e) {
e.printStackTrace();
verified = false;
}catch (IllegalArgumentException e){
log.error("the provided data signature is not in well base64-urlencoded format!");
verified = false;
}
if (verified) {
log.info("\nSignature verified.");
} else {
log.warn("\nSignature did not match.");
}
*/
public static boolean verifyBase58Signature(String data, String signature, PublicKey publicKey){
boolean verified = false;
try {
Signature sig = Signature.getInstance("MD5WithRSA");
sig.initVerify(publicKey);
sig.update(data.getBytes(SystemUtil.default_charSet));
byte[]sigBytes = Base58.decode(signature);
verified = sig.verify(sigBytes);
} catch (SignatureException se) {
se.printStackTrace();
verified = false;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
verified = false;
} catch (InvalidKeyException e) {
e.printStackTrace();
verified = false;
}catch (IllegalArgumentException e){
verified = false;
} catch (UnsupportedEncodingException e) {
verified = false;
}
return verified;
}
public static boolean verifyBase64Signature(String data, String signature, PublicKey publicKey){
boolean verified = false;
try {
Signature sig = Signature.getInstance("MD5WithRSA");
sig.initVerify(publicKey);
sig.update(data.getBytes(SystemUtil.default_charSet));
//byte[]sigBytes = Base58.decode(signature);
byte[]sigBytes = Base64.getDecoder().decode(signature);
verified = sig.verify(sigBytes);
} catch (SignatureException se) {
se.printStackTrace();
verified = false;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
verified = false;
} catch (InvalidKeyException e) {
e.printStackTrace();
verified = false;
}catch (IllegalArgumentException e){
verified = false;
} catch (UnsupportedEncodingException e) {
verified = false;
}
return verified;
}
}