package com.xgw.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.Key;
import javax.crypto.Cipher;
public class EncryptMain
{
private static String defaultKeyStr = "huawei.esb.key";
public static String encryptDES(String info)
{
// 定义加密算法,可用,DES,DESede,Blowfish
String Algorithm = "DES";
// 定义加密key
Key key = getKey();
// 定义要生成的密文
String rs = "";
byte[] cipherByte = null;
try
{
// 得到加密/解密器
Cipher cl = Cipher.getInstance(Algorithm);
// 用指定的密钥和模式初始化Cipher对象
// 参数:(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)
cl.init(Cipher.ENCRYPT_MODE, key);
// 对要加密的内容进行编码处理。
cipherByte = cl.doFinal(info.getBytes());
rs = byte2hex(cipherByte);
}
catch (Exception e)
{
e.printStackTrace();
}
// 返回密文的16进制形式
return rs;
}
private static String byte2hex(byte[] arrB)
{
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串长度是数据长度的2倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++)
{
int intTmp = arrB[i];
// 把负数转换成正数
while (intTmp < 0)
{
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16)
{
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
public static Key getKey()
{
byte[] arrBTmp = defaultKeyStr.getBytes();
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++)
{
arrB[i] = arrBTmp[i];
}
// 生成密
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
public static void main(String[] args)
{
System.out.println("Program running...");
boolean b_mark = true;
while (b_mark)
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String source = br.readLine();
if ("exit".equals(source))
{
System.out.println("Program Exit.");
b_mark = false;
}
else
{
String target = EncryptMain.encryptDES(source);
System.out.println(target);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.Key;
import javax.crypto.Cipher;
public class EncryptMain
{
private static String defaultKeyStr = "huawei.esb.key";
public static String encryptDES(String info)
{
// 定义加密算法,可用,DES,DESede,Blowfish
String Algorithm = "DES";
// 定义加密key
Key key = getKey();
// 定义要生成的密文
String rs = "";
byte[] cipherByte = null;
try
{
// 得到加密/解密器
Cipher cl = Cipher.getInstance(Algorithm);
// 用指定的密钥和模式初始化Cipher对象
// 参数:(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)
cl.init(Cipher.ENCRYPT_MODE, key);
// 对要加密的内容进行编码处理。
cipherByte = cl.doFinal(info.getBytes());
rs = byte2hex(cipherByte);
}
catch (Exception e)
{
e.printStackTrace();
}
// 返回密文的16进制形式
return rs;
}
private static String byte2hex(byte[] arrB)
{
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串长度是数据长度的2倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++)
{
int intTmp = arrB[i];
// 把负数转换成正数
while (intTmp < 0)
{
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16)
{
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
public static Key getKey()
{
byte[] arrBTmp = defaultKeyStr.getBytes();
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++)
{
arrB[i] = arrBTmp[i];
}
// 生成密
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
public static void main(String[] args)
{
System.out.println("Program running...");
boolean b_mark = true;
while (b_mark)
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String source = br.readLine();
if ("exit".equals(source))
{
System.out.println("Program Exit.");
b_mark = false;
}
else
{
String target = EncryptMain.encryptDES(source);
System.out.println(target);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}