Java RSA generates the public key private key, and javarsa generates

Source: Internet
Author: User

Java RSA generates the public key private key, and javarsa generates

/*** The imported package is a jar package that comes with Java * Key-related package * base64 codec * encoding only */import java. security. key; import java. security. keyPair; import java. security. keyPairGenerator; import java. security. interfaces. rsw.vatekey; import java. security. interfaces. RSAPublicKey; import java. util. hashMap; import java. util. map; import sun. misc. BASE64Decoder; import sun. misc. BASE64Encoder; public class CreateSecrteKey {public class Keys {} public static final String KEY_ALGORITHM = "RSA"; // public static final String SIGNATURE_ALGORITHM = "MD5withRSA "; private static final String PUBLIC_KEY = "RSAPublicKey"; private static final String PRIVATE_KEY = "RSAPrivateKey"; // obtain the public key public static String getPublicKey (Map <String, Object> keyMap) throws Exception {// get the public key object in the map and convert it to the Key object key = (Key) keyMap. get (PUBLIC_KEY); // byte [] publicKey = key. getEncoded (); // The return string for encoding: return encryptBASE64 (key. getEncoded ();} // obtain the private key public static String getPrivateKey (Map <String, Object> keyMap) throws Exception {// get the private key object in the map to the Key object key = (Key) keyMap. get (PRIVATE_KEY); // byte [] privateKey = key. getEncoded (); // The return string for encoding: return encryptBASE64 (key. getEncoded ();} // return byte public static byte [] decryptBASE64 (String key) throws Exception {return (new BASE64Decoder ()). decodeBuffer (key);} // The encoded return String public static String encryptBASE64 (byte [] key) throws Exception {return (new BASE64Encoder ()). encodeBuffer (key);} // The public/private key public static map <String, Object> initKey () is stored in the Map Object () throws Exception {// obtain the 1024-byte KeyPairGenerator keyPairGen = KeyPairGenerator of the object's KeyPairGenerator parameter RSA. getInstance (KEY_ALGORITHM); keyPairGen. initialize (1024); // obtain the object KeyPair keyPair = keyPairGen through the object KeyPairGenerator. generateKeyPair (); // obtain the RSA Public/Private Key through the KeyPair object RSAPublicKey rs?vatekey RSAPublicKey publicKey = (RSAPublicKey) keyPair. getPublic (); rsw.vatekey privateKey = (rsw.vatekey) keyPair. getPrivate (); // The public/private key Object is stored in map. Map <String, Object> keyMap = new HashMap <String, Object> (2); keyMap. put (PUBLIC_KEY, publicKey); keyMap. put (PRIVATE_KEY, privateKey); return keyMap;} public static void main (String [] args) {Map <String, Object> keyMap; try {keyMap = initKey (); string publicKey = getPublicKey (keyMap); System. out. println (publicKey); String privateKey = getPrivateKey (keyMap); System. out. println (privateKey);} catch (Exception e) {e. printStackTrace ();}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.