DES算法

package Temp;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.SecureRandom;


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;


import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;


/**
 * sun.misc.BASE64Decoder.jar
 * 
 DES 全称为Data Encryption Standard即数据加密算法,它是IBM公司研究成功并公开发表的。
 * DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被
 * a
 * 加密或被解密的数据; Mode为DES的工作方式,有两种:加密或解密。 DES算法是这样工作的:如Mode为加密,则用Key
 * 去把数据Data进行加密,生成Data的密码形式(64位)作为DES的输出结果;  
 *
 * 。故Key 实际可用位数便只有56位。 3-DES(TripleDES):该算法被用来解决使用 DES 技术的 56
 * 位时密钥日益减弱的强度,其方法是:使用两个密钥对明文运行
 * 
 * DES 算法三次,从而得到 112 位有效密钥强度。 TripleDES 又称为 DESede(表示加密、解密和加密这三个阶段)。
 * 扩展阅读:http://zh.wikipedia.org/zh/3DES
 * 
 */


public class A {


/** 指定加密算法为DESede */
private static String ALGORITHM = "DESede";
/** 指定密钥存放文件 */
private static String KEYFile = "C:\\Users\\Administrator\\Desktop\\a.txt";


/**
* 生成密钥
*/
private static void generateKey() throws Exception {
/** DES算法要求有一个可信任的随机数源 */
SecureRandom sr = new SecureRandom();
/** 为DES算法创建一个KeyGenerator对象 */
KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
/** 利用上面的随机数据源初始化这个KeyGenerator对象 */
kg.init(sr);
/** 生成密匙 */
SecretKey key = kg.generateKey();
/** 用对象流将生成的密钥写入文件 */
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
KEYFile));
oos.writeObject(key);
/** 清空缓存,关闭文件输出流 */
oos.close();
}


/**
* 加密方法

* source 源数据
*/
public static String encrypt(String source) throws Exception {
generateKey();
/** 将文件中的SecretKey对象读出 */
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
KEYFile));
SecretKey key = (SecretKey) ois.readObject();
/** 得到Cipher对象来实现对源数据的DES加密 */
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] b = source.getBytes();
/** 执行加密操作 */
byte[] b1 = cipher.doFinal(b);
BASE64Encoder encoder = new BASE64Encoder();
ois.close();
return encoder.encode(b1);
}


/**
* 解密密钥 cryptograph:密文
*/
public static String decrypt(String cryptograph) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
KEYFile));
SecretKey key = (SecretKey) ois.readObject();
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder decoder = new BASE64Decoder();
byte[] b1 = decoder.decodeBuffer(cryptograph);
byte[] b = cipher.doFinal(b1);
ois.close();
return new String(b);
}


public static void main(String[] args) throws Exception {
String source = "Hello";// 要加密的字符串
String cryptograph = encrypt(source);// 生成的密文
System.out.println("生成的密文:"+cryptograph);

String target = decrypt(cryptograph);// 解密密文
System.out.println("解密后为:"+target);

}
}

你可能感兴趣的:(DES算法)