1

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

4

2 回答 2

1

如果你想要字节数组的十六进制表示,请使用

javax.xml.bind.DatatypeConverter.printHexBinary(byte[] array)

于 2013-06-12T04:35:06.470 回答
0

您可以使用 base64 进行通信而不是休息。另一个示例是 HTTP Basic 身份验证,它使用 base64 对密码进行编码以将其放入 HTTP 标头中

于 2013-06-12T04:49:31.593 回答