java加密与解密

luxiangzjj 2007-09-18 09:44:54
我想用java将一个文本加密,请问有什么思路,我没写过此类的东西。。请指教@!
...全文
457 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
shadao 2007-10-16
  • 打赏
  • 举报
回复


/**
* 消息摘要
*
* @param input
* @return
* @throws NoSuchAlgorithmException
*/
public byte[] theOne(byte[] input) throws NoSuchAlgorithmException {
MessageDigest instance = MessageDigest.getInstance("md5");

return instance.digest(input);
}

/**
* 对称算法
*
* @param input
*
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public byte[] theOther(byte[] input) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
Cipher c = Cipher.getInstance("DES");

KeyGenerator keygen = null;
keygen = KeyGenerator.getInstance("DES");
keygen.init(56);

Key key = keygen.generateKey();// 这个key要用ObjectOutputStream 留下来

c.init(Cipher.ENCRYPT_MODE, key);

return c.doFinal(input);
}

/**
* 解密对称算法
*
* @param key 之前生成的key
* @param input
* @return
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
*/
public byte[] deTheOther(Key key, byte[] input)
throws IllegalBlockSizeException, BadPaddingException,
InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException {
Cipher c = Cipher.getInstance("DES");
c.init(Cipher.DECRYPT_MODE, key);

return c.doFinal(input);
}


非对称差不多的 区别只是算法不同 加密和解密分别用不同的Key而已
还有生成器不一样 貌似叫KeyPairGenerator
他们都是一对对出现的 哈哈

lz关注下CipherInputStream和CipherOutputStream
他们可以包装输出输入流 在实际运用中比直接使用Cipher实例还方便些
shadao 2007-10-16
  • 打赏
  • 举报
回复
俗称的“加密”分号几种

1:消息摘要
特点 不可逆
拥有特征:机密性,完整性
经典算法:md4 md5 4楼说得就是个消息摘要算法

2:对称加密
用同一个密钥加密和解密
拥有特征:机密性
经典算法:des

3:非对称加密
这个算法设计需要2个密钥,一个公开密钥,一个私有密钥
 其中一个密钥加密后,只有另一个密钥才能解密;这个很nb 不过一般实现效率都不高

拥有特征:机密性,可鉴别性,完整性,不可否认性
 经典算法:rsa


java自从5.0(好像是)以后就带上了jce
这个包里支持了很多主流加密算法

休息会,一会写个



 
wunan320 2007-10-16
  • 打赏
  • 举报
回复
可以用md5
luxiangzjj 2007-10-16
  • 打赏
  • 举报
回复
谢谢各位大侠了 小弟也会努力!
zhb_821012 2007-10-16
  • 打赏
  • 举报
回复
大体思路:
网上有加密算法的方法
加密完毕后会产生一个密钥
然后把密钥传给要使用加密后文本一端
对方有密钥就可以还原了
这是最安全的算法
dingkui 2007-10-15
  • 打赏
  • 举报
回复
我就写了个简单的加密程序,算法很简单,异或加密
/*
* @param input
* @return
*/
private byte[] encry(byte[] input,String password) {
byte[] pass = password.getBytes();
int j = 0;
for (int i = 0; i < input.length; i++) {
if(pass.length!=0){
j = i % pass.length;
input[i] ^= pass[j];
}
}
return input;
}
achao818 2007-09-20
  • 打赏
  • 举报
回复
看doc java.security

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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