欢迎来到天天文库
浏览记录
ID:38215014
大小:567.89 KB
页数:6页
时间:2019-06-07
《JAVA实现RSA非对称性密码加密与解密》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、创建RSAKeyMap.java类packagetest_time;importjava.security.Key;importjava.security.KeyPair;importjava.security.KeyPairGenerator;importjava.security.interfaces.RSAPrivateKey;importjava.security.interfaces.RSAPublicKey;importjava.util.HashMap;importjava.util.Map;importsun.m
2、isc.BASE64Decoder;importsun.misc.BASE64Encoder;@SuppressWarnings("unused")publicclassRSAKeyMap{publicstaticfinalStringKEY_ALGORITHM="RSA";publicstaticfinalStringSIGNATURE_ALGORITHM="MD5withRSA";privatestaticfinalStringPUBLIC_KEY="RSAPublicKey";privatestaticfinalStrin
3、gPRIVATE_KEY="RSAPrivateKey";publicstaticStringgetPublicKey(MapkeyMap)throwsException{Keykey=(Key)keyMap.get(PUBLIC_KEY);byte[]publicKey=key.getEncoded();returnencryptBASE64(key.getEncoded());}publicstaticStringgetPrivateKey(MapkeyMap)th
4、rowsException{Keykey=(Key)keyMap.get(PRIVATE_KEY);byte[]privateKey=key.getEncoded();returnencryptBASE64(key.getEncoded());}publicstaticbyte[]decryptBASE64(Stringkey)throwsException{return(newBASE64Decoder()).decodeBuffer(key);}publicstaticStringencryptBASE64(byte[]ke
5、y)throwsException{return(newBASE64Encoder()).encodeBuffer(key);}publicstaticMapinitKey()throwsException{KeyPairGeneratorkeyPairGen=KeyPairGenerator.getInstance(KEY_ALGORITHM);keyPairGen.initialize(1024);KeyPairkeyPair=keyPairGen.generateKeyPair();RSAPu
6、blicKeypublicKey=(RSAPublicKey)keyPair.getPublic();RSAPrivateKeyprivateKey=(RSAPrivateKey)keyPair.getPrivate();MapkeyMap=newHashMap(2);keyMap.put(PUBLIC_KEY,publicKey);keyMap.put(PRIVATE_KEY,privateKey);returnkeyMap;}/*publicstaticvoidma
7、in(String[]args){MapkeyMap;try{keyMap=initKey();StringpublicKey=getPublicKey(keyMap);System.out.println(publicKey);StringprivateKey=getPrivateKey(keyMap);System.out.println(privateKey);}catch(Exceptione){e.printStackTrace();}}*/}测试截图将main方法注释掉创建RSA.jav
8、a类packagetest_time;importjava.io.UnsupportedEncodingException;importjava.security.InvalidKeyException;importjava.security.KeyFactor
此文档下载收益归作者所有