I am trying to generate public key and signature in readable format which can be used in rest service .i have write following code .
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();
}
}
But this is not generating in alphanumeric value .how can i achieve that .please help me
如果要字节数组的十六进制表示,请使用
javax.xml.bind.DatatypeConverter.printHexBinary(byte[] array)
You can use base64 for communicating over rest. Another example is HTTP Basic authentication that encode password using base64 to putting it in HTTP header
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.