系统中加密算法EncryptUtil01

在以后系统设计中,可能对哪些敏感的字符要做加密的算法:
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值