简体   繁体   English

Java到VB.NET AES加密

[英]Java to VB.NET AES Encryption

I have this code here which I'm having difficulty converting to VB.NET and hoping someone who give me a hand with it. 我在这里有这段代码,我很难转换到VB.NET并希望有人帮助我。

I'm having difficulty with the part on the cipher - I have tried Googling but was unable to find a clear resource explaining it in simple terms. 我在密码部分上遇到困难-我尝试了Googling,但找不到简单的术语来解释它的清晰资源。 (I don't know how to proceed with it as it seems there's no equivalent for it??) (我不知道该如何进行,因为它似乎无可比拟??)

I have almost no knowledge of java hence am trying to google and find out what each part means and convert it after. 我对Java几乎一无所知,因此试图在Google上查找每个部分的含义,然后将其转换。 Hope someone would be able to point me in the right direction! 希望有人能够指出正确的方向!

 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;
    }
}

What I've tried so far is... 到目前为止,我尝试过的是...

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

Try that 试试看

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