首页 > 代码库 > DES加密算法

DES加密算法

 1 package com.tem1.util; 2  3 import java.security.InvalidKeyException;   4 import java.security.NoSuchAlgorithmException;   5 import java.security.Security;   6    7  8 import javax.crypto.BadPaddingException;   9 import javax.crypto.Cipher;  10 import javax.crypto.IllegalBlockSizeException;  11 import javax.crypto.KeyGenerator;  12 import javax.crypto.NoSuchPaddingException;  13 import javax.crypto.SecretKey;  14 15 import com.sun.crypto.provider.SunJCE;16   17 public class EncrypDES {  18       19     //KeyGenerator 提供对称密钥生成器的功能,支持各种算法  如:DES  DESede(3DES)  AES20     private KeyGenerator keygen;  21     //SecretKey 负责保存对称密钥  22     private SecretKey deskey;  23     //Cipher负责完成加密或解密工作  24     private Cipher c;  25     //该字节数组负责保存加密的结果  26     private byte[] cipherByte;  27       28     public EncrypDES() throws NoSuchAlgorithmException, NoSuchPaddingException{  29         Security.addProvider(new SunJCE());  30         //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)  31        // keygen = KeyGenerator.getInstance("DESede");  32         keygen = KeyGenerator.getInstance("DES");  33         //生成密钥  34         deskey = keygen.generateKey();  35         System.out.println("Key  :"+deskey);36         //生成Cipher对象,指定其支持的DES算法  37         c = Cipher.getInstance("DES");  38     }  39       40     /** 41      * 对字符串加密 42      *  43      * @param str 44      * @return 45      * @throws InvalidKeyException 46      * @throws IllegalBlockSizeException 47      * @throws BadPaddingException 48      */  49     public byte[] Encrytor(String str) throws InvalidKeyException,  50             IllegalBlockSizeException, BadPaddingException {  51         // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式  52         c.init(Cipher.ENCRYPT_MODE, deskey);  53       54         byte[] src =http://www.mamicode.com/ str.getBytes();  55         // 加密,结果保存进cipherByte  56         cipherByte = c.doFinal(src);  57         return cipherByte;  58     }  59   60     /** 61      * 对字符串解密 62      *  63      * @param buff 64      * @return 65      * @throws InvalidKeyException 66      * @throws IllegalBlockSizeException 67      * @throws BadPaddingException 68      */  69     public byte[] Decryptor(byte[] buff) throws InvalidKeyException,  70             IllegalBlockSizeException, BadPaddingException {  71         // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式  72         c.init(Cipher.DECRYPT_MODE, deskey);  73         cipherByte = c.doFinal(buff);  74         return cipherByte;  75     }  76   77     /** 78      * @param args 79      * @throws NoSuchPaddingException  80      * @throws NoSuchAlgorithmException  81      * @throws BadPaddingException  82      * @throws IllegalBlockSizeException  83      * @throws InvalidKeyException   84      */  85     public static void main(String[] args) throws Exception {  86         EncrypDES de1 = new EncrypDES();  87         String msg ="明天七点出发";  88         byte[] encontent = de1.Encrytor(msg);  89         byte[] decontent = de1.Decryptor(encontent);  90         System.out.println("明文是:" + msg);  91         System.out.println("加密后:" + new String(encontent));  92         System.out.println("解密后:" + new String(decontent));  93     }  94   95 }

DES加密算法