Encryption 使用公钥的SAML2断言加密(opensaml)

Encryption 使用公钥的SAML2断言加密(opensaml),encryption,public-key-encryption,assertion,saml-2.0,opensaml,Encryption,Public Key Encryption,Assertion,Saml 2.0,Opensaml,我最近尝试使用中继方服务公钥加密Saml2断言。不幸的是,我甚至不能完成测试阶段 这是我的密码 公共类加密测试{ 公共静态void main(字符串参数[]){ 试一试{ //要加密的断言 文件输入流fis; 数据输入流输入,in2; 文件f=新文件(“src/main/resources/AssertionTest”); 字节[]缓冲区=新字节[(int)f.length()]; in=新数据输入流(新文件输入流(f)); in.readfull(缓冲区); in.close(); //断言=

我最近尝试使用中继方服务公钥加密Saml2断言。不幸的是,我甚至不能完成测试阶段

这是我的密码

公共类加密测试{
公共静态void main(字符串参数[]){
试一试{
//要加密的断言
文件输入流fis;
数据输入流输入,in2;
文件f=新文件(“src/main/resources/AssertionTest”);
字节[]缓冲区=新字节[(int)f.length()];
in=新数据输入流(新文件输入流(f));
in.readfull(缓冲区);
in.close();
//断言=DataInputStream.readUTF(in);
_assert中的字符串=新字符串(缓冲区);
System.out.println(in_断言);
org.apache.axiom.om.OMElement-OMElementAssertion=org.apache.axiom.om.util.AXIOMUIL.stringToOM(in_断言);
断言断言=ConvertOmementToAssertion2(OMElementAssertion);
//假设它包含收件人的RSA公钥
凭证密钥加密凭证;
keyEncryptionCredential=getCredentialFromFilePath(“src/main/resources/cert.pem”);
EncryptionParameters encParams=新的EncryptionParameters();
encParams.setAlgorithm(EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128);
KeyEncryptionParameters kekParams=新的KeyEncryptionParameters();
kekParams.setEncryptionCredential(keyEncryptionCredential);
kekParams.setAlgorithm(EncryptionConstants.ALGO_ID_KEYTRANSPORT_rsoaep);
KeyInfoGeneratorFactory kigf=
Configuration.getGlobalSecurity配置()
.getKeyInfoGeneratorManager().getDefaultManager()
.getFactory(keyEncryptionCredential);
setKeyInfoGenerator(kigf.newInstance());
Encrypter samlEncrypter=新加密机(encParams、kekParams);
samlEncrypter.setKeyPlacement(KeyPlacement.PEER);
EncryptedAssertion EncryptedAssertion=samlEncrypter.encrypt(断言);
System.out.println(加密配置);
}捕获(EncryptionException e){
e、 printStackTrace();
}捕获(证书例外e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(键异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(XMLStreamException e2){
//TODO自动生成的捕捉块
e2.printStackTrace();
}
}
公共静态凭据getCredentialFromFilePath(字符串certPath)引发IOException、CertificateException、KeyException{
InputStream inStream=新文件InputStream(certPath);
CertificateFactory cf=CertificateFactory.getInstance(“X.509”);
证书证书=cf.generateCertificate(河道内);
流内关闭();
//“展示你自己!”
System.out.println(cert.toString());
BasicX509Credential cred=新的BasicX509Credential();
cred.setEntityCertificate((java.security.cert.X509Certificate)证书);
cred.setPrivateKey(空);
//System.out.println(cred.toString());
回信;
//return(Credential)org.opensaml.xml.security.SecurityHelper.getSimpleCredential((X509Certificate)cert,privatekey);
}
公共静态断言ConvertOmeElement到Assertion2(OmeElement元素){
元素断言amldoom=(元素)新statxombuilder(doomastractfactory.getOMFactory(),Element.getXMLStreamReader()).getDocumentElement();
试一试{
UnmarshallerFactory UnmarshallerFactory=Configuration.getUnmarshallerFactory();
Unmarshaller Unmarshaller=unmarshallerFactory.getUnmarshaller(Assertion.DEFAULT\u ELEMENT\u NAME);
return(Assertion)unmarshaller.unmarshall(assertionsamdoom);
}捕获(异常e1){
System.out.println(“错误:+e1.toString());
}
返回null;
}
}
我经常在中收到空指针异常

KeyInfoGeneratorFactory kigf=
Configuration.getGlobalSecurity配置()
.getKeyInfoGeneratorManager().getDefaultManager()
.getFactory(keyEncryptionCredential);
setKeyInfoGenerator(kigf.newInstance());

我如何设置GlobalSecurity配置,或者是否有不同的加密断言方法可以工作?

这个问题已经讨论太久了。问题是OpenSaml的初始化。 简单的

帮助解决了问题

DefaultBootstrap.bootstrap();