資源描述:
《JAVA實(shí)現(xiàn)RSA非對(duì)稱性密碼加密與解密》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、創(chuàng)建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();}}*/}測(cè)試截圖將main方法注釋掉創(chuàng)建RSA.jav
8、a類packagetest_time;importjava.io.UnsupportedEncodingException;importjava.security.InvalidKeyException;importjava.security.KeyFactor