使用java进行文件加密

来源:互联网 发布:数据库mysql优化 编辑:程序博客网 时间:2024/05/16 09:08

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import java.security.Security;
import javax.crypto.spec.SecretKeySpec;

import java.net.URLEncoder;
import java.net.URLDecoder;

public class Encryption {
 /**
  * 测试用的主函数
  * @param args
  */
public static void main(String[] args){
    String str = "admin";
 //System.out.println("加密前:"+str);
 try {
  String f1= Encrypt(str);
  //System.out.println("加密后:"+f1);
  String f2= Decrypt(f1);
  //System.out.println("解密后:"+f2);
 } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 
 
 

  /**
   * 构造函数
   */
  public Encryption() {
    super();
    //
  }
 //定义加密算法,可用DES,DESede,Blowfish
 private static String Algorithm = "DES";
 private static String key = "*:@1$7!a";
 static{
   Security.addProvider(new com.sun.crypto.provider.SunJCE());
  }

  /**
  * 使用默认密钥加密字符串数据
  * @param input String
  * @throws Exception
  * @return String
  */
  public static String Encrypt(String cleartext) throws Exception{
  
   SecretKey skey = new SecretKeySpec(key.getBytes(),Algorithm);
   Cipher cipher = Cipher.getInstance(Algorithm);
   cipher.init(Cipher.ENCRYPT_MODE,skey);
   byte[] cipherByte = cipher.doFinal(cleartext.getBytes());
   //转码,不转码为iso-8859-1会出一些问题
   return URLEncoder.encode(new String(cipherByte,"ISO-8859-1"),"ISO-8859-1");
  
  }

  /**
  * 使用密钥解密字节码数据,用来解密的密钥与加密密钥相同(单钥密码体制)
  * @param ciphertext String
  * @throws Exception
  * @return String
  */
  public static String Decrypt(String ciphertext) throws Exception{
   //解码
    ciphertext=URLDecoder.decode(ciphertext,"ISO-8859-1");
   SecretKey skey = new SecretKeySpec(key.getBytes(),Algorithm);
   Cipher cipher = Cipher.getInstance(Algorithm);
   cipher.init(Cipher.DECRYPT_MODE,skey);
   byte[] clearByte = cipher.doFinal(ciphertext.getBytes("ISO-8859-1"));
   return new String(clearByte);
  }
  /**
  * 使用默认密钥加密字符串数据
  * @param input String
  * @throws Exception
  * @return String
  */
  public static String EncryptForFloat(float cleartext) throws Exception{
  
   SecretKey skey = new SecretKeySpec(key.getBytes(),Algorithm);
   Cipher cipher = Cipher.getInstance(Algorithm);
   cipher.init(Cipher.ENCRYPT_MODE,skey);
  // byte[] cipherByte = cipher.doFinal(cleartext.getBytes());  
   //转码,不转码为iso-8859-1会出一些问题
  // return URLEncoder.encode(new String(cipherByte,"ISO-8859-1"),"ISO-8859-1");
   byte[] cipherByte = cipher.doFinal(String.valueOf(cleartext).getBytes());  
   //转码,不转码为iso-8859-1会出一些问题
   String temp = URLEncoder.encode(new String(cipherByte,"ISO-8859-1"),"ISO-8859-1");
  // return Float.valueOf(temp).floatValue();
   return temp;
  }

  /**
  * 使用密钥解密字节码数据,用来解密的密钥与加密密钥相同(单钥密码体制)
  * @param ciphertext String
  * @throws Exception
  * @return String
  */
  public static float DecryptForFloat(String ciphertext) throws Exception{
   //解码
   String temp1 = String.valueOf(ciphertext);
   temp1=URLDecoder.decode(temp1,"ISO-8859-1");
   SecretKey skey = new SecretKeySpec(key.getBytes(),Algorithm);
   Cipher cipher = Cipher.getInstance(Algorithm);
   cipher.init(Cipher.DECRYPT_MODE,skey);
   byte[] clearByte = cipher.doFinal(temp1.getBytes("ISO-8859-1"));
   String temp2=new String(clearByte);  
   return Float.valueOf(temp2).floatValue();
  }
}