我正在尝试以可读格式生成公钥和签名,可以在休息服务中使用。我已经编写了以下代码。
public static void GenearetSignature(String url,String signatureValue,String publicKey){
KeyPairGenerator keyGen;
try {
keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
System.out.println("Priavte key"+priv);
System.out.println("Public key");
Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
dsa.initSign(priv);
dsa.update(url.getBytes());
byte[] signatureValueArray = dsa.sign();
signatureValue=new String(signatureValueArray,"UTF8");
byte[] publicKeyArray=pub.getEncoded();
publicKey = new String(publicKeyArray);
System.out.println("publicKey is "+publicKey);
System.out.println("signatureValue is "+signatureValue);
} catch (NoSuchAlgorithmException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
}
但这不会产生字母数字值。我可以实现这一点。请帮助我
答案 0 :(得分:1)
如果要使用字节数组的十六进制表示
javax.xml.bind.DatatypeConverter.printHexBinary(byte[] array)
答案 1 :(得分:0)
您可以使用base64进行静态通信。另一个例子是HTTP Basic身份验证,它使用base64对密码进行编码,将其置于HTTP头
中