我想加个main函数对下面的代码进行调试,谁帮我下。 还有下面代码需要引用的包是什么,可以帮我列一下吗?谢谢了/**
* 把生成的一对密钥保存到DesKey.xml文件中
*/
public static void saveDesKey(){
try {
// 初始化一个随即数据源
SecureRandom sr = new SecureRandom();
//为DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance ("DES" );
//初始化密钥生成器
kg.init (sr);
FileOutputStream fos = new FileOutputStream("C:/DesKey.xml");
ObjectOutputStream oos = new ObjectOutputStream(fos);//创建输出流对象
//生成密钥
Key key = kg.generateKey();
oos.writeObject(key); //关闭对象输出流
oos.close();//关闭文件输出流
} catch (Exception e) {
e.printStackTrace();
}
}
获取密钥方法如下:
/**
获得DES加密的密钥。在交易处理的过程中应该定时更换密钥。需要JCE的支持,如果jdk版本低于1.4,则需要安装jce-1_2_2才能正常使用。
@return Key 返回对称密钥
*/
public static Key getKey() {
Key kp = null;
try {
StringfileName = "conf/DesKey.xml"
//查找名称为fileName的资源
InputStream is = DesUtil.class.getClassLoader()
.getResourceAsStream(fileName);
//创建对象输入流,读取文件内容
ObjectInputStream oos = new ObjectInputStream(is);
//读取IS文件内容,并把它转换为Key型
kp = (Key) oos.readObject();
oos.close();//关闭对象输入流
} catch (Exception e) {
e.printStackTrace();
}
return kp;
}
文件采用DES算法加密文件/**
* 文件file进行加密并保存目标文件destFile中
* @param file
* 要加密的文件 如c:/test/srcFile.txt
* @param destFile
* 加密后存放的文件名 如c:/加密后文件.txt
*/public static void encrypt(String file, String destFile) throws Exception { Cipher cipher = Cipher.getInstance("DES");
//用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
//构造CipherInputStream,is将被处理的文件
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r); } cis.close(); is.close(); out.close(); }
文件采用DES算法解密文件
/**
* 文件file进行加密并保存目标文件destFile中* @param file
* 已加密的文件 如c:/加密后文件.txt
* @param destFile
* 解密后存放的文件名 如c:/ test/解密后文件.txt
*/public static void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, getKey());//指定为解密密钥,并获取密钥。
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
//解密过程
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
}
* 把生成的一对密钥保存到DesKey.xml文件中
*/
public static void saveDesKey(){
try {
// 初始化一个随即数据源
SecureRandom sr = new SecureRandom();
//为DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance ("DES" );
//初始化密钥生成器
kg.init (sr);
FileOutputStream fos = new FileOutputStream("C:/DesKey.xml");
ObjectOutputStream oos = new ObjectOutputStream(fos);//创建输出流对象
//生成密钥
Key key = kg.generateKey();
oos.writeObject(key); //关闭对象输出流
oos.close();//关闭文件输出流
} catch (Exception e) {
e.printStackTrace();
}
}
获取密钥方法如下:
/**
获得DES加密的密钥。在交易处理的过程中应该定时更换密钥。需要JCE的支持,如果jdk版本低于1.4,则需要安装jce-1_2_2才能正常使用。
@return Key 返回对称密钥
*/
public static Key getKey() {
Key kp = null;
try {
StringfileName = "conf/DesKey.xml"
//查找名称为fileName的资源
InputStream is = DesUtil.class.getClassLoader()
.getResourceAsStream(fileName);
//创建对象输入流,读取文件内容
ObjectInputStream oos = new ObjectInputStream(is);
//读取IS文件内容,并把它转换为Key型
kp = (Key) oos.readObject();
oos.close();//关闭对象输入流
} catch (Exception e) {
e.printStackTrace();
}
return kp;
}
文件采用DES算法加密文件/**
* 文件file进行加密并保存目标文件destFile中
* @param file
* 要加密的文件 如c:/test/srcFile.txt
* @param destFile
* 加密后存放的文件名 如c:/加密后文件.txt
*/public static void encrypt(String file, String destFile) throws Exception { Cipher cipher = Cipher.getInstance("DES");
//用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
//构造CipherInputStream,is将被处理的文件
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r); } cis.close(); is.close(); out.close(); }
文件采用DES算法解密文件
/**
* 文件file进行加密并保存目标文件destFile中* @param file
* 已加密的文件 如c:/加密后文件.txt
* @param destFile
* 解密后存放的文件名 如c:/ test/解密后文件.txt
*/public static void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, getKey());//指定为解密密钥,并获取密钥。
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
//解密过程
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货