我在网上摘抄了一段关于DES加密算法的程序,运行正常,如下,但现在我想将其添加一个加密函数和一个解密函数,原型为:
public String encrypt(String encryptStr);
public String decrypt(String decryptStr);
请问怎样写这两个函数???/*
安全程序 DESede/DES测试
*/
import java.security.*;
import javax.crypto.*;public class testDES 
{
    public static void main(String[] args)
    {
        testDES my = new testDES();
        my.run("要加密的信息");
    }    public String encrypt(String encryptStr)
    {
    }    public String decrypt(String decryptStr)
    {
    }
    
    public void run(String willDES) 
    {
        //添加新安全算法,如果用JCE就要把它添加进去
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        String Algorithm = "DES"; //定义 加密算法,可用 DES,DESede,Blowfish
        try 
        {
            //生成密钥
            KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
            SecretKey deskey = keygen.generateKey();
            
            //加密
            System.out.println("加密前的二进串:" + byte2hex(willDES.getBytes()));
            System.out.println("加密前的信息:" + willDES);
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.ENCRYPT_MODE, deskey);
            byte[] cipherByte = c1.doFinal(willDES.getBytes());
            System.out.println("加密后的二进串:" + byte2hex(cipherByte));
            
            //解密
            c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.DECRYPT_MODE,deskey);
            byte[] clearByte=c1.doFinal(cipherByte);
            System.out.println("解密后的二进串:" + byte2hex(clearByte));
            System.out.println("解密后的信息:" + (new String(clearByte)));
        }
        catch (java.security.NoSuchAlgorithmException e1) 
        {
            e1.printStackTrace();
        }
        catch (javax.crypto.NoSuchPaddingException e2) 
        {
            e2.printStackTrace();
        }
        catch (java.lang.Exception e3) 
        {
            e3.printStackTrace();
        }
    }
    
    private 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.*;public class testDES 
    {
    private String algorithm;     //加密算法
    private KeyGenerator keygen;  //密钥产生器
    private SecretKey deskey;     //密钥

    public testDES (String algorithm) {
            //添加新安全算法,如果用JCE就要把它添加进去
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    try {
    this.keygen = KeyGenerator.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
            this.deskey = keygen.generateKey();
    this.algorithm = algorithm;
    }
        public static void main(String[] args)
        {
            testDES my = new testDES("DES");
            //my.run("要加密的信息");
            String str = "要加密的信息";
            byte[] makeCode = my.encrypt(str);
            System.out.println("加密后的二进串:" + my.byte2hex(makeCode));
            String breakCode = my.decrypt(makeCode);
            System.out.println("解密后的信息:" + breakCode);
            
        }    public byte[] encrypt(String encryptStr)
        {
         byte[] cipherByte = null;
         try {
         Cipher c1 = Cipher.getInstance(algorithm);
            c1.init(Cipher.ENCRYPT_MODE, this.deskey);
            cipherByte = c1.doFinal(encryptStr.getBytes());
         }
         catch (java.security.NoSuchAlgorithmException e1) 
            {
                e1.printStackTrace();
            }
            catch (javax.crypto.NoSuchPaddingException e2) 
            {
                e2.printStackTrace();
            }
            catch (java.lang.Exception e3) 
            {
                e3.printStackTrace();
            }
            return cipherByte;
        }
    //
        public String decrypt(byte[] decryptBytes)
        {
         byte[] clearByte =null;
         try{
          Cipher c1 = Cipher.getInstance(algorithm);
          c1 = Cipher.getInstance(algorithm);
             c1.init(Cipher.DECRYPT_MODE,this.deskey);
             clearByte = c1.doFinal(decryptBytes);
         }
         catch (java.security.NoSuchAlgorithmException e1) 
            {
                e1.printStackTrace();
            }
            catch (javax.crypto.NoSuchPaddingException e2) 
            {
                e2.printStackTrace();
            }
            catch (java.lang.Exception e3) 
            {
                e3.printStackTrace();
            }
            
             return new String(clearByte);
        }
            
        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();
        }
    /**
     * @return the algorithm
     */
    public String getAlgorithm() {
    return algorithm;
    }
    /**
     * @param algorithm the algorithm to set
     */
    public void setAlgorithm(String algorithm) {
    this.algorithm = algorithm;
    }
    }
      

  2.   

    to zeroasan_():先看看,对了就给分.
      

  3.   

    to zeroasan_():对不起,你这个没有按照我的要求写,我的加密解密函数的返回和传入的参数应该均为String
      

  4.   

    并且这个程序编译时正确,运行时出现下列错误:
    D:\JavaTest>java testDES
    加密后的二进串:98:23:47:B7:EB:D2:84:B9:F4:E8:CD:6A:49:E3:FF:BB
    javax.crypto.BadPaddingException: Given final block not properly padded
            at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
            at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
            at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
            at javax.crypto.Cipher.doFinal(DashoA6275)
            at testDES.decrypt(testDES.java:106)
            at testDES.main(testDES.java:15)
    Exception in thread "main" java.lang.NullPointerException
            at java.lang.String.<init>(Unknown Source)
            at testDES.decrypt(testDES.java:120)
            at testDES.main(testDES.java:15)
      

  5.   

    要String参数,只要稍微修改一下就行了
    运行错误我就不知道了,我只能说我运行是对的
      

  6.   

    多谢zeroasan_():我是初学Java,就麻烦你帮忙改成String吧,谢谢!
      

  7.   

    不过改成String好象不是那么容易的.
      

  8.   

    我的想法是在JSP里面写一个数据库连接的字符串,这个字符串是加密后的字符串,再回到Java里面来进行解密,然后连接数据库,因为我不能在JSP里面显式的写上数据库的用户名和密码等信息.