- /**
- * DES加密算法的演示验证
- * DES为对称加密算法,加密、解密双方使用同一个密钥
- * 密钥的长度固定为8byte,其中字节的最后一位为校验,因此实际长度为7byte(56bit)
- * 加密也是采用分组加密,每8byte明文加密成密文
- */
- package encrypto;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.security.InvalidKeyException;
- import java.security.NoSuchAlgorithmException;
- import javax.crypto.BadPaddingException;
- import javax.crypto.Cipher;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.KeyGenerator;
- import javax.crypto.NoSuchPaddingException;
- import javax.crypto.SecretKey;
- /**
- * @author liuhuabai,liuhuabai@163.com
- *
- */
- public class DESDemo {
- /**
- * 密钥
- */
- private SecretKey secretKey;
- /**
- * 加解密
- */
- private Cipher cipher;
- /**
- * 明文块的长度
- */
- private int originLength = 8;
- /**
- * 密文块的长度
- */
- private int encrytLength = 16;
- /**
- * 得到密钥
- * @throws NoSuchAlgorithmException
- */
- public void initKey() throws NoSuchAlgorithmException {
- KeyGenerator kengen = KeyGenerator.getInstance("DES");
- secretKey = kengen.generateKey();
- }
- /**
- * 将密钥保存至文件
- * @param file 待写入的文件
- * @return true 写入成功;false 写入失败
- */
- public boolean saveKey(File file) {
- boolean write;
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(file);
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- System.out.println(secretKey.getFormat());
- //注意,此处采用writeObject方法,读取时也要采用readObject方法
- oos.writeObject(secretKey);
- write = true;
- } catch (IOException e) {
- write = false;
- } finally {
- try {
- if(fos != null) fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return write;
- }
- /**
- * 从文件中得到密钥
- * @param file 保存密钥的文件
- */
- public void getKey(File file) {
- FileInputStream fis;
- try {
- //读取数据
- fis = new FileInputStream(file);
- ObjectInputStream ois = new ObjectInputStream(fis);
- secretKey = (SecretKey) ois.readObject();
- //关闭资源
- ois.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- /**
- * DES算法加密
- * @param origin 明文
- * @return 密文
- */
- protected byte [] encrypt(byte [] origin) {
- byte [] enc = null;
- try {
- cipher = Cipher.getInstance("DES");
- cipher.init(Cipher.ENCRYPT_MODE, secretKey);
- enc = cipher.doFinal(origin);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (NoSuchPaddingException e) {
- e.printStackTrace();
- } catch (InvalidKeyException e) {
- e.printStackTrace();
- } catch (IllegalBlockSizeException e) {
- e.printStackTrace();
- } catch (BadPaddingException e) {
- e.printStackTrace();
- }
- return enc;
- }
- /**
- * DES算法解密
- * @param enc 密文
- * @return 明文
- */
- protected byte [] decrypt(byte [] enc) {
- byte [] origin = null;
- try {
- cipher = Cipher.getInstance("DES");
- cipher.init(Cipher.DECRYPT_MODE, secretKey);
- origin = cipher.doFinal(enc);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (NoSuchPaddingException e) {
- e.printStackTrace();
- } catch (InvalidKeyException e) {
- e.printStackTrace();
- } catch (IllegalBlockSizeException e) {
- e.printStackTrace();
- } catch (BadPaddingException e) {
- e.printStackTrace();
- }
- return origin;
- }
- /**
- * 加密文件
- * @param origin 明文件
- * @throws IOException
- */
- public void encryptFile(File origin) throws IOException {
- FileInputStream fis = null;
- FileOutputStream fos = null;
- //读入
- fis = new FileInputStream(origin);
- BufferedInputStream bis = new BufferedInputStream(fis);
- byte [] originbyte = new byte [originLength];
- //写出
- fos = new FileOutputStream(new File(origin+".encrypt"));
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- byte [] encryptbyte;
- //int k;
- while(bis.read(originbyte) > 0) {
- encryptbyte = this.encrypt(originbyte);
- bos.write(encryptbyte);
- originbyte = new byte[originLength];
- }
- //压入
- bos.flush();
- //关闭资源
- if(fis != null) fis.close();
- if(fos != null) fos.close();
- }
- /**
- * 解密文件
- * @param encrypt 密文
- * @throws IOException
- */
- public void decryptFile(File encrypt) throws IOException {
- FileInputStream fis = null;
- FileOutputStream fos = null;
- //读入
- fis = new FileInputStream(encrypt);
- BufferedInputStream bis = new BufferedInputStream(fis);
- byte [] encryptbyte = new byte [encrytLength];
- //写出
- fos = new FileOutputStream(new File(encrypt+".decrypt"));
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- byte [] originbyte;
- //int k;
- while(bis.read(encryptbyte) > 0) {
- originbyte = this.decrypt(encryptbyte);
- bos.write(originbyte);
- encryptbyte = new byte [encrytLength];
- }
- //压入
- bos.flush();
- //关闭资源
- if(fis != null) fis.close();
- if(fos != null) fos.close();
- }
- }
DES加解密(Java)
最新推荐文章于 2022-08-04 10:34:22 发布