发表于: 2019-11-11 23:24:08
1 973
今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)
1.实现DES加密
实现过程:
1.1生成秘钥
现将秘钥字符串转成byte[] 再通过 SecretKeySpec生成秘钥
public Key getKey(byte[] arrBTemp) throws Exception{
byte[] arrB = new byte[8];
for (int i=0;i<arrB.length;i++){
if(i<arrBTemp.length){
arrB[i] = arrBTemp[i];
}else{
arrB[i] = (byte) 0;
}
}
Key key = new javax.crypto.spec.SecretKeySpec(arrB,"DES");
return key;
}
1.2Cipher对象实际完成加密操作
public Des(String strKey) throws Exception{
Security.addProvider( new com.sun.crypto.provider.SunJCE());//提供者是特定加密算法的实现者,有的提供者(提供的加密技术)是免费的,有的不免费
// 这里可以更改加密算法实现通过 addProvider
// SecureRandom random = new SecureRandom();
Key key = getKey(strKey.getBytes());//这里的key就是秘钥
encryptCiper = Cipher.getInstance("DES");
encryptCiper.init(Cipher.ENCRYPT_MODE,key);
decryptCiper = Cipher.getInstance("DES");
decryptCiper.init(Cipher.DECRYPT_MODE,key);
System.out.println("Cipher provider: " + encryptCiper.getProvider());
System.out.println("Cipher algorithm: " + encryptCiper.getAlgorithm());
}
Cliper.init()方法进行加密的初始化操作
1.3调研Cliper.doFinal()方法进行加密解密操作
/**
* 加密字节数组
* @param arrB 需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception{
return encryptCiper.doFinal(arrB);
}
根据DES算法原理,返回的的是一个字节类型的数组
1.4 将字节类型数组转换成16进制字符串
/**
* 将byte数组转换为表示16进制值的字符串
* 如byte[]{8,18}转换为:0813
* @param arrB 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception
* @Description
* byte 是8由八个二进制表示的 0000 0000 最大数是 0111 1111 127 ~ -128
*
* */
public static String byteArr2HexStr(byte[] arrB) throws Exception{
int iLen = arrB.length;
StringBuffer sb = new StringBuffer(iLen*2);
for (int i=0;i<iLen;i++){
int intTemp = arrB[i];
// while (intTemp<0){-128
// intTemp += 256;//2^8
// }
// if(intTemp<16){
// sb.append(0);
// }
// sb.append(Integer.toString(intTemp,16));
// ff
int h = intTemp >> 4 & 0xf;
int l = intTemp & 0xf;
sb.append(Integer.toString(h,16));
sb.append(Integer.toString(l,16));
}
return sb.toString();
}
最终返回16进制字符串
1.5测试
public static void main(String[] args) throws Exception {
String str = "aaaabbbb";
String password = "ccccddddeeeegggg";
byte[] result = encrypt2(str.getBytes(),password);
System.out.println("加密后"+new String(result));
System.out.println("解密后"+new String(decrypt2(result,password)));
}
能够正常加密和解密
2.通过SecretKeyFactory创建秘钥来进行加密
public static byte[] encrypt2(byte[] dataSource,String password) {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = null;
desKey = new DESKeySpec(password.getBytes());
//创建一个密匙工厂,然后用它把DESKeySpec转换成
System.out.println(desKey);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = secretKeyFactory.generateSecret(desKey);
//Cipher对象实际完成加密操作
System.out.println();
Cipher cipher = Cipher.getInstance("DES");
//用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE,secretKey,random);
//现在,获取数据并加密
//正式执行加密操作
return cipher.doFinal(dataSource);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
2.1 解密过程
public static byte[] decrypt2(byte[] src,String password) throws Exception {
System.out.println(src.length);
// DES算法要求有一个可信任的随机数源
SecureRandom secureRandom = new SecureRandom();
// 创建一个DESKeySpec对象
DESKeySpec desKeySpec = new DESKeySpec(password.getBytes());
// 创建一个密匙工厂
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
// 将DESKeySpec对象转换成SecretKey对象
SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,secretKey,secureRandom);
return cipher.doFinal(src);
}
2.3测试
public static void main(String[] args) throws Exception {
String str = "aaaabbbb";
String password = "ccccddddeeeegggg";
byte[] result = encrypt2(str.getBytes(),password);
System.out.println("加密后"+new String(result));
System.out.println("解密后"+new String(decrypt2(result,password)));
}
能够正常加密和解密
明天计划的事情:
看看JWT如何实现
遇到的问题:
收获:
评论