簡體   English   中英

Java到VB.NET AES加密

[英]Java to VB.NET AES Encryption

我在這里有這段代碼,我很難轉換到VB.NET並希望有人幫助我。

我在密碼部分上遇到困難-我嘗試了Googling,但找不到簡單的術語來解釋它的清晰資源。 (我不知道該如何進行,因為它似乎無可比擬??)

我對Java幾乎一無所知,因此試圖在Google上查找每個部分的含義,然后將其轉換。 希望有人能夠指出正確的方向!

 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

試試看

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM