关于java的3DES算法的例子或者资料,Up 都会有分 如果google都搜索不到,那也就沒有甚麼辦法了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JDK里面有个JCE,里面就支持3DES,下面是我头两天写的测试JCE的一个类,你可以参考一下,最好不要直接使用,因为这个类是急急忙忙写出来的,结构没有经过调整。后面的是个测试类,展示了如何使用这个类。import javax.crypto.*;import javax.crypto.spec.*;import java.security.*;import java.security.spec.*;public class DESede { public static final int KEY_LEN = 24; private Cipher cipher = null; public DESede(String rawKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException { StringBuffer sb = new StringBuffer(); sb.append(rawKey); int rawLen = rawKey.length(); for (int i = 0; i < KEY_LEN - rawLen; i++) { sb.append("\0"); } byte[] rawkey = sb.toString().getBytes(); DESedeKeySpec keyspec = new DESedeKeySpec(rawkey); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede"); Key key = keyfactory.generateSecret(keyspec); cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key); } public String encrypt(String clearText) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException { return byteArray2HexStr(encrypt(clearText.getBytes())); } public byte[] encrypt(byte[] clearText) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException { return cipher.doFinal(clearText); } private static final char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; static String byteArray2HexStr(byte[] ba) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < ba.length; i++) { int high = ( (ba[i] & 0xf0) >> 4); int low = (ba[i] & 0x0f); sb.append(hexChars[high]); sb.append(hexChars[low]); } return sb.toString(); }}public class TestDESede { public static void main(String[] args) throws Exception { try { DESede desede = new DESede("1111"); System.out.println(desede.encrypt("1111")); } catch (Exception e) { e.printStackTrace(); }』 //--------------------得到DES加密密钥-------------------------------------------------- public Key GetSecretKey() throws Exception { Key mykey = null; try { FileInputStream inFile = new FileInputStream(FileName); ObjectInputStream inObject = new ObjectInputStream(inFile); Object object = inObject.readObject(); mykey = (SecretKey) object; } catch (Exception e) { } return mykey; }//--------------------DES加密-------------------------------------------------- /** *<b>Function:</b><br> * -DES加密-.<br> *@param StrTxt 参数说明<br> * StrTxt<br> * Type-- String<br> * Comment:明文<br> *@return * Type-- String<br> * 返回加密后的密文. */ public String encode(String StrTxt) throws Exception { cipher.init(Cipher.ENCRYPT_MODE, GetSecretKey()); System.out.println("encode(String StrTxt)输入的明文是:" + StrTxt); byte[] plaintext = StrTxt.getBytes("UTF8");// System.out.println("StrTxt.getBytes(UTF8)=" + byte2hex(plaintext) + "\n"); String output1 = new String(plaintext); System.out.println("\n\noutput1 text:" + output1); //注意把字符串转换成字节需要指定编码方式,一般用此“UTF8”。 byte[] ciphertext = cipher.doFinal(plaintext); //密文。//解密过程。很简单阿。// cipher.init(Cipher.DECRYPT_MODE, GetSecretKey());// byte[] decryptedText = cipher.doFinal(ciphertext);// System.out.println("ciphertext=" + byte2hex(ciphertext) + "\n"); System.out.println("ciphertext.length=" + ciphertext.length); String output = ""; //new String(ciphertext); char[] cTmp; cTmp = new char[ciphertext.length]; for (int i = 0; i < ciphertext.length; i++) { cTmp[i] = (char) (ciphertext[i]); output = output + cTmp[i]; } return output; }//--------------------DES解密-------------------------------------------------- /** *<b>Function:</b><br> * -DES解密-.<br> *@param StrTxt 参数说明<br> * StrTxt<br> * Type-- String<br> * Comment:密文<br> *@return * Type-- String<br> * 返回解密后的明文. * @exception AllException */ public String decode(String StrTxt) throws Exception { cipher.init(Cipher.DECRYPT_MODE, mykey); String output = "";// System.out.println("decode(String StrTxt)输入的密文为=" + StrTxt); char[] cTmp = StrTxt.toCharArray(); byte[] plaintext1; plaintext1 = new byte[cTmp.length]; for (int i = 0; i < cTmp.length; i++) { plaintext1[i] = (byte) (cTmp[i]); } //此处指定编码方式了哈哈。肯定行的。 byte[] decryptedText = cipher.doFinal(plaintext1);// System.out.println("decode(String StrTxt)1=" + byte2hex(decryptedText)); output = new String(decryptedText, "UTF8");// System.out.println("decode(String StrTxt)输出的明文为=" + output); return output; }给你我写的方法,密钥生产的方法不能给你,呵呵,不好意思。记得给分 对于我的程序补充两句,我的JDK版本是1.4.1,我的程序的密钥和明文是相同的,所以只要参考用法就可以了,更具体的用法可以参考Java的文档。 Up多一次 谢谢帮忙再放一两天吧,或者会有更多的收获的因为昨天考虑了一下RSA加密和DSA加密虽然安全性和3DES差不多,但是不适合在网络上用,因为耗费cpu资源太多,加密解密速度慢所以还是决定用3DES算法 看看这个地方 有很多加密算法的资料http://www.comms.engg.susx.ac.uk/fft/ java的问题 struts1使用定时器时后台如何调用注入类 动态画<table> 出现多条线,新手问,在线等!! 微博的圈人功能实现思路 SSH中怎样实现文件下载 很简单的一个问题,请知道的帮忙看看,表单中的文件域file中如何选择文件类型? 关于post方法和get方法?请大家帮忙? 怎么把a.jsp的值传到b.jsp中的一个iframe里 关于jdom的一个问题? 小问题:特急 没钱提问了,大哥们将就一下,来看看我的JAVA出错。:) 弄了个论坛,欢迎大家光临!:)up有分!
import javax.crypto.spec.*;
import java.security.*;
import java.security.spec.*;public class DESede {
public static final int KEY_LEN = 24; private Cipher cipher = null; public DESede(String rawKey) throws InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
StringBuffer sb = new StringBuffer();
sb.append(rawKey);
int rawLen = rawKey.length();
for (int i = 0; i < KEY_LEN - rawLen; i++) {
sb.append("\0");
} byte[] rawkey = sb.toString().getBytes(); DESedeKeySpec keyspec = new DESedeKeySpec(rawkey); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
Key key = keyfactory.generateSecret(keyspec); cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
} public String encrypt(String clearText) throws IllegalStateException,
IllegalBlockSizeException,
BadPaddingException {
return byteArray2HexStr(encrypt(clearText.getBytes()));
} public byte[] encrypt(byte[] clearText) throws IllegalStateException,
IllegalBlockSizeException,
BadPaddingException {
return cipher.doFinal(clearText);
} private static final char[] hexChars = {
'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f'}; static String byteArray2HexStr(byte[] ba) {
StringBuffer sb = new StringBuffer(); for (int i = 0; i < ba.length; i++) {
int high = ( (ba[i] & 0xf0) >> 4);
int low = (ba[i] & 0x0f); sb.append(hexChars[high]);
sb.append(hexChars[low]);
} return sb.toString();
}
}public class TestDESede {
public static void main(String[] args) throws Exception {
try {
DESede desede = new DESede("1111");
System.out.println(desede.encrypt("1111"));
} catch (Exception e) {
e.printStackTrace();
}
』
public Key GetSecretKey() throws Exception {
Key mykey = null;
try {
FileInputStream inFile =
new FileInputStream(FileName);
ObjectInputStream inObject =
new ObjectInputStream(inFile);
Object object = inObject.readObject();
mykey = (SecretKey) object;
}
catch (Exception e) {
}
return mykey;
}//--------------------DES加密--------------------------------------------------
/**
*<b>Function:</b><br>
* -DES加密-.<br>
*@param StrTxt 参数说明<br>
* StrTxt<br>
* Type-- String<br>
* Comment:明文<br>
*@return
* Type-- String<br>
* 返回加密后的密文.
*/ public String encode(String StrTxt) throws Exception {
cipher.init(Cipher.ENCRYPT_MODE, GetSecretKey());
System.out.println("encode(String StrTxt)输入的明文是:" + StrTxt);
byte[] plaintext = StrTxt.getBytes("UTF8");
// System.out.println("StrTxt.getBytes(UTF8)=" + byte2hex(plaintext) + "\n");
String output1 = new String(plaintext);
System.out.println("\n\noutput1 text:" + output1);
//注意把字符串转换成字节需要指定编码方式,一般用此“UTF8”。
byte[] ciphertext = cipher.doFinal(plaintext); //密文。//解密过程。很简单阿。// cipher.init(Cipher.DECRYPT_MODE, GetSecretKey());
// byte[] decryptedText = cipher.doFinal(ciphertext);
// System.out.println("ciphertext=" + byte2hex(ciphertext) + "\n");
System.out.println("ciphertext.length=" + ciphertext.length);
String output = ""; //new String(ciphertext);
char[] cTmp;
cTmp = new char[ciphertext.length];
for (int i = 0; i < ciphertext.length; i++) {
cTmp[i] = (char) (ciphertext[i]);
output = output + cTmp[i];
}
return output; }
//--------------------DES解密--------------------------------------------------
/**
*<b>Function:</b><br>
* -DES解密-.<br>
*@param StrTxt 参数说明<br>
* StrTxt<br>
* Type-- String<br>
* Comment:密文<br>
*@return
* Type-- String<br>
* 返回解密后的明文.
* @exception AllException
*/ public String decode(String StrTxt) throws Exception {
cipher.init(Cipher.DECRYPT_MODE, mykey);
String output = "";
// System.out.println("decode(String StrTxt)输入的密文为=" + StrTxt);
char[] cTmp = StrTxt.toCharArray();
byte[] plaintext1;
plaintext1 = new byte[cTmp.length];
for (int i = 0; i < cTmp.length; i++) {
plaintext1[i] = (byte) (cTmp[i]);
}
//此处指定编码方式了哈哈。肯定行的。
byte[] decryptedText = cipher.doFinal(plaintext1);
// System.out.println("decode(String StrTxt)1=" + byte2hex(decryptedText));
output = new String(decryptedText, "UTF8");
// System.out.println("decode(String StrTxt)输出的明文为=" + output);
return output;
}
给你我写的方法,密钥生产的方法不能给你,呵呵,不好意思。
记得给分
再放一两天吧,或者会有更多的收获的因为昨天考虑了一下RSA加密和DSA加密
虽然安全性和3DES差不多,但是不适合在网络上用,因为耗费cpu资源太多,加密解密速度慢所以还是决定用3DES算法
有很多加密算法的资料http://www.comms.engg.susx.ac.uk/fft/