发表于: 2017-06-06 21:01:00
2 1003
今天完成的事:
今天完成了登录和注册的页面展示,学习了DES、MD5和Cookie,这三个在Java中都有对应的类。
DES:
public class DESUtil {
private final static String DES = "DES"
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
// 正式执行加密操作
return cipher.doFinal(src);
}
}
MD5:
public class MD5Util {
public static String getMD5(String string) {
MessageDigest messageDigest = null;
try {
//生成一个MD5加密计算摘要
messageDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
//计算MD5函数
messageDigest.update(string.getBytes());
//digest()最后确定返回md5 hash值,返回值为8位字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
//BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
return new BigInteger(1, messageDigest.digest()).toString(16);
}
}
明日计划:
把登录的逻辑判断给写出来。
遇到的问题:DES、MD5和Cookie,虽然都在网上看了是怎么一回事,但是怎么把这些和注册登录结合起来木钱还不明白。
收获:
评论