RSA加密算法的java实现.docx
- 文档编号:30320841
- 上传时间:2023-08-13
- 格式:DOCX
- 页数:12
- 大小:15.48KB
RSA加密算法的java实现.docx
《RSA加密算法的java实现.docx》由会员分享,可在线阅读,更多相关《RSA加密算法的java实现.docx(12页珍藏版)》请在冰豆网上搜索。
RSA加密算法的java实现
packagecom.encryp;
importjava.security.MessageDigest;
importsun.misc.BASE64Decoder;
importsun.misc.BASE64Encoder;
publicclassCoder{
publicstaticfinalStringKEY_SHA="SHA";
publicstaticfinalStringKEY_MD5="MD5";
/**
*BASE64解密
*@paramkey
*@return
*@throwsException
*/
publicstaticbyte[]decryptBASE64(Stringkey)throwsException{
return(newBASE64Decoder()).decodeBuffer(key);
}
/**
*BASE64加密
*@paramkey
*@return
*@throwsException
*/
publicstaticStringencryptBASE64(byte[]key)throwsException{
return(newBASE64Encoder()).encodeBuffer(key);
}
/**
*MD5加密
*@paramdata
*@return
*@throwsException
*/
publicstaticbyte[]encryptMD5(byte[]data)throwsException{
MessageDigestmd5=MessageDigest.getInstance(KEY_MD5);
md5.update(data);
returnmd5.digest();
}
/**
*SHA加密
*@paramdata
*@return
*@throwsException
*/
publicstaticbyte[]encryptSHA(byte[]data)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.security.PrivateKey;
importjava.security.PublicKey;
importjava.security.Signature;
importjava.security.interfaces.RSAPrivateKey;
importjava.security.interfaces.RSAPublicKey;
importjava.security.spec.PKCS8EncodedKeySpec;
importjava.security.spec.X509EncodedKeySpec;
importjava.util.HashMap;
importjava.util.Map;
importjavax.crypto.Cipher;
publicclassRsaUtilextendsCoder{
publicstaticfinalStringKEY_ALGORTHM="RSA";
publicstaticfinalStringSIGNATURE_ALGORITHM="MD5withRSA";
publicstaticfinalStringPUBLIC_KEY="RSAPublicKey";//公钥
publicstaticfinalStringPRIVATE_KEY="RSAPrivateKey";//私钥
/**
*初始化密钥
*
*@return
*@throwsException
*/
publicstaticMap
KeyPairGeneratorkeyPairGenerator=KeyPairGenerator.getInstance(KEY_ALGORTHM);
keyPairGenerator.initialize(1024);
KeyPairkeyPair=keyPairGenerator.generateKeyPair();
//公钥
RSAPublicKeypublicKey=(RSAPublicKey)keyPair.getPublic();
//私钥
RSAPrivateKeyprivateKey=(RSAPrivateKey)keyPair.getPrivate();
Map
(2);
keyMap.put(PUBLIC_KEY,publicKey);
keyMap.put(PRIVATE_KEY,privateKey);
returnkeyMap;
}
/**
*取得公钥,并转化为String类型
*
*@paramkeyMap
*@return
*@throwsException
*/
publicstaticStringgetPublicKey(Map
Keykey=(Key)keyMap.get(PUBLIC_KEY);
returnencryptBASE64(key.getEncoded());
}
/**
*取得私钥,并转化为String类型
*
*@paramkeyMap
*@return
*@throwsException
*/
publicstaticStringgetPrivateKey(Map
Keykey=(Key)keyMap.get(PRIVATE_KEY);
returnencryptBASE64(key.getEncoded());
}
/**
*用私钥加密
*
*@paramdata
*加密数据
*@paramkey
*密钥
*@return
*@throwsException
*/
publicstaticbyte[]encryptByPrivateKey(byte[]data,Stringkey)throwsException{
//解密密钥
byte[]keyBytes=decryptBASE64(key);
//取私钥
PKCS8EncodedKeySpecpkcs8EncodedKeySpec=newPKCS8EncodedKeySpec(keyBytes);
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORTHM);
KeyprivateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
//对数据加密
Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE,privateKey);
returncipher.doFinal(data);
}
/**
*用私钥解密
*
*@paramdata
*加密数据
*@paramkey
*密钥
*@return
*@throwsException
*/
publicstaticbyte[]decryptByPrivateKey(byte[]data,Stringkey)throwsException{
//对私钥解密
byte[]keyBytes=decryptBASE64(key);
PKCS8EncodedKeySpecpkcs8EncodedKeySpec=newPKCS8EncodedKeySpec(keyBytes);
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORTHM);
KeyprivateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
//对数据解密
Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE,privateKey);
returncipher.doFinal(data);
}
/**
*用公钥加密
*
*@paramdata
*加密数据
*@paramkey
*密钥
*@return
*@throwsException
*/
publicstaticbyte[]encryptByPublicKey(byte[]data,Stringkey)throwsException{
//对公钥解密
byte[]keyBytes=decryptBASE64(key);
//取公钥
X509EncodedKeySpecx509EncodedKeySpec=newX509EncodedKeySpec(keyBytes);
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORTHM);
KeypublicKey=keyFactory.generatePublic(x509EncodedKeySpec);
//对数据解密
Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
returncipher.doFinal(data);
}
/**
*用公钥解密
*
*@paramdata
*加密数据
*@paramkey
*密钥
*@return
*@throwsException
*/
publicstaticbyte[]decryptByPublicKey(byte[]data,Stringkey)throwsException{
//对私钥解密
byte[]keyBytes=decryptBASE64(key);
X509EncodedKeySpecx509EncodedKeySpec=newX509EncodedKeySpec(keyBytes);
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORTHM);
KeypublicKey=keyFactory.generatePublic(x509EncodedKeySpec);
//对数据解密
Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE,publicKey);
returncipher.doFinal(data);
}
/**
*用私钥对信息生成数字签名
*
*@paramdata
*//加密数据
*@paramprivateKey
*//私钥
*@return
*@throwsException
*/
publicstaticStringsign(byte[]data,StringprivateKey)throwsException{
//解密私钥
byte[]keyBytes=decryptBASE64(privateKey);
//构造PKCS8EncodedKeySpec对象
PKCS8EncodedKeySpecpkcs8EncodedKeySpec=newPKCS8EncodedKeySpec(keyBytes);
//指定加密算法
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORTHM);
//取私钥匙对象
PrivateKeyprivateKey2=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
//用私钥对信息生成数字签名
Signaturesignature=Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(privateKey2);
signature.update(data);
returnencryptBASE64(signature.sign());
}
/**
*校验数字签名
*
*@paramdata
*加密数据
*@parampublicKey
*公钥
*@paramsign
*数字签名
*@return
*@throwsException
*/
publicstaticbooleanverify(byte[]data,StringpublicKey,Stringsign)throwsException{
//解密公钥
byte[]keyBytes=decryptBASE64(publicKey);
//构造X509EncodedKeySpec对象
X509EncodedKeySpecx509EncodedKeySpec=newX509EncodedKeySpec(keyBytes);
//指定加密算法
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORTHM);
//取公钥匙对象
PublicKeypublicKey2=keyFactory.generatePublic(x509EncodedKeySpec);
Signaturesignature=Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(publicKey2);
signature.update(data);
//验证签名是否正常
returnsignature.verify(decryptBASE64(sign));
}
publicstaticvoidmain(String[]args)throwsException{
MapkeyMap=RsaUtil.initKey();
StringprivateKey=RsaUtil.getPrivateKey(keyMap);
StringpublicKey=RsaUtil.getPublicKey(keyMap);
System.out.println(privateKey);
System.out.println(publicKey);
Stringpassword="123abc";
byte[]encodePassword=RsaUtil.encryptByPublicKey(password.getBytes(),publicKey);
System.out.println(newString(RsaUtil.decryptByPrivateKey(encodePassword,privateKey),"UTF-8"));
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- RSA 加密算法 java 实现