0

可能重复:
Java 中的 AES 加密和 C# 中的解密

我有一个用 C# 加密的文件 alg 有我的 IV 和密钥,IV 是 16 字节,密钥是 32。加密属性是 AES/CBC/PKCS7Padding。完成加密后,我存储了密钥,因为这是一个 256 字节的加密,bytes[] 包含范围从 0 到 255 的数字。我已经证明,如果我使用小于 128 的所有数字加密文件,我的 Java 代码将正确解密文件. 但是,当我尝试解密 byte[] 包含大于 127 的内容的任何文件时,它无法解密,因为密钥不正确。当我查看 Java 中的字节时,它们会自动转换为有符号的,这意味着 241 被转换为 -14。我在想这些位应该是相同的,它应该可以工作,但事实并非如此。在 Java 代码中,我将字节放在一个字符串中,以验证它们正是 C# 中用于加密的内容,并且可以在 C# 上进行解密。但是当它们被放入传递的 byte[] 中时,它们会被翻译。

请帮忙

bool Encrypt(string fileIn,string fileOut, Guid archiveKey, SymmetricAlgorithm alg) { // 首先我们要打开文件流 var fsIn = new FileStream(fileIn, FileMode.Open, FileAccess.Read, FileShare.None); var fsOut = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);

        // Now create a crypto stream through which we are going
        // to be pumping data. 
        // Our fileOut is going to be receiving the encrypted bytes. 
        var x = alg.CreateEncryptor();
        var cs = new CryptoStream(fsOut,
            x, CryptoStreamMode.Write);

        // Now will will initialize a buffer and will be processing
        // the input file in chunks. 
        // This is done to avoid reading the whole file (which can
        // be huge) into memory. 
        bufferLen = Shared.GetFileAttributes.ProcessingBufferSize;
        buffer = new byte[bufferLen];

        do
        {
            // read a chunk of data from the input file 
            bytesRead = fsIn.Read(buffer, 0, bufferLen);

            // encrypt it 
            cs.Write(buffer, 0, bytesRead);
            var inc = bytesRead == Shared.GetFileAttributes.ProcessingBufferSize ? 1 : bytesRead / Shared.GetFileAttributes.ProcessingBufferSize;
        } while (bytesRead != 0);

        // close everything 

        // this will also close the unrelying fsOut stream
        cs.Close();
        fsIn.Close();
        return true;
    }

Java 代码 public void DecryptTheFile(String fileName, String fileOut, String keys) 抛出异常

 {
  setupKeys(IV, KEY,keys);

  // setup the cipher with the keys
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
  SecretKeySpec key = new SecretKeySpec(KEY, "AES/CBC/PKCS7Padding");
  IvParameterSpec ips = new IvParameterSpec(IV);
  cipher.init(Cipher.DECRYPT_MODE, key,ips);

  // decrypt a file
  byte[] buffer = new byte[1024];
  InputStream iFile = new FileInputStream(fileName);
  OutputStream oFile = new FileOutputStream(fileOut);

  iFile = new CipherInputStream(iFile, cipher);

  int r = 0;
  while ((r = iFile.read(buffer, 0, 1024)) > 0) {
   oFile.write(buffer, 0, r);
  }
  oFile.close();
  iFile.close();
 }
 private void setupKeys(byte[] IV,byte[] KEY,String keys)
 {
     String[] keyparts = keys.split(",");
     for (int i = 0; i < 16; i++)
     {
         Long l = Long.parseLong(keyparts[i]);
         IV[i] = (byte) (l.byteValue() );
         if (siv == null) siv = l.toString();
         else    siv = siv + l.toString();
     }
     for (int i = 16; i < 47; i++)
     {
         Long l = Long.parseLong(keyparts[i]);
         KEY[i - 16] = (byte) (l.byteValue() & 0xff);
         if (skey == null ) skey = l.toString();
         else            skey = skey + l.toString();
     }

 }
4

0 回答 0