简体   繁体   中英

How to convert Key to String and vice versa

I'm very new to Java Security and hope you guys can do me a favor by providing solution for me. I have a method as stated below:

public Key keyGen() throws NoSuchAlgorithmException{
    KeyGenerator keyGen = KeyGenerator.getInstance("DES");
    keyGen.init(56);
    Key key = keyGen.generateKey();
    return key;
}

My objective is to convert this Key type value to either byte[] type or String type and of course, convert them back to Key type again for decryption purpose. What's the ideal solution available for me?

P/S: This method will be implemented in Android Apps and Key value are to pass from activity to activity.

Key has a getEncoded() method to convert to byte[]. Don't use String for this. String is not a container for binary data.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM