===================使用DES进行加密和解密============================
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.security.SecureRandom;
/**
* 功能: DES加密
*/
private String encrypt(String key, String source){
String algorithm = "DES";
byte [] cipherByte = null;
try {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecureRandom sr = new SecureRandom();
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher c1 = Cipher.getInstance(algorithm);
c1.init(Cipher.ENCRYPT_MODE, securekey, sr);
cipherByte = c1.doFinal(source.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
BASE64Encoder base64encoder = new BASE64Encoder();
String encode=base64encoder.encode(cipherByte);
return encode;
}
/**
* 功能: DES解密
* @param key
* @param source
* @return
*/
private String decrypt(String key, String source){
String algorithm = "DES";
SecureRandom sr = new SecureRandom();
String result = null;
try {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encodeByte = base64decoder.decodeBuffer(source);
byte[] cipherByte = cipher.doFinal(encodeByte);
result = new String(cipherByte, "GBK");
} catch (Exception e){
e.printStackTrace();
}
return result;
}
分享到:
相关推荐
DES加密与解密算法实现源代码,是我们上课时做的作业
DES加密与解密方法DES加密与解密方法DES加密与解密方法
DES加密与解密(初学者)
用C语言实现DES加密与解密.doc
用纯C语言实现的DES加密与解密算法,希望对朋友们有用。
DES加密与解密程序,利用数组作为储存方式
QR码DES加密与解密小详解
DES加密与解密算法的C程序,在main()函数中指定明文、密钥、密文对文件进行加解密,如:DES_Encrypt("f.txt","computer","c.txt")。
C#.net DES加密与解密程序 开发环境: C# .NET 2005(转)
DES加密与解密算法的实现
md5、3des加密与解密 相信对有需要的人是一个很好的帮助
基于net的DES与3DES加密数据与解密源码,vb.net 实现。
DES加密DES,使用java编译,使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.。
des加密与解密算法.rar..................
采用简易CBC模式,简单的c++语句实现DES加密与解密,清晰易懂,结合DES的有关详解食用更加
C#源码DES加密与解密,密钥为8个字符