package Lib;/*安全程序 DESede/DES测试*/
import java.security.*;
import javax.crypto.*;
import java.lang.*;

public class TmpDES
{
public String sAlgorithm = "DES";
public SecretKey deskey; public TmpDES()
{
// 
}

    public static void main(String[] args)
    {
     String testInfo = "要加密的信息";
        TmpDES app = new TmpDES();        
        System.out.println("加密前的信息:" + testInfo);
        System.out.println("加密前的二进串:" + app.byte2hex(testInfo.getBytes()));
        String sEncryptInformation = app.Encrypt(testInfo);
        System.out.println("加密后的信息:" + sEncryptInformation);
        System.out.println("加密后的二进串:" + app.byte2hex(sEncryptInformation.getBytes()));        System.out.println("================================================================");
        System.out.println("解密前的信息:" + sEncryptInformation);
System.out.println("解密前的二进串:" + app.byte2hex(sEncryptInformation.getBytes()));
        String sDecryptInformation = app.Decrypt(sEncryptInformation);
        System.out.println("解密后的信息:" + sDecryptInformation);
System.out.println("解密后的二进串:" + app.byte2hex(sDecryptInformation.getBytes()));
    }
    
/**
 * 加密
 * 
*/
    public  String Encrypt(String sInformation)
    {        
        // 添加新安全算法,如果用JCE就要把它添加进去 
        Security.addProvider(new com.sun.crypto.provider.SunJCE());        

       try
       {
           // 生成密钥   
           KeyGenerator keygen = KeyGenerator.getInstance(sAlgorithm);
           deskey = keygen.generateKey();
           System.out.println("密钥:" + deskey);
           // 加密 
           Cipher cipher = Cipher.getInstance(sAlgorithm);
           cipher.init(Cipher.ENCRYPT_MODE,deskey);
           byte[] cipherByte = cipher.doFinal(sInformation.getBytes());   
           return byte2hex(cipherByte);
        } 
        catch(NoSuchAlgorithmException e1)
        {
         System.out.println("NoSuchAlgorithmException:" + e1.getMessage());
         return null;
        }
        catch(NoSuchPaddingException e2)
        {
         System.out.println("NoSuchPaddingException:" + e2.getMessage());
         return null;
        }
        catch(Exception e3)
        {
         System.out.println("Exception:" + e3.getMessage());
         return null;
        }
    }
    
    public  String Decrypt(String sInformation)
    {
        // 添加新安全算法,如果用JCE就要把它添加进去 
        //Security.addProvider(new com.sun.crypto.provider.SunJCE());

       try
       {                      
   System.out.println("密钥:" + deskey);
           // 解密   
           Cipher cipher = Cipher.getInstance(sAlgorithm);
           cipher.init(Cipher.DECRYPT_MODE, deskey);
           byte[] cipherByte = cipher.doFinal(sInformation.getBytes());
   return byte2hex(cipherByte);
        } 
        catch(NoSuchAlgorithmException e1)
        {
         System.out.println("NoSuchAlgorithmException:" + e1.getMessage());
         return null;
        }
        catch(NoSuchPaddingException e2)
        {
         System.out.println("NoSuchPaddingException:" + e2.getMessage());
         return null;
        }
        catch(Exception e3)
        {
         System.out.println("Exception:" + e3.getMessage());
         return null;
        }
    }
    
    // 二行制转字符串 
    public String byte2hex(byte[] b)    
    {     
String hs="";
String stmp="";
for (int n=0; n<b.length;n++)
{
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1)
{
    hs=hs+"0"+stmp;
} else {
    hs=hs+stmp;
}
if (n<b.length-1)
{
    hs=hs+":";
}
}
return hs.toUpperCase();
    }
}

解决方案 »

  1.   

    import java.security.*;
    import javax.crypto.*;
    import java.lang.*;public class Test {
        public String sAlgorithm = "DES";
        public SecretKey deskey;    public Test() {
            // 
        }    public static void main(String[] args) {
            String testInfo = "我是莫飞";
            Test app = new Test();
            System.out.println("加密前的信息:" + testInfo);
            System.out.println("加密前的二进串:" + app.byte2hex(testInfo.getBytes()));
            byte[] sEncryptInformation = app.Encrypt(testInfo.getBytes());
            System.out.println("加密后的二进串:" + app.byte2hex(sEncryptInformation));
            System.out.println("加密后的信息:" + new String(sEncryptInformation));
            byte[] sDecryptInformation = app.Decrypt(sEncryptInformation);
            System.out.println("解密后的二进串:" + app.byte2hex(sDecryptInformation));
            System.out.println("解密后的信息:" + new String(sDecryptInformation));
        }    /**
         * 加密
         * 
         */
        public byte[] Encrypt(byte[] sInformation) {
            // 添加新安全算法,如果用JCE就要把它添加进去
            // Security.addProvider(new com.sun.crypto.provider.SunJCE());        try {
                // 生成密钥
                KeyGenerator keygen = KeyGenerator.getInstance(sAlgorithm);
                deskey = keygen.generateKey();
                System.out.println("密钥:" + deskey);
                // 加密
                Cipher cipher = Cipher.getInstance(sAlgorithm);
                cipher.init(Cipher.ENCRYPT_MODE, deskey);
                byte[] cipherByte = cipher.doFinal(sInformation);
                return cipherByte;
            } catch (NoSuchAlgorithmException e1) {
                System.out.println("NoSuchAlgorithmException:" + e1.getMessage());
                return null;
            } catch (NoSuchPaddingException e2) {
                System.out.println("NoSuchPaddingException:" + e2.getMessage());
                return null;
            } catch (Exception e3) {
                e3.printStackTrace();
                System.out.println("Exception:" + e3.getMessage());
                return null;
            }
        }    public byte[] Decrypt(byte[] sInformation) {
            // 添加新安全算法,如果用JCE就要把它添加进去
            // Security.addProvider(new com.sun.crypto.provider.SunJCE());        try {
                System.out.println("密钥:" + deskey);
                // 解密
                Cipher cipher = Cipher.getInstance(sAlgorithm);
                cipher.init(Cipher.DECRYPT_MODE, deskey);
                byte[] cipherByte = cipher.doFinal(sInformation);
                return cipherByte;
            } catch (NoSuchAlgorithmException e1) {
                System.out.println("NoSuchAlgorithmException:" + e1.getMessage());
                return null;
            } catch (NoSuchPaddingException e2) {
                System.out.println("NoSuchPaddingException:" + e2.getMessage());
                return null;
            } catch (Exception e3) {
                e3.printStackTrace();
                System.out.println("Exception:" + e3.getMessage());
                return null;
            }
        }    // 二行制转字符串
        public String byte2hex(byte[] b) {
            String hs = "";
            String stmp = "";
            for (int n = 0; n < b.length; n++) {
                stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
                if (stmp.length() == 1) {
                    hs = hs + "0" + stmp;
                } else {
                    hs = hs + stmp;
                }
                if (n < b.length - 1) {
                    hs = hs + ":";
                }
            }
            return hs.toUpperCase();
        }
    }