在以后系统设计中,可能对哪些敏感的字符要做加密的算法:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.log4j.Logger;
/*
* Class: EncryptUtil
* Description: Encrypt and Decrypt Tool Class
* Version: 1.0
* Author: Carson.Huang
* Created on March 1, 2011
*/
public class EncryptUtil
{
private static Logger logger = Logger.getLogger(EncryptUtil.class);
private static String defaultKeyStr = "huawei.esb.key";
public static void main(String[] args)
{
boolean b_mark = true;
while (b_mark)
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String source = br.readLine();
if ("exit".equals(source))
{
logger.info("Program Exit.");
b_mark = false;
}
else
{
String target = EncryptUtil.encryptMD5(source);
logger.info("[MD5 Encrypt] source: " + source
+ ", encrypt: " + target);
target = EncryptUtil.encryptSHA(source);
logger.info("[SHA256 Encrypt] source: " + source
+ ", encrypt: " + target);
target = EncryptUtil.encryptDES(source);
System.out.println(target);
String restore = EncryptUtil.decryptDES(target);
System.out.println(restore);
logger.info("[DES56 Encrypt] source: " + source
+ ", encrypt: " + target + ", decrypt: " + restore);
target = EncryptUtil.encryptAES(source);
// System.out.println(target);
restore = EncryptUtil.decryptAES(target);
// System.out.println(restore);
logger.info("[AES128 Encrypt] source: " + source
+ ", encrypt: " + target + ", decrypt: " + restore);
}
}
catch (IOException e)
{
logger.error(e.getMessage(), e);
}
}
}
// md5
public static String encryptMD5(String inputText)
{
return encrypt(inputText, "md5");
}
// sha256, can modify SHA384、SHA512
public static String encryptSHA(String inputText)
{
return encrypt(inputText, "SHA-256");
}
/**
* md5或者sha-1加密
*
* @param inputText 要加密的内容
* @param algorithmName 加密算法名称:md5或者sha-1,不区分大小写
* @return
*/
public static String encrypt(String inputText, String algorithmName)
{
if (inputText == null)
{
throw new IllegalArgumentException(
"The input parameter cannot be null.");
}
if (algorithmName == null || "".equals(algorithmName.trim()))
{
algorithmName = "md5";
}
String encryptText = null;
try
{
MessageDigest m = MessageDigest.getInstance(algorithmName);
m.update(inputText.getBytes("UTF8"));
byte s[] = m.digest();
// m.digest(inputText.getBytes("UTF8"));
return byte2hex(s);
}
catch (NoSuchAlgorithmException e)
{
logger.error(e.getMessage(), e);
}
catch (UnsupportedEncodingException e)
{
logger.error(e.getMessage(), e);
}
return encryptText;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.log4j.Logger;
/*
* Class: EncryptUtil
* Description: Encrypt and Decrypt Tool Class
* Version: 1.0
* Author: Carson.Huang
* Created on March 1, 2011
*/
public class EncryptUtil
{
private static Logger logger = Logger.getLogger(EncryptUtil.class);
private static String defaultKeyStr = "huawei.esb.key";
public static void main(String[] args)
{
boolean b_mark = true;
while (b_mark)
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String source = br.readLine();
if ("exit".equals(source))
{
logger.info("Program Exit.");
b_mark = false;
}
else
{
String target = EncryptUtil.encryptMD5(source);
logger.info("[MD5 Encrypt] source: " + source
+ ", encrypt: " + target);
target = EncryptUtil.encryptSHA(source);
logger.info("[SHA256 Encrypt] source: " + source
+ ", encrypt: " + target);
target = EncryptUtil.encryptDES(source);
System.out.println(target);
String restore = EncryptUtil.decryptDES(target);
System.out.println(restore);
logger.info("[DES56 Encrypt] source: " + source
+ ", encrypt: " + target + ", decrypt: " + restore);
target = EncryptUtil.encryptAES(source);
// System.out.println(target);
restore = EncryptUtil.decryptAES(target);
// System.out.println(restore);
logger.info("[AES128 Encrypt] source: " + source
+ ", encrypt: " + target + ", decrypt: " + restore);
}
}
catch (IOException e)
{
logger.error(e.getMessage(), e);
}
}
}
// md5
public static String encryptMD5(String inputText)
{
return encrypt(inputText, "md5");
}
// sha256, can modify SHA384、SHA512
public static String encryptSHA(String inputText)
{
return encrypt(inputText, "SHA-256");
}
/**
* md5或者sha-1加密
*
* @param inputText 要加密的内容
* @param algorithmName 加密算法名称:md5或者sha-1,不区分大小写
* @return
*/
public static String encrypt(String inputText, String algorithmName)
{
if (inputText == null)
{
throw new IllegalArgumentException(
"The input parameter cannot be null.");
}
if (algorithmName == null || "".equals(algorithmName.trim()))
{
algorithmName = "md5";
}
String encryptText = null;
try
{
MessageDigest m = MessageDigest.getInstance(algorithmName);
m.update(inputText.getBytes("UTF8"));
byte s[] = m.digest();
// m.digest(inputText.getBytes("UTF8"));
return byte2hex(s);
}
catch (NoSuchAlgorithmException e)
{
logger.error(e.getMessage(), e);
}
catch (UnsupportedEncodingException e)
{
logger.error(e.getMessage(), e);
}
return encryptText;
}