Java到VB.NET AES加密

问题描述 投票:0回答:1

我在这里有这个代码,我很难转换到VB.NET,并希望有人帮助我。

我在密码上的部分有困难 - 我尝试了谷歌搜索,但无法找到一个简单的资源来解释它。 (我不知道如何继续它,因为它似乎没有相应的??)

我几乎不知道java因此我试图谷歌并找出每个部分的含义并转换后。希望有人能指出我正确的方向!

 public static byte[] decryptPDF(String password, String filePath) {
    try {
        byte[] headerSaltAndCipherText = Base64.decode(IOUtils.toString(new FileInputStream(new File(filePath)), "UTF-8").toString().getBytes("UTF-8"), 0);
        byte[] salt = Arrays.copyOfRange(headerSaltAndCipherText, 8, 16);
        byte[] encrypted = Arrays.copyOfRange(headerSaltAndCipherText, 16, headerSaltAndCipherText.length);
        Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[][] keyAndIV = EVP_BytesToKey(32, aesCBC.getBlockSize(), MessageDigest.getInstance(CommonUtils.MD5_INSTANCE), salt, password.getBytes("UTF-8"), 1);
        aesCBC.init(2, new SecretKeySpec(keyAndIV[0], "AES"), new IvParameterSpec(keyAndIV[1]));
        return aesCBC.doFinal(encrypted);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (NoSuchPaddingException e2) {
        e2.printStackTrace();
        return null;
    } catch (InvalidAlgorithmParameterException e3) {
        e3.printStackTrace();
        return null;
    } catch (NoSuchAlgorithmException e4) {
        e4.printStackTrace();
        return null;
    } catch (IllegalBlockSizeException e5) {
        e5.printStackTrace();
        return null;
    } catch (BadPaddingException e6) {
        e6.printStackTrace();
        return null;
    } catch (InvalidKeyException e7) {
        e7.printStackTrace();
        return null;
    }
}

到目前为止我尝试过的是......

Public Shared Function decryptPDF(ByVal password As String, ByVal fileAsString As String) As Byte()
    Try
        Dim headerSaltAndCipherText() As Byte = System.Convert.FromBase64String(fileAsString)
        Dim salt() As Byte = headerSaltAndCipherText.Skip(7).Take(8)
        Dim encrypted() As Byte = headerSaltAndCipherText.Skip(15).Take(headerSaltAndCipherText.Length)
        Dim aesCBC As Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
        Dim keyAndIV() As Byte = EVP_BytesToKey(32, aesCBC.getBlockSize, MessageDigest.getInstance(CommonUtils.MD5_INSTANCE), salt, password.getBytes("UTF-8"), 1)
        aesCBC.init(2, New SecretKeySpec(keyAndIV(0), "AES"), New IvParameterSpec(keyAndIV(1)))
        Return aesCBC.doFinal(encrypted)

    End Try
End Function
java vb.net encryption
1个回答
0
投票

试试吧

Public Shared Function decryptPDF(ByVal password As String, ByVal filePath As String) As Byte()
        Try 
            Dim headerSaltAndCipherText() As Byte = Base64.decode(IOUtils.toString(New FileInputStream(New File(filePath)), "UTF-8").toString.getBytes("UTF-8"), 0)
            Dim salt() As Byte = Arrays.copyOfRange(headerSaltAndCipherText, 8, 16)
            Dim encrypted() As Byte = Arrays.copyOfRange(headerSaltAndCipherText, 16, headerSaltAndCipherText.length)
            Dim aesCBC As Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
            Dim keyAndIV(,) As Byte = EVP_BytesToKey(32, aesCBC.getBlockSize, MessageDigest.getInstance(CommonUtils.MD5_INSTANCE), salt, password.getBytes("UTF-8"), 1)
            aesCBC.init(2, New SecretKeySpec(keyAndIV(0), "AES"), New IvParameterSpec(keyAndIV(1)))
            Return aesCBC.doFinal(encrypted)
        Catch e As IOException
            e.printStackTrace
            Return Nothing
        Catch e2 As NoSuchPaddingException
            e2.printStackTrace
            Return Nothing
        Catch e3 As InvalidAlgorithmParameterException
            e3.printStackTrace
            Return Nothing
        Catch e4 As NoSuchAlgorithmException
            e4.printStackTrace
            Return Nothing
        Catch e5 As IllegalBlockSizeException
            e5.printStackTrace
            Return Nothing
        Catch e6 As BadPaddingException
            e6.printStackTrace
            Return Nothing
        Catch e7 As InvalidKeyException
            e7.printStackTrace
            Return Nothing
        End Try

    End Function
© www.soinside.com 2019 - 2024. All rights reserved.