我正在尝试对 http - web 响应进行数字签名。本质上,我创建了 HTML 和多部分内容类型的响应,对响应进行签名,然后将数字签名附加到响应中。我想我已经接近但走了几步,因为这不是真正的 PGP 签名,因为附加的签名实际上是 HEXtoString。重要的是能够正确表示签名,以便可以正确解释响应。可以在这里使用一些建议,因为我对此很满意。在此先感谢.. 下面是我现在使用的代码片段。
StringBuffer myResponse = new StringBuffer("");
myResponse.append(getHttpHeader());
KeyPair pair2 = loadKeyPair();//loads a key pair from generated files
if (signer==null)
signer = Signature.getInstance("MD5withRSA");
signer.initSign(pair2.getPrivate());
signer.update(message.getBytes());
byte[] b = signer.sign();
FileOutputStream sigfos = new FileOutputStream(getFileLocation(0,localTest));
sigfos.write(b);
sigfos.close();
//verify
signer.initVerify(pair2.getPublic());//pubKey);
signer.update(message.getBytes());
if (signer.verify(b)){
myResponse.append(message);
}
StringBuffer signed= new StringBuffer("");
signed.append(boundary);
signed.append(CRLF);
signed.append("content-type: application/pgp-signature");
signed.append(CRLF);
signed.append("-----BEGIN PGP MESSAGE-----");
signed.append(CRLF);
signed.append("Version: 1");//update this
signed.append(CRLF);
signed.append(CRLF);
signed.append(digSignature);//generated as HexString representation of signed file from above
signed.append(CRLF);
signed.append("-----END PGP MESSAGE-----");
signed.append(CRLF);
signed.append(boundary+"--");
myResponse.append (signed);
ServletOutputStream.println(myResponse);
传输的结果“签名”是签名文件的字节散列 hexToString 表示。我正在使用标准的 java 类,但不确定其他库是否会给我一个真正的 PGP 表示,其中包含 0-9a-f 表示之外的字符。想法??