对加密这东西一点也不懂
网上看了些java代码
结果对于两个不同的java的des加密代码
相同的明文和密匙输入
却是不同的密文输出
我很郁闷
不知道怎么写才是规范的正确的输入的明文 hello world!!
自定的密匙 abcdefgh
两个程序输出的密文分别是
xhQJ6TWnnhb8lv3xll9mNg==
059d04d6b2e82f2cb627f0ac0429b7a0 有没有大侠帮我验证一下哪个是对的
或者两个都是错的
那么到底该怎么写呢
很急...麻烦帮帮忙...
网上看了些java代码
结果对于两个不同的java的des加密代码
相同的明文和密匙输入
却是不同的密文输出
我很郁闷
不知道怎么写才是规范的正确的输入的明文 hello world!!
自定的密匙 abcdefgh
两个程序输出的密文分别是
xhQJ6TWnnhb8lv3xll9mNg==
059d04d6b2e82f2cb627f0ac0429b7a0 有没有大侠帮我验证一下哪个是对的
或者两个都是错的
那么到底该怎么写呢
很急...麻烦帮帮忙...
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.commons.codec.binary.Base64;public class Test1 {
private final static char[] HEX = "0123456789abcdef".toCharArray(); public static void main(String[] args) throws Exception {
byte[] bys = "hello world!!".getBytes();
byte[] key = "abcdefgh".getBytes();
byte[] b = encode(key, bys);
System.out.println(bytes2Hex2(b));
System.out.println(new String(Base64.encodeBase64(b)));
b = decode(key, b);
System.out.println(new String(b));
}
public static byte[] encode(byte[] key, byte[] bys) throws Exception {
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey sk = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, sk);
return cipher.doFinal(bys);
}
public static byte[] decode(byte[] key, byte[] bys) throws Exception {
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey sk = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, sk);
return cipher.doFinal(bys);
}
public static String bytes2Hex2(byte[] bys) {
char[] chs = new char[bys.length * 2];
for(int i = 0; i < bys.length; i++) {
chs[2 * i] = HEX[bys[i] >> 4 & 0xf];
chs[2 * i + 1] = HEX[bys[i] & 0xf];
}
return new String(chs);
}
}
后面那个是不是des,我没有具体的加密程序 不能确定
这个包提示有错误 在哪导入的
后面的程序和1楼给的差的不多
是字节数组转16进制的这么说des的加密不唯一?
字节数组转16进制
复杂的过程能用api搞定就最好
这是 Apache Commons 的 Codec 类库:
http://commons.apache.org/codec/
网址好像进不去
这和sun.misc.BASE64Encoder包作用相同吗
import javax.crypto.*;
import javax.crypto.spec.*;/**
* DES暗号化、復号化
* @version 2008/09/16
*/
public class BBCCDecryption {
//暗号化、復号化用パスワード
private static final String PASSWORD = "abcdefgh";
//暗号化アルゴリズム
private static final String ALGORITHM = "DES";
public static void main(String[] args) throws Exception {
String data = "hello world!!";
//暗号化
String str = BBCCDecryption.encrypt(data);
System.out.println("str: " + str);
//復号化
str = BBCCDecryption.decrypt(str);
System.out.println("str: " + str);
}
/**
* DES暗号化
* @param data 暗号化前のデータ
* @return 暗号化したデータ
* @throws Exception
*/
public final static String encrypt(String data) throws Exception {
return byte2hex(encrypt(data.getBytes(), PASSWORD
.getBytes()));
}
/**
* 暗号化したデータを復号化
* @param data 暗号化したデータ
* @return 復号化後のデータ
* @throws Exception
*/
public final static String decrypt(String data) throws Exception {
return new String(decrypt(hex2byte(data.getBytes()),
PASSWORD.getBytes()));
}
/** *//**
* keyにより、暗号化を実行
* @param data 暗号化前のデータ
* @param key 暗号化用key
* @return 暗号化したデータ
* @throws Exception
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception { SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
byte[] cipherData =cipher.doFinal(data);
return cipherData;
}
/** *//**
* keyにより、復号化を実行
* @param data
* @param key
* @return 復号化したデータ
* @throws Exception
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception { SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
byte[] cipherData =cipher.doFinal(data);
return cipherData;
}
/**
*
* @param b
* @return
*/
public static byte[] hex2byte(byte[] b) {
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;
}
/**
* byte[]⇒String
* @param byte[] b
* @return String
*/
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
}