user1047873
user1047873

Reputation: 268

How to convert public key and signature value in human readable format

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

Upvotes: 1

Views: 1062

Answers (2)

Milad
Milad

Reputation: 89

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

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136112

If you want hexadecimal represenation of byte array use

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

Upvotes: 1

Related Questions