DES加密、Base64加密\File文件相互转换
来源:互联网 发布:学会计的软件 编辑:程序博客网 时间:2024/05/18 13:12
注意:DES加密和解密过程中,密钥长度都必须是8的倍数
/**
* 加密*
* @param datasource byte[]
* @param password String
* @return byte[]
*/
public static byte[] encrypt(byte[] datasource, byte[] password) {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(password);
//创建一个密匙工厂,然后用它把DESKeySpec转换成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
//Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
//用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey);
//现在,获取数据并加密
//正式执行加密操作
return cipher.doFinal(datasource);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param src byte[]
* @param password String
* @return byte[]
* @throws Exception
*/
public static byte[] decrypt(byte[] src, byte[] password) {
// DES算法要求有一个可信任的随机数源
SecureRandom random = new SecureRandom();
// 创建一个DESKeySpec对象
DESKeySpec desKey = null;
try {
desKey = new DESKeySpec(password);
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 将DESKeySpec对象转换成SecretKey对象
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");//NoPadding
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey);
// 真正开始解密操作
return cipher.doFinal(src);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 将 16 进制字符串转换成 16 进制数字数组
* 在密钥传输交换阶段,使用DES进行加解密前需要调用以下方法对DES的密钥进行转 换,即转换后的才是正真的密钥。
*/
public static byte[] asc2bin(String hexString) {
byte[] hexbyte = hexString.getBytes();
byte[] bitmap = new byte[hexbyte.length / 2];
for (int i = 0; i < bitmap.length; i++) {
hexbyte[i * 2] -= hexbyte[i * 2] > '9' ? 7 : 0;
hexbyte[i * 2 + 1] -= hexbyte[i * 2 + 1] > '9' ? 7 : 0;
bitmap[i] = (byte) ((hexbyte[i * 2] << 4 & 0xf0) | (hexbyte[i * 2 + 1] & 0x0f));
}
return bitmap;
}
/**
* 把 16 进制数字数组换成16进制字符串
*/
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
/**
* BASE64 解密
*
* @param key 需要解密的字符串
* @return 字节数组
* @throws Exception
*/
public static byte[] decryptBase64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
/**
* BASE64 加密
*
* @param key 需要加密的字节数组
* @return 字符串
* @throws Exception
*/
public static String encryptBase64(byte[] key) throws Exception {
return (new BASE64Encoder()) .encodeBuffer(key);
}
/**将 file 转化为 Base64
* @throws Exception */
public static String fileToBase64(File file) throws Exception {
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
return encryptBase64(buffer);
}
/**将Base64 转换为file文件
* @throws FileNotFoundException */
public static boolean base64ToFile(String base64, File file) throws Exception {
byte[] buffer = decryptBase64(base64);
FileOutputStream out = new FileOutputStream(file);
out.write(buffer);
out.close();
return true;
}
阅读全文
0 0
- DES加密、Base64加密\File文件相互转换
- DES加密+Base64转换转实现代码
- des加密加密 base64 utf8
- DES+Base64 加密、解密
- DES加密 base64转化
- iOS DES 加密转base64
- DES+Base64 加密、解密详情
- iOS DES+Base64 加密、解密
- 有关Des加密,与base64加密技术
- String加密--MD5加密、Des加密、Base64加密
- des 和 3des 的base64加密
- des加密文件
- 文件DES加密解密
- des文件加密、解密
- DES加密文件
- DES加密解密文件
- des文件加密
- iOS 面试问答之加密形式: AES加密, DES加密, MD5加密,SHA1加密, BASE64加密
- CSS3笔记——渐变
- Junit 4学习和使用
- 机器学习实战【6】(非线性SVM)
- 手机端文档识别OCR识别技术
- BZOJ 4562 [Haoi2016]食物链
- DES加密、Base64加密\File文件相互转换
- 源码(七)
- python中字符串中一些函数的用法
- 《笨办法学python》加分习题37——我的答案
- Webstorm常用快捷键
- Oracle编程基础的东西
- python设计模式之享元模式
- Spring启动过程分析(一)
- 拦截过滤器模式