我已经生成了一个私钥:
openssl genrsa [-out file] –des3
Run Code Online (Sandbox Code Playgroud)
在此之后,我生成了一个公钥:
openssl rsa –pubout -in private.key [-out file]
Run Code Online (Sandbox Code Playgroud)
我想用我的私钥签署一些消息,并使用我的公钥验证其他一些消息,使用如下代码:
public String sign(String message) throws SignatureException{
try {
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initSign(privateKey);
sign.update(message.getBytes("UTF-8"));
return new String(Base64.encodeBase64(sign.sign()),"UTF-8");
} catch (Exception ex) {
throw new SignatureException(ex);
}
}
public boolean verify(String message, String signature) throws SignatureException{
try {
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initVerify(publicKey);
sign.update(message.getBytes("UTF-8"));
return sign.verify(Base64.decodeBase64(signature.getBytes("UTF-8")));
} catch (Exception ex) {
throw new SignatureException(ex);
}
}
Run Code Online (Sandbox Code Playgroud)
我找到了将我的私钥转换为PKCS8格式并加载它的解决方案.它适用于这样的一些代码:
public PrivateKey getPrivateKey(String filename) throws Exception {
File f …Run Code Online (Sandbox Code Playgroud) 我使用rsa密钥加密一个长字符串,我将发送到我的服务器(将使用服务器的公钥和我的私钥加密它)但它抛出一个异常,就像javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes
我觉得我还没有理解rsa的工作到现在为止(使用内置库是导致此问题的原因).
有人可以解释为什么抛出这个异常.是不是可以发送长字符串加密?