现需要使用C#对zip文件加密,zip文件中含有txt、excel、图片以及音视频文件;密钥是固定的:“RMB-GOLD-2012-Phone”,加密成功后用java代码解密,java中的加密解密代码如下,那么在C#中我该怎么加密才能使用下面的java代码正确解密呢,各位大大帮忙看看吧!/**
* 文件file进行加密并保存目标文件destFile中
*
* @param file
* 要加密的文件 如c:/test/srcFile.txt
* @param destFile
* 加密后存放的文件名 如c:/加密后文件.txt
*/
public void encrypt(String file, String destFile) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
// cipher.init(Cipher.ENCRYPT_MODE, getKey());
cipher.init(Cipher.ENCRYPT_MODE, this.key);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(destFile);
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算法解密文件
*
* @param file
* 已加密的文件 如c:/加密后文件.txt * @param destFile 解密后存放的文件名 如c:/
* test/解密后文件.txt
*/
public void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, this.key);
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();
}
* 文件file进行加密并保存目标文件destFile中
*
* @param file
* 要加密的文件 如c:/test/srcFile.txt
* @param destFile
* 加密后存放的文件名 如c:/加密后文件.txt
*/
public void encrypt(String file, String destFile) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
// cipher.init(Cipher.ENCRYPT_MODE, getKey());
cipher.init(Cipher.ENCRYPT_MODE, this.key);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(destFile);
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算法解密文件
*
* @param file
* 已加密的文件 如c:/加密后文件.txt * @param destFile 解密后存放的文件名 如c:/
* test/解密后文件.txt
*/
public void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, this.key);
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();
}
解决方案 »
- 希望能在propertygrid (C#)字段中添加自定义异常提示信息
- 关于反射后的对象操作
- 设置打印纸张规格
- 为什么VisualStudio的Intilisense没有识别出一个class
- 怎么在网页上动态显示数据库
- 为什么没有人理我啊555555555全选框功能不能实现的问题,谢谢
- 请问怎样做一个类来实现连接数据库服务器!
- 请问,怎么获得页面弹窗的ID?
- 我要在business service/logic 层调用dataset
- 怎样使Windows和Web两种界面很好地交互?
- 有用过vs2012的吗?是不是也很耗资源?metro程序是不是只能用vs2012开发?只能通过微软那个商店安装?
- asp.net oracle 权限不足
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);
//byte[] btKey = Encoding.Default.GetBytes(mykey);
//byte[] btIV = Encoding.Default.GetBytes(myiv);
//DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //建立一個Mode=ECB, Padding=None, Key為12345678的DESCryptoServiceProvider
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//Key的長度要64bits -> 8bytes;用ASCII編碼將Key轉為byte[]
des.Key = Encoding.ASCII.GetBytes(mykey);
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.None;
byte[] btFile = File.ReadAllBytes(sourceFile);
FileStream fs = null;
CryptoStream cs = null;
try
{
fs = new FileStream(destFile, FileMode.Create, FileAccess.Write);
cs = new CryptoStream(fs, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(btFile, 0, btFile.Length);
cs.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (null != cs)
cs.Close();
if(null!=fs)
fs.Close();
}
}
* 文件采用DES算法解密文件
*
* @param file
* 已加密的文件 如c:/加密后文件.txt * @param destFile 解密后存放的文件名 如c:/
* test/解密后文件.txt
*/
public static void decrypt(String file, String dest) throws Exception { // 建立解密所需的Key. 因為加密時的key是用ASCII轉換, 所以這邊也用ASCII做
DESKeySpec objDesKeySpec = new DESKeySpec(SECURE.getBytes("ASCII"));
SecretKeyFactory objKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey objSecretKey = objKeyFactory.generateSecret(objDesKeySpec); // 設定一個DES/ECB/NoPadding的Cipher
// ECB對應到.Net的CipherMode.ECB
// NoPadding對應到.Net的PaddingMode.None
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// 設定為解密模式, 並設定解密的key
cipher.init(Cipher.DECRYPT_MODE, objSecretKey); 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();
}