代码如下,各位大侠帮忙分析下,谢谢!
/**
   * 
   * 使用JDK中的JCE生成密钥
   * @return
   * @throws Exception
   */
    public static byte[] generateKey() throws Exception 
{
        try 
{
            //生成一个可信任的随机数源
            SecureRandom sr = new SecureRandom();
            //为我们选择的DES算法生成一个KeyGenerator对象
            KeyGenerator kg = KeyGenerator.getInstance(algorithm);
            kg.init(sr);
            //生成密钥
            SecretKey key = kg.generateKey();
            //返回密钥的二进制形式
            return key.getEncoded();
        } catch (Exception e) 
        {
            throw new Exception("没有这种加密算法");
        }
    }   /**//**
    * 利用DES算法加密
    * @param s String 需要加密的字符串
    * @return String 加密后的字符串
    * @throws Exception
   */
  public byte[] encryptData(byte[] s,byte[] key) throws Exception {   // DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
  //从原始密钥数据创建DESKeySpec对象
          try{
           
          DESKeySpec dks = new DESKeySpec(key);          //创建一个密钥工厂,然后用它把DESKeySpec转换成Secret Key对象          SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);          SecretKey keySpec = keyFactory.generateSecret(dks);          //Cipher对象实际完成加密操作          Cipher cipher = Cipher.getInstance(algorithm);          //用密钥初始化Cipher对象          cipher.init(Cipher.ENCRYPT_MODE, keySpec, sr);
          //cipher.init(Cipher.DECRYPT_MODE, keySpec, sr);          //执行加密操作          byte[] cryptotext= cipher.doFinal(s);
          return cryptotext;
          
      } catch (InvalidKeyException e)       {
                  throw new Exception("密钥非法");              } catch (NoSuchAlgorithmException e) 
      {
                  throw new Exception("没有这种加密算法");              } catch (BadPaddingException e) 
      {
                  throw new Exception("加密失败");
              }    }/**
*读取文件
*
**/
 private static byte[] readFile(String filename) throws Exception 
{
        try 
{
        File f = new File(filename);
        FileInputStream fs = new FileInputStream(f);
        FileChannel fc = fs.getChannel();         //只要文件大小不超过2G(int的范围)即可加密
        int size = (int) fc.size();
        //采用DirectBuffer的方式读文件数据,属于JDK1.4的API,处理大容量数据更快
        ByteBuffer buffer=ByteBuffer.allocateDirect(size);
        //通过文件通道将文件内容读入ByteBuffer
        fc.read(buffer);
        //buffer操作完一次必须flip回到最初位置,才能再进行操作
        buffer.flip();
        byte[] byteArray=new byte[size];         //将缓冲区内容存入数组
        buffer.get(byteArray);
        buffer.clear();
        fc.close();
        fs.close();
        return byteArray;
        } catch (FileNotFoundException e) 
{
            throw new Exception("文件未找到");         } catch (IOException e) 
{
            throw new Exception("文件操作错误");
        }
    }
  
  
  
  /**//**
    * 测试程序
    * @param args String[]
    * @throws Exception
   */
  public static void main(String[] args) throws Exception {

     sample sample = new sample();
     
     byte[] cryptotext=sample.encryptData(sample.readFile("c:\\andy.txt"), sample.generateKey());
     
     String sl=new String(cryptotext);
     System.out.println(sl);
   }

解决方案 »

  1.   

    密文是二进制数据,你用 String sl=new String(cryptotext); 这个是不是在搞笑?你想用 ASCII 存储的话,可以转换成十六进制字符串,或者 Base64 字符串。
      

  2.   

    那建议使用 Base64 吧。到 http://commons.apache.org/codec/ 下载 Apache Commons Codec 的包,如果你的应用中已经有 commons-codec-x.x.x.jar 的话就不用下载了。具体这样用:import java.util.Arrays;import org.apache.commons.codec.binary.Base64;public class Test {    public static void main(String[] args) {
            byte[] bys = { 0, 1, 2, 3, 4, 5, 6, 7 };
            String base64 = encodeBase64(bys);
            System.out.println("encode base64: " + base64);
            System.out.println("decode base64: " + Arrays.toString(decodeBase64(base64)));
        }
        
        public static String encodeBase64(byte[] bys) {
            if(bys == null) {
                return null;
            }
            return new String(Base64.encodeBase64(bys));
        }
        
        public static byte[] decodeBase64(String base64) {
            if(base64 == null) {
                return null;
            }
            return Base64.decodeBase64(base64.getBytes());
        }
    }
      

  3.   

    public static String encodeBase64(byte[] bys) 
    public static byte[] decodeBase64(String base64) 楼主请注意,这里有2个方法,一个编码,一个解码。
    解密前,需要先解码了。
           
      

  4.   

    1:使用 Base64 的字符串调用 decodeBase64 这个方法,获得一个 byte 数组。
    2:根据这个 byte[] 使用 String str = new String(byte[], "gbk"); 就可以还原你原来中文了,当然了,你原来是按 GBK 来拆成字节的。