1

我在 vb.net 中编写了代码来加密内存流中的文件。我还解密文件并将内存流复制到文件以确保加密/解密工作。我的 vb 解决方案有效。

但是我需要使用 Java 解密。当我解密我的文件时,我总是得到一个额外的“?” 文件开头的字符,但除此之外,结果是完美的。有没有人见过这样的事情?我必须承认,我的结果来自仅使用一组数据,但我两次都使用新密钥和向量对其进行了加密。

一些细节。我在 vb 中使用 AES、PKCS7 填充,在 Java 中使用 PKCS5 填充。该文件可以是任意长度。任何帮助表示赞赏。

我是用手机发这个的,手边没有代码。我明天可以添加它。我只是希望这个描述能给某人敲响警钟。

谢谢, SH

当我在 VB 中写入 MemoryStream 时,我声明了一个 StreamWriter,如下所示:

Writer = New IO.StreamWriter(MS, System.Text.Encoding.UTF8)

这是我的 VB.NET 加密功能。

    Public Shared Function WriteEncryptedFile(ms As MemoryStream, FileName As String) As List(Of Byte())

    Try
        Dim original() As Byte
        Dim myAes As System.Security.Cryptography.Aes = Aes.Create()
        myAes.KeySize = 128
        myAes.Padding = PadMode
        Dim keys As New List(Of Byte())
        keys.Add(myAes.Key)
        keys.Add(myAes.IV)

        original = ms.ToArray
        Dim encryptor As ICryptoTransform = myAes.CreateEncryptor(myAes.Key, myAes.IV)
        Using FileEncrypt As New FileStream(FileName, FileMode.Create, FileAccess.Write)
            Using csEncrypt As New CryptoStream(FileEncrypt, encryptor, CryptoStreamMode.Write)
                csEncrypt.Write(original, 0, original.Length)
                csEncrypt.FlushFinalBlock()
                FileEncrypt.Flush()
                FileEncrypt.Close()
                csEncrypt.Close()
            End Using
        End Using

        Return keys
    Catch e As Exception
        MsgBox("Error during encryption." & vbCrLf & e.Message)
    End Try
    Return Nothing
End Function

这是Java解密:

public static void DecryptLIGGGHTSInputFile(String fileIn, String fileOut, String base64Key, String base64IV) throws Exception
{

    // Get the keys from base64 text
    byte[] key = Base64.decodeBase64(base64Key);
    byte[] iv= Base64.decodeBase64(base64IV);

    // Read fileIn into a byte[]
    int len = (int)(new File(fileIn).length());
    byte[] cipherText = new byte[len];
    FileInputStream bs = new FileInputStream(fileIn);
    bs.read(cipherText, 1, len-1);
    System.out.println(cipherText.length);
    System.out.println((double)cipherText.length/128);
    bs.close();

    // Create an Aes object 
    // with the specified key and IV. 
    Cipher cipher = null;
    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    // Encrypt the message. 
    SecretKey secret = new SecretKeySpec(key, "AES");

    /*
    cipher.init(Cipher.ENCRYPT_MODE, secret, ivspec);
    cipherText = cipher.doFinal("Hello, World!".getBytes("UTF-8"));
    System.out.println(cipherText);
    */

    cipher.init(Cipher.DECRYPT_MODE, secret , new IvParameterSpec(iv));
    String plaintext = new String(cipher.doFinal(cipherText), "UTF-8");
    System.out.println(plaintext.length());


    FileWriter fw = new FileWriter(fileOut);
    fw.write(plaintext);
    fw.close();
}
4

1 回答 1

1

一个 BOM 问题。当我用 VB 创建 MemoryStream 时,我用 UTF-8 编码对其进行了初始化。我文件中的第一个字符将流的大小和位置从 0 字节提升到 4 字节,而它本来应该只有一个。解决方案是创建基于 UTF-8 的编码,没有字节顺序标记,如下所示:

Dim UTF8EncodingWOBOM As New System.Text.UTF8Encoding(False) 'indicates to omit BOM
Writer = New IO.StreamWriter(MS, UTF8EncodingWOBOM)

我在这里读到,由于存在或缺少字节顺序标记,平台之间的编码不兼容经常存在问题,因为既不推荐也不要求这样做。用一个不对,用一个也没有错。您基本上必须找到一种方法来处理它们。大量其他文章和帖子提出了不同的方法来做到这一点。要点是,要么识别它们,要么在它们存在时处理它们。由于我可以控制写作和阅读,因此完全取消它们也很有意义。

上海

于 2013-07-22T13:23:39.667 回答