代码如下,各位大侠帮忙分析下,谢谢!
/**
*
* 使用JDK中的JCE生成密钥
* @return
* @throws Exception
*/
public static byte[] generateKey() throws Exception
{
try
{
//生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
//为我们选择的DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
kg.init(sr);
//生成密钥
SecretKey key = kg.generateKey();
//返回密钥的二进制形式
return key.getEncoded();
} catch (Exception e)
{
throw new Exception("没有这种加密算法");
}
} /**//**
* 利用DES算法加密
* @param s String 需要加密的字符串
* @return String 加密后的字符串
* @throws Exception
*/
public byte[] encryptData(byte[] s,byte[] key) throws Exception { // DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
//从原始密钥数据创建DESKeySpec对象
try{
DESKeySpec dks = new DESKeySpec(key); //创建一个密钥工厂,然后用它把DESKeySpec转换成Secret Key对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm); SecretKey keySpec = keyFactory.generateSecret(dks); //Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(algorithm); //用密钥初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, keySpec, sr);
//cipher.init(Cipher.DECRYPT_MODE, keySpec, sr); //执行加密操作 byte[] cryptotext= cipher.doFinal(s);
return cryptotext;
} catch (InvalidKeyException e) {
throw new Exception("密钥非法"); } catch (NoSuchAlgorithmException e)
{
throw new Exception("没有这种加密算法"); } catch (BadPaddingException e)
{
throw new Exception("加密失败");
} }/**
*读取文件
*
**/
private static byte[] readFile(String filename) throws Exception
{
try
{
File f = new File(filename);
FileInputStream fs = new FileInputStream(f);
FileChannel fc = fs.getChannel(); //只要文件大小不超过2G(int的范围)即可加密
int size = (int) fc.size();
//采用DirectBuffer的方式读文件数据,属于JDK1.4的API,处理大容量数据更快
ByteBuffer buffer=ByteBuffer.allocateDirect(size);
//通过文件通道将文件内容读入ByteBuffer
fc.read(buffer);
//buffer操作完一次必须flip回到最初位置,才能再进行操作
buffer.flip();
byte[] byteArray=new byte[size]; //将缓冲区内容存入数组
buffer.get(byteArray);
buffer.clear();
fc.close();
fs.close();
return byteArray;
} catch (FileNotFoundException e)
{
throw new Exception("文件未找到"); } catch (IOException e)
{
throw new Exception("文件操作错误");
}
}
/**//**
* 测试程序
* @param args String[]
* @throws Exception
*/
public static void main(String[] args) throws Exception {
sample sample = new sample();
byte[] cryptotext=sample.encryptData(sample.readFile("c:\\andy.txt"), sample.generateKey());
String sl=new String(cryptotext);
System.out.println(sl);
}
/**
*
* 使用JDK中的JCE生成密钥
* @return
* @throws Exception
*/
public static byte[] generateKey() throws Exception
{
try
{
//生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
//为我们选择的DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
kg.init(sr);
//生成密钥
SecretKey key = kg.generateKey();
//返回密钥的二进制形式
return key.getEncoded();
} catch (Exception e)
{
throw new Exception("没有这种加密算法");
}
} /**//**
* 利用DES算法加密
* @param s String 需要加密的字符串
* @return String 加密后的字符串
* @throws Exception
*/
public byte[] encryptData(byte[] s,byte[] key) throws Exception { // DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
//从原始密钥数据创建DESKeySpec对象
try{
DESKeySpec dks = new DESKeySpec(key); //创建一个密钥工厂,然后用它把DESKeySpec转换成Secret Key对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm); SecretKey keySpec = keyFactory.generateSecret(dks); //Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(algorithm); //用密钥初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, keySpec, sr);
//cipher.init(Cipher.DECRYPT_MODE, keySpec, sr); //执行加密操作 byte[] cryptotext= cipher.doFinal(s);
return cryptotext;
} catch (InvalidKeyException e) {
throw new Exception("密钥非法"); } catch (NoSuchAlgorithmException e)
{
throw new Exception("没有这种加密算法"); } catch (BadPaddingException e)
{
throw new Exception("加密失败");
} }/**
*读取文件
*
**/
private static byte[] readFile(String filename) throws Exception
{
try
{
File f = new File(filename);
FileInputStream fs = new FileInputStream(f);
FileChannel fc = fs.getChannel(); //只要文件大小不超过2G(int的范围)即可加密
int size = (int) fc.size();
//采用DirectBuffer的方式读文件数据,属于JDK1.4的API,处理大容量数据更快
ByteBuffer buffer=ByteBuffer.allocateDirect(size);
//通过文件通道将文件内容读入ByteBuffer
fc.read(buffer);
//buffer操作完一次必须flip回到最初位置,才能再进行操作
buffer.flip();
byte[] byteArray=new byte[size]; //将缓冲区内容存入数组
buffer.get(byteArray);
buffer.clear();
fc.close();
fs.close();
return byteArray;
} catch (FileNotFoundException e)
{
throw new Exception("文件未找到"); } catch (IOException e)
{
throw new Exception("文件操作错误");
}
}
/**//**
* 测试程序
* @param args String[]
* @throws Exception
*/
public static void main(String[] args) throws Exception {
sample sample = new sample();
byte[] cryptotext=sample.encryptData(sample.readFile("c:\\andy.txt"), sample.generateKey());
String sl=new String(cryptotext);
System.out.println(sl);
}
解决方案 »
- 问一个关于DTD的问题。。好心人进来帮忙看下
- 关于Spring中使用Quartz的问题?
- web前端 primefaces
- java得到window的分区问题
- struts标签
- spring-tomcat-weaver.jar,请问高手这个Jar包是干嘛使的?
- 求解 ssh 语句理解问题
- 大虾们过来看一看,servlet action threw exception 抛出的异常
- 请教eclipse中的问题,环境eclipse+Lomboz+Tomcat5.0
- cmp中配置了各个bean的relationship之后,如何应用阿?
- 紧急求助关于2个EJB项目表间的关联~~~~~~解决立刻给分
- JAVA 如何接收xml
byte[] bys = { 0, 1, 2, 3, 4, 5, 6, 7 };
String base64 = encodeBase64(bys);
System.out.println("encode base64: " + base64);
System.out.println("decode base64: " + Arrays.toString(decodeBase64(base64)));
}
public static String encodeBase64(byte[] bys) {
if(bys == null) {
return null;
}
return new String(Base64.encodeBase64(bys));
}
public static byte[] decodeBase64(String base64) {
if(base64 == null) {
return null;
}
return Base64.decodeBase64(base64.getBytes());
}
}
public static byte[] decodeBase64(String base64) 楼主请注意,这里有2个方法,一个编码,一个解码。
解密前,需要先解码了。
2:根据这个 byte[] 使用 String str = new String(byte[], "gbk"); 就可以还原你原来中文了,当然了,你原来是按 GBK 来拆成字节的。