des加密java代码 java实现des加密解密( 三 )


// InputStream is = Encrypt.class.getClassLoader().getResourceAsStream(fileName);
// 绝对路径
String fileName = "d:/DesKey.xml";
FileInputStream is = new FileInputStream(fileName);
ObjectInputStream oos = new ObjectInputStream(is);
kp = (Key) oos.readObject();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
return kp;
}
//加密开始
public static void encrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
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();
}
//解密开始
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();
}
}
//des加密主方法
public class DES {
public static void main(String[] args) throws Exception {
Encrypt.saveDesKey();
System.out.println("生成key");
Encrypt.getKey();
System.out.println("获取key");
Encrypt.encrypt("d:\\hello.txt", "d:\\encrypt.txt");
System.out.println("加密");
Encrypt.decrypt("d:\\encrypt.txt", "d:\\decrypt.txt");
System.out.println("解密");
}
以上方法亲测可用 。
Des加密解密方法 用java C#和C++三种方式实现Solaris下的系统 , 有一个用C做的加密工具,调用Sunwcry的des(1)对文件进行加密 , 然后在java中对文件进行解密 。java中用的是标准的DES/CBC/NoPadding算法,可是解密后发现开头有8byte的数据出错了,请高人指点一下 。
cbc_encrypt.c : 加密用的C程序
cbc_decrypt.c:解密用的C程序
TestDescbc.java:解密用的java程序
Test01.dat原始文件
Test03.datcbc_encrypt加密后的文件
Test05.dat cbc_decrypt解密后的文件
Test06.dat TestDescbc解密后的文件
【des加密java代码 java实现des加密解密】des加密java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现des加密解密、des加密java代码的信息别忘了在本站进行查找喔 。

推荐阅读