資源描述:
《RSA加密算法的java實現(xiàn).docx》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、packagecom.encryp;importjava.security.MessageDigest;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;publicclassCoder{publicstaticfinalStringKEY_SHA="SHA";publicstaticfinalStringKEY_MD5="MD5";/***BASE64解密*@paramkey*@return*@throwsException*/publicstati
2、cbyte[]decryptBASE64(Stringkey)throwsException{return(newBASE64Decoder()).decodeBuffer(key);}/***BASE64加密*@paramkey*@return*@throwsException*/publicstaticStringencryptBASE64(byte[]key)throwsException{return(newBASE64Encoder()).encodeBuffer(key);}/***MD5加密*@para
3、mdata*@return*@throwsException*/publicstaticbyte[]encryptMD5(byte[]data)throwsException{MessageDigestmd5=MessageDigest.getInstance(KEY_MD5);md5.update(data);returnmd5.digest();}/***SHA加密*@paramdata*@return*@throwsException*/publicstaticbyte[]encryptSHA(byte[]da
4、ta)throwsException{MessageDigestsha=MessageDigest.getInstance(KEY_SHA);sha.update(data);returnsha.digest();}}packagecom.encryp;importjava.security.Key;importjava.security.KeyFactory;importjava.security.KeyPair;importjava.security.KeyPairGenerator;importjava.sec
5、urity.PrivateKey;importjava.security.PublicKey;importjava.security.Signature;importjava.security.interfaces.RSAPrivateKey;importjava.security.interfaces.RSAPublicKey;importjava.security.spec.PKCS8EncodedKeySpec;importjava.security.spec.X509EncodedKeySpec;import
6、java.util.HashMap;importjava.util.Map;importjavax.crypto.Cipher;publicclassRsaUtilextendsCoder{publicstaticfinalStringKEY_ALGORTHM="RSA";publicstaticfinalStringSIGNATURE_ALGORITHM="MD5withRSA";publicstaticfinalStringPUBLIC_KEY="RSAPublicKey";//公鑰publicstaticfin
7、alStringPRIVATE_KEY="RSAPrivateKey";//私鑰/***初始化密鑰**@return*@throwsException*/publicstaticMapinitKey()throwsException{KeyPairGeneratorkeyPairGenerator=KeyPairGenerator.getInstance(KEY_ALGORTHM);keyPairGenerator.initialize(1024);KeyPairkeyPair=keyP
8、airGenerator.generateKeyPair();//公鑰RSAPublicKeypublicKey=(RSAPublicKey)keyPair.getPublic();//私鑰RSAPrivateKeyprivateKey=(RSAPrivateKey)keyPair.getPrivate();Map