欢迎来到天天文库
浏览记录
ID:59252774
大小:10.73 KB
页数:15页
时间:2020-09-08
《RSA加密算法的java实现.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
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
此文档下载收益归作者所有