Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB2008与Android之间的加密/解密_Android_Vb.net_Encryption - Fatal编程技术网

VB2008与Android之间的加密/解密

VB2008与Android之间的加密/解密,android,vb.net,encryption,Android,Vb.net,Encryption,我使用这些代码加密vb2008中的任何纯文本。 我想解密并在我的android应用程序中使用生成的文件。 我知道将文件放在资产文件夹中,我会使用它。 这段代码使用加密技术来加密。 如何在android应用程序中解密生成的文件。 以及我如何在android应用程序中使用它们。 有什么想法吗 Imports System.Security.Cryptography Public NotInheritable Class Simple3Des Private TripleDes As Ne

我使用这些代码加密vb2008中的任何纯文本。
我想解密并在我的android应用程序中使用生成的文件。 我知道将文件放在资产文件夹中,我会使用它。
这段代码使用加密技术来加密。 如何在android应用程序中解密生成的文件。 以及我如何在android应用程序中使用它们。 有什么想法吗

Imports System.Security.Cryptography

Public NotInheritable Class Simple3Des

    Private TripleDes As New TripleDESCryptoServiceProvider

    Private Function TruncateHash(ByVal key As String, ByVal length As Integer) As Byte()

        Dim sha1 As New SHA1CryptoServiceProvider

        ' Hash the key.
        Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key)
        Dim hash() As Byte = sha1.ComputeHash(keyBytes)

        ' Truncate or pad the hash.
        ReDim Preserve hash(length - 1)
        Return hash
    End Function

    Sub New(ByVal key As String)
        ' Initialize the crypto provider.
        TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
        TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
    End Sub

    Public Function EncryptData(ByVal plaintext As String) As String

        ' Convert the plaintext string to a byte array.
        Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)

        ' Create the stream.
        Dim ms As New System.IO.MemoryStream
        ' Create the encoder to write to the stream.
        Dim encStream As New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

        ' Use the crypto stream to write the byte array to the stream.
         encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
         encStream.FlushFinalBlock()

        ' Convert the encrypted stream to a printable string.
        Return Convert.ToBase64String(ms.ToArray)
    End Function

    Public Function DecryptData(ByVal encryptedtext As String) As String

        ' Convert the encrypted text string to a byte array.
        Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)

         ' Create the stream.
         Dim ms As New System.IO.MemoryStream
        ' Create the decoder to write to the stream.
        Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

        ' Use the crypto stream to write the byte array to the stream.
        decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
        decStream.FlushFinalBlock()

        ' Convert the plaintext stream to a string.
        Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
    End Function

    End Class
在我的班级里

    Sub TestEncoding()
        Dim plainText As String = InputBox("Enter the plain text:")
        Dim password As String = InputBox("Enter the password:")

        Dim wrapper As New Simple3Des(password)
        Dim cipherText As String = wrapper.EncryptData(plainText)

        MsgBox("The cipher text is: " & cipherText)
        My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\cipherText.txt", cipherText, False)
    End Sub

这并不特定于Android,您需要使用JavaJCEAPI翻译代码。基本上你会的

  • 使用
    MessageDigest.getInstance(“SHA1”)
    获取SHA1实现
  • 散列您的密码以获取密钥字节和IV
  • 使用类似于
    Cipher.getInstance(“3DES/CBC/PKCS5Padding”)
    的方法来获得3DES实现
  • 使用密钥初始化加密密码,并使用
    cipher.init()初始化IV
  • 使用
    Cipher.doFinal()加密数据

  • 您需要首先确保获得与VB中相同的密钥和IV,然后继续加密。您似乎在散列一个空字符串以获取.NET中的IV,但不确定这会给您带来什么。还要检查.NET文档,以检查
    TripleDESCryptoServiceProvider
    的默认填充是什么。

    我是这方面的初学者,我的代码是从站点中的源代码复制的。plz型样品给我。谢谢你可能想先读一下这篇文章,然后试着把一些东西放在一起。当你陷入困境时,提出具体的问题。