使用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();
}
}
- 使用java进行文件加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- 使用java对文件或文件夹进行压缩和加密
- Java 关于进行文件加密
- Java 关于进行文件加密
- 使用CryptoAPI对文件进行加密解密
- 使用RijndaelManaged对epub文件进行加密
- java对文件进行加密解密操作
- 对文件进行key加密java源码
- Java异或进行文件加密
- 使用对称加密aes对文件进行zip加密解密
- 使用DES加密方式对文件进行加密
- java中使用MD5进行加密
- Firefox 火狐浏览器使用技巧
- 融合成发展新趋势 中国软件业正在起飞时
- 取得服务器ip地址
- 几种web打印方案的比较
- Linux下阅读.chm文档
- 使用java进行文件加密
- .net开发中的一些注意事项及小技巧
- ASP.NET對象
- 深入探讨.NET中的钩子技术
- 一个写的非常漂亮的数据库操作类
- Table导出为xls,doc,txt,htm方法
- 测试发表文章
- 普及与深化应用成为ERP市场的两大主题
- 在搜索结果出来之前页面显示“等待中...”的做法