admin 管理员组文章数量: 887021
2023年12月23日发(作者:config配置文件)
package ty;
/**
* MD5工具
*
* @author leizhimin 2010-6-3 11:10:29
*/
import eDigest;
public class MD5 {
/**
* MD5值计算
* MD5的算法在RFC1321 中定义:
* 在RFC 1321中,给出了Test suite用来检验你的实现是否正确:
* MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
* MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
* MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
* MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
* MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
*
* @param res 源字符串
* @return md5值
*/
public final static String MD5(String res) {
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'};
try {
byte[] strTemp = es();
MessageDigest mdTemp = tance("MD5");
(strTemp);
byte[] md = ();
int j = ;
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];
}
String dd = new String(str);
return dd;
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
n(5(""));
n(5("a"));
n(5("abc"));
}
}
package ty;
import ortedEncodingException;
/**
* Base64双向加密工具
*
* @author leizhimin 2010-6-3 14:10:11
*/
public class Base64 {
private static char[] base64EncodeChars = new char[]{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};
private static byte[] base64DecodeChars = new byte[]{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1};
/**
* 加密
*
* @param data 明文的字节数组
* @return 密文字符串
*/
public static String encode(byte[] data) {
StringBuffer sb = new StringBuffer();
int len = ;
int i = 0;
int b1, b2, b3;
while (i < len) {
b1 = data[i++] & 0xff;
if (i == len) {
(base64EncodeChars[b1 >>> 2]);
(base64EncodeChars[(b1 & 0x3) << 4]);
("==");
break;
}
b2 = data[i++] & 0xff;
if (i == len) {
(base64EncodeChars[b1 >>> 2]);
(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
(base64EncodeChars[(b2 & 0x0f) << 2]);
("=");
break;
}
b3 = data[i++] & 0xff;
(base64EncodeChars[b1 >>> 2]);
(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]);
(base64EncodeChars[b3 & 0x3f]);
}
return ng();
}
/**
* 解密
*
* @param str 密文
* @return 明文的字节数组
* @throws UnsupportedEncodingException
*/
public static byte[] decode(String str) throws UnsupportedEncodingException {
StringBuffer sb = new StringBuffer();
byte[] data = es("US-ASCII");
int len = ;
int i = 0;
int b1, b2, b3, b4;
while (i < len) {
/* b1 */
do {
b1 = base64DecodeChars[data[i++]];
} while (i < len && b1 == -1);
if (b1 == -1) break;
/* b2 */
do {
b2 = base64DecodeChars
[data[i++]];
} while (i < len && b2 == -1);
if (b2 == -1) break;
((char) ((b1 << 2) | ((b2 & 0x30) >>> 4)));
/* b3 */
do {
b3 = data[i++];
if (b3 == 61) return ng().getBytes("iso8859-1");
b3 = base64DecodeChars[b3];
} while (i < len && b3 == -1);
if (b3 == -1) break;
((char) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));
/* b4 */
do {
b4 = data[i++];
if (b4 == 61) return ng().getBytes("iso8859-1");
b4 = base64DecodeChars[b4];
} while (i < len && b4 == -1);
if (b4 == -1) break;
((char) (((b3 & 0x03) << 6) | b4));
}
return ng().getBytes("iso8859-1");
}
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "abcd";
n("加密前:" + s);
String x = encode(es());
n("加密后:" + x);
String x1 = new String(decode(x));
n("解密后:" + x1);
}
}
package ty;
import 64Decoder;
import 64Encoder;
import ;
import erator;
import ;
import Random;
/**
* 使用3DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.
*
* @author leizhimin 2010-6-3 11:10:54
*/
public class ThreeDES {
private Key key; //密钥
/**
* 根据参数生成KEY
*
* @param strKey 密钥字符串
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = tance("DES");
_(new SecureRandom(es()));
= _teKey();
_generator = null;
} catch (Exception e) {
tackTrace();
}
}
/**
* 加密String明文输入,String密文输出
*
* @param strMing String明文
* @return String密文
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = es("UTF8");
byteMi = Code(byteMing);
strMi = (byteMi);
} catch (Exception e) {
tackTrace();
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文输入,String明文输出
*
* @param strMi String密文
* @return String明文
*/
public String getDesString(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = Buffer(strMi);
byteMing = Code(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
tackTrace();
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
*
* @param byteS byte[]明文
* @return byte[]密文
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = tance("DES");
(T_MODE, key);
byteFina = l(byteS);
} catch (Exception e) {
tackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文输入,以byte[]明文输出
*
* @param byteD byte[]密文
* @return byte[]明文
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = tance("DES");
(T_MODE, key);
byteFina = l(byteD);
} catch (Exception e) {
tackTrace();
} finally {
cipher = null;
}
return byteFina;
}
public static void main(String[] args) {
ThreeDES des = new ThreeDES(); // 实例化一个对像
("abcdefgasiainfo"); // 生成密匙
String strEnc = String("abc");// 加密字符串,返回String的密文
n(strEnc);
String strDes = String(strEnc);// 把String 类型的密文解密
n(strDes);
}
}
版权声明:本文标题:Java实现的Base64、MD5、3DES三种工具 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1703302277h446095.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论