目录
1.MD5
a.MD5简介:
MD5一种不可逆的加密算法,什么意思呢?
网站一般会保存用户密码,为了不让数据库管理员看到用户的密码。
你输入的密码是这样的:12345
网站加密后的密码可能是这样的:E10ADC3949BA59ABBE56E057F20F883E
b.应用场景:
密码加密
软件校对是否已修改过
获得指定长度的随机数
c.代码演示
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你要加密的数z:");
String score=scanner.next();
System.out.println(MD5(score));
}
public static String MD5(String s){
char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
try {
byte[] btInput = s.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("Md5");
mdInst.update(btInput);
byte[] md=mdInst.digest();
int j =md.length;
char str[] =new char[j*2];
int k = 0;
for (int i=0;i<j;i++){
byte byte0 = md[i];
str[k++] = hexDigits[byte0>>>4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}return new String(str);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
d.测试结果:
2.RSA加密:
1.RSA加密简介
1978年就出现了这种算法,它是第一个既能用于数据加密 也能用于数字签名的算法。它易于理解和操作,也很流行。算
法的名字以发明者的名字命名:Ron Rivest, AdiShamir 和 Leonard Adleman。但RSA的安全性一直未能得到理论上的证明。
2.应用场景
3.公钥和私钥
.RSA加密解密:
(1)获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥 (2)加密 (3)解密
2.RSA签名和验证
(1)获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥 (2)获取待签名的Hash码 (3)获取签名的字符串 (4)验证
3.公钥与私钥的理解:
(1)私钥用来进行解密和签名,是给自己用的。
(2)公钥由本人公开,用于加密和验证签名,是给别人用的。
(3)当该用户发送文件时,用私钥签名,别人用他给的公钥验证签名,可以保证该信息是由他发送的。当该用户接受文件时,别人用他的公钥加密,他用私钥解密,可以保证该信息只能由他接收到。
4.代码演示
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
//获取公钥(Base64编码)
public static String getPublicKey(KeyPair keyPair){
PublicKey publicKey = keyPair.getPublic();
byte[] bytes = publicKey.getEncoded();
return byte2Base64(bytes);
}
//获取私钥(Base64编码)
public static String getPrivateKey(KeyPair keyPair){
PrivateKey privateKey = keyPair.getPrivate();
byte[] bytes = privateKey.getEncoded();
return byte2Base64(bytes);
}
//将Base64编码后的公钥转换成PublicKey对象
public static PublicKey string2PublicKey(String pubStr) throws Exception{
byte[] keyBytes = base642Byte(pubStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
//将Base64编码后的私钥转换成PrivateKey对象
public static PrivateKey string2PrivateKey(String priStr) throws Exception{
byte[] keyBytes = base642Byte(priStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
//公钥加密
public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
//私钥解密
public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
//字节数组转Base64编码
public static String byte2Base64(byte[] bytes){
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(bytes);
}
//Base64编码转字节数组
public static byte[] base642Byte(String base64Key) throws IOException{
BASE64Decoder decoder = new BASE64Decoder();
return decoder.decodeBuffer(base64Key);
}
}
public class Demo {
public static void main(String[] args) throws IOException, InvalidKeySpecException {
try {
//生成RSA公钥和私钥,并Base64编码
KeyPair keyPair = RSA.getKeyPair();
String publicKeyStr = RSA.getPublicKey(keyPair);
String privateKeyStr = RSA.getPrivateKey(keyPair);/*
System.out.println("RSA公钥Base64编码:" + publicKeyStr);
System.out.println("RSA私钥Base64编码:" + privateKeyStr);*/
//hello, i am infi, good night!加密
/*String message = "hello, i am infi, good night!";*/
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你要加密的数z:");
String score=scanner.next();
//将Base64编码后的公钥转换成PublicKey对象
PublicKey publicKey = RSA.string2PublicKey(publicKeyStr);
//用公钥加密
byte[] publicEncrypt = RSA.publicEncrypt(score.getBytes(), publicKey);
//加密后的内容Base64编码
String byte2Base64 = RSA.byte2Base64(publicEncrypt);
System.out.println( byte2Base64);
//############## 网络上传输的内容有Base64编码后的公钥 和 Base64编码后的公钥加密的内容 #################
//将Base64编码后的私钥转换成PrivateKey对象
PrivateKey privateKey = RSA.string2PrivateKey(privateKeyStr);
//加密后的内容Base64解码
byte[] base642Byte = RSA.base642Byte(byte2Base64);
//用私钥解密
byte[] privateDecrypt = RSA.privateDecrypt(base642Byte, privateKey);
//解密后的明文
System.out.println("解密后的明文: " + new String(privateDecrypt));
} catch (Exception e) {
e.printStackTrace();
}
}
}