需要用到那些类?可以加密文本和图片格式文件。
最好有思路或实例能提供下。

解决方案 »

  1.   

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;public class EncryptGen { // static boolean debug =false ; // 加密KEY随便写入一段字符
     static final byte[] KEYVALUE = "qwrdsf".getBytes(); static final int BUFFERLEN = 512; public EncryptGen() {
     } /**
      * 对文件进行加密
      * 
      * @param String
      *            oldFile 原始要加密的文件
      * @param String
      *            newFile 加密后的文件
      * @return
      */
     public static void encryptFile(String oldFile, String newFile)
       throws Exception {
      FileInputStream in = new FileInputStream(oldFile);
      File file = new File(newFile);
      if (!file.exists())
       file.createNewFile();
      FileOutputStream out = new FileOutputStream(file);
      int c, pos, keylen;
      pos = 0;
      keylen = KEYVALUE.length;
      byte buffer[] = new byte[BUFFERLEN];
      while ((c = in.read(buffer)) != -1) {
       for (int i = 0; i < c; i++) {
        buffer[i] ^= KEYVALUE[pos];
        out.write(buffer[i]);
        pos++;
        if (pos == keylen)
         pos = 0;
       }
      }
      in.close();
      out.close();
     } /**
      * 对文件进行解密
      * 
      * @param String
      *            oldFile 原始要解密的文件
      * @param String
      *            newFile 解密后的文件
      * @return
      */
     public static void decryptFile(String oldFile, String newFile)
       throws Exception {
      FileInputStream in = new FileInputStream(oldFile);
      File file = new File(newFile);
      if (!file.exists())
       file.createNewFile();
      FileOutputStream out = new FileOutputStream(file);
      int c, pos, keylen;
      pos = 0;
      keylen = KEYVALUE.length;
      byte buffer[] = new byte[BUFFERLEN];
      while ((c = in.read(buffer)) != -1) {
       for (int i = 0; i < c; i++) {
        buffer[i] ^= KEYVALUE[pos];
        out.write(buffer[i]);
        pos++;
        if (pos == keylen)
         pos = 0;
       }
      }
      in.close();
      out.close();
     } /**
      * @param args
      */
     public static void main(String[] args) {
      // TODO Auto-generated method stub
      try {
       // debug =false ;   String oldFile = new String("C:\\aaa\\aa.txt");
       String newFile = new String("C:\\aaa\\aa_en.txt");
       encryptFile(oldFile, newFile);     System.out.println("ok");
      } catch (Exception e) {
       e.printStackTrace();
      } }}
      

  2.   

    楼上的很强,自家原创加密古典算法,不过我觉得如果想加密还是要要官方的一些JAVA加密解密包,例如JCT就是.
      

  3.   

    混杂原创和java自带的一些包 会更强大的!
      

  4.   

    sun本身就提供了加密算法的API。有现成的实现MD5算法的包。下面是一个简单MD5加密算法的实现package com.feng.common;import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;import sun.misc.BASE64Encoder;public class MD5Util {
    public static String md(String msg) {
    try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] b2 = md.digest(msg.getBytes());
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(b2);
    } catch (NoSuchAlgorithmException e) {
    return null;
    }
    }
    }传进去的字符串出来后就自动被加密了。