求一个数据加密的算法!(在线等) ```如题``越简单越好,我很菜的``谢谢大家了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看你是要可逆的还是不可逆的了不可逆的有md5,sha-1可逆的有Base64等等 简单是吧,DES吧。package com.gka.util;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import java.security.Security;public class DES { public static int _DES = 1; public static int _DESede = 2; public static int _Blowfish = 3; private Cipher p_Cipher; private SecretKey p_Key; private String p_Algorithm; private void selectAlgorithm(int al) { switch (al) { default: case 1: this.p_Algorithm = "DES"; break; case 2: this.p_Algorithm = "DESede"; break; case 3: this.p_Algorithm = "Blowfish"; break; } } public DES(int algorithm) throws Exception { this.selectAlgorithm(algorithm); Security.addProvider(new com.sun.crypto.provider.SunJCE()); this.p_Cipher = Cipher.getInstance(this.p_Algorithm); } public byte[] getKey() { return this.checkKey().getEncoded(); } private SecretKey checkKey() { try { if (this.p_Key == null) { KeyGenerator keygen = KeyGenerator.getInstance(this.p_Algorithm); this.p_Key = keygen.generateKey(); } } catch (Exception nsae) { } return this.p_Key; } public void setKey(byte[] enckey) { this.p_Key = new SecretKeySpec(enckey, this.p_Algorithm); } public byte[] encode(byte[] data) throws Exception { this.p_Cipher.init(Cipher.ENCRYPT_MODE, this.checkKey()); return this.p_Cipher.doFinal(data); } public byte[] decode(byte[] encdata, byte[] enckey) throws Exception { this.setKey(enckey); this.p_Cipher.init(Cipher.DECRYPT_MODE, this.p_Key); return this.p_Cipher.doFinal(encdata); } public byte[] decode(byte[] encdata) throws Exception { this.p_Cipher.init(Cipher.DECRYPT_MODE, this.p_Key); return this.p_Cipher.doFinal(encdata); } public String byte2hex(byte[] b) { String hs = ""; String stmp = ""; for (int i = 0; i < b.length; i++) { stmp = Integer.toHexString(b[i] & 0xFF); if (stmp.length() == 1) { hs += "0" + stmp; } else { hs += stmp; } } return hs; } public byte[] hex2byte(String hex) throws IllegalArgumentException { if (hex.length() % 2 != 0) { throw new IllegalArgumentException(); } char[] arr = hex.toCharArray(); byte[] b = new byte[hex.length() / 2]; for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) { String swap = "" + arr[i++] + arr[i]; int byteint = Integer.parseInt(swap, 16) & 0xFF; b[j] = new Integer(byteint).byteValue(); } return b; } public static void main(String[] args) throws Exception { String info = "000000"; DES des = new DES(_Blowfish); byte[] key = "gkean".getBytes(); des.setKey(key); byte[] enc = des.encode(info.getBytes()); String encStr = des.byte2hex(enc); System.out.println(encStr); String dec = new String(des.decode(des.hex2byte(encStr), key)); System.out.println(dec);// String info = "要加密的字串";// System.out.println("region string:" + info);// byte[] key; //密钥文件(byte)// DES des = new DES(DES._DESede); // 声明DES// key = des.getKey(); //获取随机生成的密钥// System.out.println("encrypted key(byte):" + new String(key));// String hexkey = des.byte2hex(key); //生成十六进制密钥// System.out.println("encrypted key(hex):" + hexkey);// byte[] enc = des.encode(info.getBytes()); //生成加密文件(byte)// System.out.println("encrypted string(byte):" + new String(enc));//// String hexenc = des.byte2hex(enc); //生成十六进制加密文件// System.out.println("encrypted string(hex):" + hexenc);//// byte[] dec = des.decode(enc, des.hex2byte(hexkey)); //解密文件,其中转换十六进制密钥为byte// System.out.println("decrypted string:" + new String(dec)); //生成解密文件字符串, 与info相同 }} 关于Iterator的问题,请教高手。。。 程序编译通过,但运行后只要输入一个数就会抛出异常,求解答! 内部类问题 servlet中文编码出现乱码的问题 利用poi操作excel的问题 请教关于数据记录在表格里分页显示问题? super调用的问题。 同一页面的两个APPLET,希望通过一个让另一个自动刷新,都不是图象 急!!请教各位大虾 在java字符串中如何判断字符的类型 大哥们呀帮帮忙呀 一个DataSource 的jndi配置
不可逆的有md5,sha-1
可逆的有Base64等等
package com.gka.util;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;public class DES { public static int _DES = 1;
public static int _DESede = 2;
public static int _Blowfish = 3; private Cipher p_Cipher;
private SecretKey p_Key;
private String p_Algorithm; private void selectAlgorithm(int al) {
switch (al) {
default:
case 1:
this.p_Algorithm = "DES";
break;
case 2:
this.p_Algorithm = "DESede";
break;
case 3:
this.p_Algorithm = "Blowfish";
break;
}
} public DES(int algorithm) throws Exception {
this.selectAlgorithm(algorithm);
Security.addProvider(new com.sun.crypto.provider.SunJCE());
this.p_Cipher = Cipher.getInstance(this.p_Algorithm);
} public byte[] getKey() {
return this.checkKey().getEncoded();
} private SecretKey checkKey() {
try {
if (this.p_Key == null) {
KeyGenerator keygen = KeyGenerator.getInstance(this.p_Algorithm);
this.p_Key = keygen.generateKey();
}
}
catch (Exception nsae) {
}
return this.p_Key;
} public void setKey(byte[] enckey) {
this.p_Key = new SecretKeySpec(enckey, this.p_Algorithm);
} public byte[] encode(byte[] data) throws Exception {
this.p_Cipher.init(Cipher.ENCRYPT_MODE, this.checkKey());
return this.p_Cipher.doFinal(data);
} public byte[] decode(byte[] encdata, byte[] enckey) throws Exception {
this.setKey(enckey);
this.p_Cipher.init(Cipher.DECRYPT_MODE, this.p_Key);
return this.p_Cipher.doFinal(encdata);
} public byte[] decode(byte[] encdata) throws Exception {
this.p_Cipher.init(Cipher.DECRYPT_MODE, this.p_Key);
return this.p_Cipher.doFinal(encdata);
} public String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int i = 0; i < b.length; i++) {
stmp = Integer.toHexString(b[i] & 0xFF);
if (stmp.length() == 1) {
hs += "0" + stmp;
} else {
hs += stmp;
}
}
return hs;
} public byte[] hex2byte(String hex) throws IllegalArgumentException {
if (hex.length() % 2 != 0) {
throw new IllegalArgumentException();
}
char[] arr = hex.toCharArray();
byte[] b = new byte[hex.length() / 2];
for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
String swap = "" + arr[i++] + arr[i];
int byteint = Integer.parseInt(swap, 16) & 0xFF;
b[j] = new Integer(byteint).byteValue();
}
return b;
} public static void main(String[] args) throws Exception { String info = "000000";
DES des = new DES(_Blowfish);
byte[] key = "gkean".getBytes();
des.setKey(key);
byte[] enc = des.encode(info.getBytes());
String encStr = des.byte2hex(enc);
System.out.println(encStr);
String dec = new String(des.decode(des.hex2byte(encStr), key));
System.out.println(dec);
// String info = "要加密的字串";
// System.out.println("region string:" + info);
// byte[] key; //密钥文件(byte)
// DES des = new DES(DES._DESede); // 声明DES
// key = des.getKey(); //获取随机生成的密钥
// System.out.println("encrypted key(byte):" + new String(key));
// String hexkey = des.byte2hex(key); //生成十六进制密钥
// System.out.println("encrypted key(hex):" + hexkey);
// byte[] enc = des.encode(info.getBytes()); //生成加密文件(byte)
// System.out.println("encrypted string(byte):" + new String(enc));
//
// String hexenc = des.byte2hex(enc); //生成十六进制加密文件
// System.out.println("encrypted string(hex):" + hexenc);
//
// byte[] dec = des.decode(enc, des.hex2byte(hexkey)); //解密文件,其中转换十六进制密钥为byte
// System.out.println("decrypted string:" + new String(dec)); //生成解密文件字符串, 与info相同
}
}