java证书的加密与解密代码
java很多时候要对秘要进行持久化加密,此时的加密采用md5。采用对称加密的时候就采用DES方法了,那么java证书的加密与解密代码是什么呢?下面跟本站小编一起来学习一下吧!
以下两个类可以很方便的完成字符串的加密和解密:
加密:ypt(password)
解密:ypt(password)
代码如下:
[java]
package t;
import ception;
import er;
import enerator;
import etKey;
import 64;
public class CryptUtils {
private static String Algorithm = "DES";
private static byte[] DEFAULT_KEY=new byte[] {-53, 122, -42, -88, -110, -123, -60, -74};
private static String VALUE_ENCODING="UTF-8";
/**
* 生成密钥
*
* @return byte[] 返回生成的密钥
* @throws exception
* 扔出异常.
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = nstance(Algorithm);
SecretKey deskey = rateKey();
// if (debug ) tln ("生成密钥:"+byte2hex (ncoded
// ()));
return ncoded();
}
/**
* 将指定的数据根据提供的密钥进行加密
*
* @param input
* 需要加密的数据
* @param key
* 密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
public static byte[] encryptData(byte[] input, byte[] key) throws Exception {
SecretKey deskey = new etKeySpec(key, Algorithm);
// if (debug )
// {
// tln ("加密前的二进串:"+byte2hex (input ));
// tln ("加密前的字符串:"+new String (input ));
//
// }
Cipher c1 = nstance(Algorithm);
(YPT_MODE, deskey);
byte[] cipherByte = nal(input);
// if (debug ) tln ("加密后的二进串:"+byte2hex (cipherByte ));
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input, DEFAULT_KEY);
}
/**
* 将给定的已加密的数据通过指定的`密钥进行解密
*
* @param input
* 待解密的数据
* @param key
* 密钥
* @return byte[] 解密后的数据
* @throws Exception
*/
public static byte[] decryptData(byte[] input, byte[] key) throws Exception {
SecretKey deskey = new etKeySpec(key, Algorithm);
// if (debug ) tln ("解密前的信息:"+byte2hex (input ));
Cipher c1 = nstance(Algorithm);
(YPT_MODE, deskey);
byte[] clearByte = nal(input);
// if (debug )
// {
// tln ("解密后的二进串:"+byte2hex (clearByte ));
// tln ("解密后的字符串:"+(new String (clearByte )));
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input, DEFAULT_KEY);
}
/**
* 字节码转换成16进制字符串
*
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的16进制字符串
*/
public static String byte2hex(byte[] bytes) {
StringBuilder hs = new StringBuilder();
for(byte b : bytes)
nd(at("%1$02X", b));
return ring();
}
public static byte[] hex2byte(String content) {
int l=th()>>1;
byte[] result=new byte[l];
for(int i=0;i
int j=i<<1;
String s=tring(j, j+2);
result[i]=eOf(s, 16)Value();
}
return result;
}
/**
* 将字节数组转换为base64编码字符串
* @param buffer
* @return
*/
public static String bytesToBase64(byte[] buffer) {
-
2017计算机二级考试C语言冲刺题及答案
好的复习题是备考计算机二级考试C语言的关键,为了让考生更好地复习。本站小编为大家准备了计算机二级C语言冲刺题。欢迎阅读。一、选择题(1)下列关于栈叙述正确的是A)栈顶元素最先能被删除B)栈顶元素最后才能被删除C)栈底元素永远不能被删除D)以上三种说法都不...
-
2016下半年计算机二级VB上机考试答题攻略
计算机等级考试进入冲刺备考阶段,为帮助同学们考过计算机等级考试,本站小编为大家分享的是二级VB上机考试答题攻略——常用算法,希望对同学们考试有所帮助!常用算法熟练地掌握算法原理、编程思想和代码实现,就能够做到举一反三,轻松备考,顺利过关。1.累加...
-
2017年浙江省计算机三级网络技术考试试题
计算机网络包括计算机和网络两部分.其中计算机又称电子计算机,俗称电脑,是一种能够按照程序运行,自动、高速处理海量数据的现代化智能电子设备。下面是小编整理的关于计算机三级网络技术考试试题,欢迎大家参考!1).对于下列说法,错误的是()。协议可以提供可靠的数据...
-
2017年计算机二级MSOffice上机操作题及答案
2017年NCRE继续实施2013年版考试大纲(部分科目有修订),大纲和教材详情可查询本网站大纲教材栏目。下面是小编收集的计算机二级MSOffice上机操作题及答案,希望可以帮助大家!一、字处理题请在【答题】菜单下选择【进入考生文件夹】命令,并按照题目要求完成下面的操...