我的3des加密出怪事了。。。。

linyfei 2011-02-28 04:43:39
先贴上代码再说:

单元测试类: junit4

@Test
public void getDesStrValue(){
String tmp = "1000000000000011222222222011-2-22 17:50:42";
ParameterCheck.checkAuthenticator("XgfIupcZNd9WZQ9JUpmRAw==", tmp);
//加密
String strToBase64 = Base64Coding.encode(
DES3Encrypt.getInstance().encrypt(
SHA1Encrypt.encrypt("1000000000000011222222222011-2-22 17:50:42").toString()));
System.out.println(strToBase64);
}

ParameterCheck类:
/**
* 验证:认证码,用于鉴别发送者。
* @param authenticator = base64(3DES(SHA1(SrcSsDeviceNo+AuthSsDeviceNo + LoginAccount +TimeStamp))),
* @param str = SrcSsDeviceNo+AuthSsDeviceNo + LoginAccount +TimeStamp
* @return true:合法 false:非法
*/
public static boolean checkAuthenticator (String authenticator,String str) {
boolean result = false;
if (authenticator != null && authenticator.length() <= UDBConstant.AUTHENTICATOR_LENGTH && str != null) {
/* String strToBase64 = Base64Coding.encode(
DES3Encrypt.getInstance().encrypt(
SHA1Encrypt.encrypt(str).toString()));*/

//加密
String strToBase64 = Base64Coding.encode(
DES3Encrypt.getInstance().encrypt(
SHA1Encrypt.encrypt(str).toString()));
System.out.println("str "+str);
System.out.println("strToBase64 "+strToBase64);
System.out.println("authenticator "+authenticator);

if (authenticator.equals(strToBase64)) {
result = true;
}
}
return result;
}

DES3Encrypt类:
private String Algorithm = BusinessConstants.ENCRYPT_TYPE;// 定义 加密算法,
//可用 DES,DESede,Blowfish, CBC是运算模式, PKCS7Padding是填充模式

private SecretKey secretkey; //秘密密钥

private Cipher cipher; //此类提供了针对加密和解密的密码 cipher 功能

private byte[] cipherByte; //加解密后的字节数组

private IvParameterSpec zeroIv; //向量

private static DES3Encrypt instance;

private static final String fixKey = BusinessConstants.FIX_KEY;

private static final byte[] iv = BusinessConstants.IV;
/**
* Initial key
*/
private DES3Encrypt() {
init(fixKey, iv);
}

/**
* 初始化该类
* @param fixKey
* @param iv
*/
private void init(String fixKey, byte[] iv) {
try {
zeroIv = new IvParameterSpec(iv);
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

secretkey = new SecretKeySpec(hex2byte(fixKey), "DESede");
System.out.println();
cipher = Cipher.getInstance(Algorithm,"BC");
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
} catch (NoSuchPaddingException ex) {
ex.printStackTrace();
}catch (NoSuchProviderException e) {
e.printStackTrace();
}
}

public static DES3Encrypt getInstance(){
return instance = new DES3Encrypt();

}

/**
* Enrypt 3DES加密
* @param str
* @return
*/
public byte[] encrypt(String str) {
try {
cipher.init(Cipher.ENCRYPT_MODE,secretkey, zeroIv);
cipherByte = cipher.doFinal(str.getBytes("UTF-8"));
} catch (java.security.InvalidKeyException ex) {
ex.printStackTrace();
} catch (javax.crypto.BadPaddingException ex) {
ex.printStackTrace();
} catch (javax.crypto.IllegalBlockSizeException ex) {
ex.printStackTrace();
}catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return cipherByte;
}


/**
* 把十六进制字符串转换成字节数组
*
* @param b
* 待转换的十六进制字符串
* @return 字节数组
*/
public byte[] hex2byte(String s)
{
byte[] b = s.getBytes();
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2)
{
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
/**
* decrypt 3DES解密
* @param buff
* @return
*/
public String decrypt(byte[] buff) {
String result = "";
try {
cipher.init(Cipher.DECRYPT_MODE, secretkey, zeroIv);
cipherByte = cipher.doFinal(buff);
result = new String(cipherByte, "UTF-8");
} catch (java.security.InvalidKeyException ex) {
ex.printStackTrace();
} catch (javax.crypto.BadPaddingException ex) {
ex.printStackTrace();
} catch (javax.crypto.IllegalBlockSizeException ex) {
ex.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}


问题:为何会出现图中的问题?

参数图:




...全文
162 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧