相关疑难解决方法(0)

从文件加载RSA公钥

我已经生成了一个私钥:

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)

java openssl rsa

98
推荐指数
3
解决办法
15万
查看次数

如何从文件加载RSA私钥

我正在研究SAML 1.1断言消费者服务的测试工具.测试必须生成签名的SAMLResponse并将其提交给Base64中编码的ACS.ACS必须能够使用X509公共证书验证签名的消息.

我能够构建SAMLResponse,添加必要的断言等.但是当我尝试签署对象时,我遇到了问题.这是我当前代码的片段:

String certPath = "mycert.pem";
File pubCertFile = new File(certPath);
BufferedInputStream bis = null;
try {
    bis = new BufferedInputStream(new FileInputStream(pubCertFile));
} catch(FileNotFoundException e) {
    throw new Exception("Could not locate certfile at '" + certPath + "'", e);
}
CertificateFactory certFact = null;
Certificate cert = null;
try {
    certFact = CertificateFactory.getInstance("X.509");
    cert = certFact.generateCertificate(bis);
} catch(CertificateException e) {
    throw new Exception("Could not instantiate cert", e);
}
bis.close();
ArrayList<Certificate> certs = new ArrayList<Certificate>();
certs.add(cert);

String keyPath = "mykey.pem"; …
Run Code Online (Sandbox Code Playgroud)

java rsa saml

59
推荐指数
2
解决办法
10万
查看次数

标签 统计

java ×2

rsa ×2

openssl ×1

saml ×1