现需要使用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();
}

解决方案 »

  1.   

    CSDN的牛人们你们在哪里啊,各位英雄好汉拔刀吧
      

  2.   

        public static void EncryptFile(string sourceFile, string destFile)  
        {  
            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();  
                }  
            
        }  
      

  3.   

    /**
     * 文件采用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();
    }