使用java进行文件加密

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();
  }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值