在java中对数据进行加密的方法使用MD5编码加密public static String md5Encode(String text) {try {MessageDigest md = MessageDigest.getInstance(
在java中对数据进行加密的方法
使用MD5编码加密
public static String md5Encode(String text) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(text.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
String hexString = Integer.toHexString(b & 0xFF);
if (hexString.length() == 1) {
hexString = "0" + hexString;
}
sb.append(hexString);
}
return sb.toString();
} catch (NoSuchAlGorithmException e) {
logger.error(e);
}
return null;
}
输入:123456abcdef,输出:6f3b8ded65bd7a4db11625ac84e579bb
使用DES加密
private final static byte[] KEY_BYTES = "Vp6fhlFXKpGW8k6QPRg7Q6Jb7HyAhRi6MIhJ2YtGD3Zl26eTthJTj5PnIjXH5EI4".getBytes();
public static byte[] encryptDES(byte[] content, byte[] key) {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(key);
SecreTKEyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKey);
// DES 是加密方式, EBC 是工作模式, PKCS5Padding 是填充模式
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
return cipher.doFinal(content);
} catch (Exception e) {
logger.error(e);
}
return null;
}
输入:123456abcdef,输出:j1kR1+ZraO2Tg78dHueoTg==
--结束END--
本文标题: java数据加密怎么做
本文链接: https://lsjlt.com/news/115392.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0