import javax.crypto.*;public class RSATest {
private String Algorithm = "DES";
private String myinfo = "information"; public static void main(String[] args) {
RSATest my = new RSATest();
my.encrypt();
} public void encrypt() {
try {
// key
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
// encrypt
System.out.println("info:" + myinfo);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
byte[] cipherByte = c1.doFinal(myinfo.getBytes());
String str = new String(cipherByte);
System.out.println("encrypted:" + str);
// decrypt
decrypt(str, deskey);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
} public void decrypt(String s, SecretKey deskey) {
try {
Cipher c2 = Cipher.getInstance(Algorithm);
c2.init(Cipher.DECRYPT_MODE, deskey);
System.out.println(s.getBytes());
byte[] clearByte = c2.doFinal(s.getBytes());
System.out.println("decrypted info:" + (new String(clearByte)));
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
}
}在解密的时候捕获异常:javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA12275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at des.RSATest.decrypt(RSATest.java:125)
at des.RSATest.encrypt(RSATest.java:110)
at des.RSATest.main(RSATest.java:92)
请各位大侠帮我看看,请多指教!
密钥不对,加密的密钥应该是8字节的。。如果是3DES算法,密钥应该是24字节。
md5 能解密么? 这有点搞笑了. 它是单向hash的.解密的时候 解密必须是8的倍数. 一般来说des加解密的字符串就是64位的.也就是8个字节.但是有多重des.. 所以给你的提示是 需要是8的倍数.
下面是我以前项目中写过的。只要key一样,加密解密直接调用就行了。
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;/**
* DES暗号化、復号化
* @author
* @version 2008/09/16
*/
public class BBCCDecryption {
//暗号化、復号化用パスワード,key
private static final String PASSWORD = " "; //暗号化アルゴリズム
private static final String ALGORITHM = "DES"; /**
* 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();
}
}
加密时生成的byte数组;
byte[] clearByte = c2.doFinal(s.getBytes());
s.getBytes()是你本来想要解密的字符串字节数组
cipherByte和s.getBytes()已经不是统一个数组了
具体这两个为什么不一样你只有看解密算法了
message digest...是一種hash...
import java.security.SecureRandom;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;public class Crypt { Key key; public Crypt(String str) {
setKey(str);//
} /**
* Set KEY
*/
public void setKey(String strKey) {
try {
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(strKey.getBytes()));
this.key = generator.generateKey();
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
}
} /**
* Encrypt
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
} /**
* Decrypt
*/
public String getDesString(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
} /**
* return Encrypt code
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
} /**
* return Decrypt code
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
} public static void main(String args[]) {
//data
String str = "Hello World!";
Crypt cy = new Crypt("DES");
//Encrypt
String enc = cy.getEncString(str);
System.out.println("encrypted:" + enc);
//Decrypt
String dec = cy.getDesString(enc);
System.out.println("decrypted:" + dec);
}
}