由MD5引发的血案

近日,在写有关代码时,需呀一些数据加密的东东。。。
例如:需要读取本地的数据
附上代码若干:
/**
* 读取本地存储的数据到session 并返回
*
* @return
*/
public List buildAccountInfosFromCache() {
try {
String encryptedInfos = PreferenceUtils.instance().getString(
MD5.encode(KEY_ACCOUNTS), “”);
String infoStr = StringUtils.isEmpty(encryptedInfos) ? “” : DES
.decrypt(encryptedInfos, KEY);
if (!StringUtils.isEmpty(infoStr)) {
accountList = new ArrayList(Arrays.asList(infoStr
.split(“&”)));
}
} catch (Exception e) {
e.printStackTrace();
}
return accountList;
}

此处即使用到自己编写的class MD5
MD5:
Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护。该算法的文件号为RFC 1321(R.Rivest,MIT Laboratory for Computer Science and RSA Data Security Inc. April 1992)。

经由MD2,MD3 ,MD4发展而来,MD2,MD3 ,MD4 都有缺陷,详情不叙述了。。。

MD5的因子结构:’0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’,
‘9’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’
或者称为组成元素也可。

核心代码三句话:
byte[] strTemp = string.getBytes();
MessageDigest messageDigest = MessageDigest.getInstance(“MD5”);
messageDigest.update(strTemp);
byte[] md = messageDigest.digest();

api文档这样说的:

java.security.MessageDigest

Uses a one-way hash function to turn an arbitrary number of bytes into a fixed-length byte sequence. The original arbitrary-length sequence is the message, and the fixed-length byte sequence is the digest or message digest.

Sample Code
The basic pattern to digest an java.io.InputStream looks like this:

MessageDigest digester = MessageDigest.getInstance(“MD5”);
byte[] bytes = new byte[8192];
int byteCount;
while ((byteCount = in.read(bytes)) > 0) {
digester.update(bytes, 0, byteCount);
}
byte[] digest = digester.digest();

That is, after creating or resetting a MessageDigest you should call update(byte[], int, int) for each block of input data, and then call digest to get the final digest. Note that calling digest resets the MessageDigest. Advanced users who want partial digests should clone their MessageDigest before calling digest.

This class is not thread-safe.

See Also:
MessageDigestSpi

意思不必解释了吧,实在不会的会Google好了。

算法核心的话:根据自己的需求而写:
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}

在上面的读取本地数据是用到了自己编写的class DES:
DES:数据 加密块算法。此算法是对称加密算法体系中的代表,在计算机网络系统中广泛使用.

自然也就有非对称加密算法咯。。。

非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey)。公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密;如果用私有密钥对数据进行加密,那么只有用对应的公开密钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。 非对称加密算法实现机密信息交换的基本过程是:甲方生成一对密钥并将其中的一把作为公用密钥向其它方公开;得到该公用密钥的乙方使用该密钥对机密信息进行加密后再发送给甲方;甲方再用自己保存的另一把专用密钥对加密后的信息进行解密。

在这里就不用我说了,支付宝使用的便是非对称型的。。。严格来说,涉及交易的大部分支付模块均是采用非对称的。。。

public class DES {

private static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };

public static String encrypt(String encryptString, String encryptKey)
        throws Exception {
    IvParameterSpec zeroIv = new IvParameterSpec(iv);
    SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
    byte[] encryptedData = cipher.doFinal(encryptString.getBytes());
    return Base64.encode(encryptedData);
}

public static String decrypt(String decryptString, String decryptKey)
        throws Exception {
    byte[] byteMi = new Base64().decode(decryptString);
    IvParameterSpec zeroIv = new IvParameterSpec(iv);
    SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
    byte decryptedData[] = cipher.doFinal(byteMi);
    return new String(decryptedData);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值