开放API接口及其安全性-飞外

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"; private static final String PUBLIC_KEY = "RSAPublicKey"; private static final String PRIVATE_KEY = "RSAPrivateKey"; //获得公钥 public static String getPublicKey(Map String,Object keyMap) { Key key = (Key) keyMap.get(PUBLIC_KEY); System.out.println(key.toString()); return encryptBASE64(key.getEncoded()); //获得私钥 public static String getPrivateKey(Map String,Object keyMap) { Key key = (Key) keyMap.get(PRIVATE_KEY); return encryptBASE64(key.getEncoded()); //解码返回字符串 public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); public static String encryptBASE64(byte[]key) { return (new BASE64Encoder()).encodeBuffer(key); public static Map String, Object initKey() throws Exception{ KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); Map String, Object keyMap = new HashMap String,Object (2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap;