关于java的3DES算法的例子或者资料,Up 都会有分

little06 2003-10-30 09:40:21
在网上苦苦找Java的3DES算法实现已经两天了
还是一无所获,真是痛苦啊,希望各位能给一点帮助或者提示,没有的话,Up也给分啊

还有能否提供一些关于Java加密的电子书也可以啊
谢谢
...全文
182 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
becool 2003-11-04
  • 打赏
  • 举报
回复
看看这个地方
有很多加密算法的资料

http://www.comms.engg.susx.ac.uk/fft/
little06 2003-11-04
  • 打赏
  • 举报
回复
UP
资料不够,不能实现
sunny110 2003-11-01
  • 打赏
  • 举报
回复
up
little06 2003-10-31
  • 打赏
  • 举报
回复
Up多一次 谢谢帮忙
再放一两天吧,或者会有更多的收获的

因为昨天考虑了一下RSA加密和DSA加密
虽然安全性和3DES差不多,但是不适合在网络上用,因为耗费cpu资源太多,加密解密速度慢

所以还是决定用3DES算法
Mars_Mao 2003-10-31
  • 打赏
  • 举报
回复
看来有这么多多朋友遇到此类问题,相当出我也花了不少功夫,为了大家不要犯同样的错误,需要解决此类问题或一些JAVA问题的上QQ或MSN找我把。

QQ:249127610
MSN:Ares_Mao@msn.com

大家一起交流交流
dreamhead 2003-10-31
  • 打赏
  • 举报
回复
对于我的程序补充两句,我的JDK版本是1.4.1,我的程序的密钥和明文是相同的,所以只要参考用法就可以了,更具体的用法可以参考Java的文档。
abcder 2003-10-31
  • 打赏
  • 举报
回复
我也需要啊!
up一下
langzitianya 2003-10-31
  • 打赏
  • 举报
回复
俺还没有想过加密算法的事,不过有好的东东出来的话俺也要下了保存起来,up了!
Mars_Mao 2003-10-31
  • 打赏
  • 举报
回复
//--------------------得到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;
}




给你我写的方法,密钥生产的方法不能给你,呵呵,不好意思。
记得给分
dreamhead 2003-10-31
  • 打赏
  • 举报
回复
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();
}
clare0peng 2003-10-31
  • 打赏
  • 举报
回复
如果google都搜索不到,那也就沒有甚麼辦法了
xinshou1979330 2003-10-31
  • 打赏
  • 举报
回复
帮楼主顶吧

关注
singnet 2003-10-31
  • 打赏
  • 举报
回复
up
ker456 2003-10-31
  • 打赏
  • 举报
回复
新手上路!我对这个话题很感兴趣,我也up一下!
coolfeelJava 2003-10-31
  • 打赏
  • 举报
回复
up
little06 2003-10-30
  • 打赏
  • 举报
回复
请帮忙顶一下
谢谢帮忙了

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧