vb.net与java aes alogrithm不匹配

时间:2016-04-07 11:50:29

标签: vb.net

JAVA AES代码

public AESCrypt(String password) throws Exception {
    // hash password with SHA-256 and crop the output to 128-bit for key
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.update(password.getBytes("UTF-8"));
    byte[] keyBytes = new byte[32];
    System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
    cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    //cipher = Cipher.getInstance("AES/CBC/ZeroBytePadding");
    //cipher = Cipher.getInstance("AES/CBC/NoPadding");
    key = new SecretKeySpec(keyBytes, "AES");
    spec = getIV();
}

public AlgorithmParameterSpec getIV() {
    byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
    IvParameterSpec ivParameterSpec;
    ivParameterSpec = new IvParameterSpec(iv);

    return ivParameterSpec;
}

public String encrypt(String plainText) throws Exception {
    cipher.init(Cipher.ENCRYPT_MODE, key, spec);
    byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
    String encryptedText = new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");
    System.out.println("Encrypt Data" + encryptedText);
    return encryptedText;
}

请转移上面的java代码加密与下面的vb.net加密不匹配

Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String

    Dim AES As New System.Security.Cryptography.RijndaelManaged
    Dim Hash_AES As New System.Security.Cryptography.SHA256Managed
    Dim encrypted As String = ""
    Try
        Dim hash(31) As Byte
        Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(pass))
        Array.Copy(temp, 0, hash, 0, 16)
        Array.Copy(temp, 0, hash, 15, 16)
        AES.Key = hash
        AES.Mode = Security.Cryptography.CipherMode.ECB
        Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
        Dim Buffer As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(input)
        encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
    Catch ex As Exception
    End Try
    Return encrypted
End Function

1 个答案:

答案 0 :(得分:0)

你走了,

 Public Sub AESCrypt(ByVal password As String)
        ' hash password with SHA-256 and crop the output to 128-bit for key
        Dim digest As MessageDigest = MessageDigest.getInstance("SHA-256")
        digest.update(password.getBytes("UTF-8"))
        Dim keyBytes() As Byte = New Byte((32) - 1) {}
        System.arraycopy(digest.digest, 0, keyBytes, 0, keyBytes.length)
        cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
        'cipher = Cipher.getInstance("AES/CBC/ZeroBytePadding");
        'cipher = Cipher.getInstance("AES/CBC/NoPadding");
        key = New SecretKeySpec(keyBytes, "AES")
        spec = getIV
    End Sub

    Public Function getIV() As AlgorithmParameterSpec
        Dim iv() As Byte = New Byte() {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        Dim ivParameterSpec As IvParameterSpec
        ivParameterSpec = New IvParameterSpec(iv)
        Return ivParameterSpec
    End Function

    Public Function encrypt(ByVal plainText As String) As String
        cipher.init(Cipher.ENCRYPT_MODE, key, spec)
        Dim encrypted() As Byte = cipher.doFinal(plainText.getBytes("UTF-8"))
        Dim encryptedText As String = New String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8")
        System.out.println(("Encrypt Data" + encryptedText))
        Return encryptedText
    End Function

使用this tool转换。