java RSA 產生公開金鑰私密金鑰,javarsa產生

來源:互聯網
上載者:User

java RSA 產生公開金鑰私密金鑰,javarsa產生

/** * 引進的包都是Java內建的jar包 * 秘鑰相關包 * base64 編解碼 * 這裡只用到了編碼 */import java.security.Key;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.interfaces.RSAPrivateKey;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";    //獲得公開金鑰    public static String getPublicKey(Map<String, Object> keyMap) throws Exception {        //獲得map中的公開金鑰對象 轉為key對象        Key key = (Key) keyMap.get(PUBLIC_KEY);        //byte[] publicKey = key.getEncoded();        //編碼返回字串        return encryptBASE64(key.getEncoded());    }    //獲得私密金鑰    public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {        //獲得map中的私密金鑰對象 轉為key對象        Key key = (Key) keyMap.get(PRIVATE_KEY);        //byte[] privateKey = key.getEncoded();        //編碼返回字串        return encryptBASE64(key.getEncoded());    }    //解碼返回byte    public static byte[] decryptBASE64(String key) throws Exception {        return (new BASE64Decoder()).decodeBuffer(key);    }    //編碼返回字串    public static String encryptBASE64(byte[] key) throws Exception {        return (new BASE64Encoder()).encodeBuffer(key);    }        //map對象中存放公私密金鑰    public static Map<String, Object> initKey() throws Exception {        //獲得對象 KeyPairGenerator 參數 RSA 1024個位元組        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);        keyPairGen.initialize(1024);        //通過對象 KeyPairGenerator 擷取對象KeyPair        KeyPair keyPair = keyPairGen.generateKeyPair();                //通過對象 KeyPair 擷取RSA公私密金鑰對象RSAPublicKey RSAPrivateKey        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();        //公私密金鑰對象存入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();        }    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.