JAVA中的TripleDESCryptoServiceProvider

时间:2018-06-08 08:21:35

标签: android

我们正在将项目从.net EXE转移到Android App我们在.net中使用Triple DES Encryption用于登录表单,SQLite中将存在相同的数据库结构。 问题是我们想在java中使用Triple DES算法但是不能匹配确切的输出下面是.net中的代码想要Java中的等效代码

Public Function FunEncrypt(ByVal StrLocPlainText As String) As String
            Try
                ' Convert the plaintext string to a byte array.
                Dim ByLocPlainTextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(StrLocPlainText)

                ' Create the stream.
                Dim ObjLocMs As New System.IO.MemoryStream

                Dim ObjLocTripleDesSrvProv As New TripleDESCryptoServiceProvider

                ' Initialize the crypto provider.
                ObjLocTripleDesSrvProv.Key = FunTruncateHash(StrPriKey, ObjLocTripleDesSrvProv.KeySize \ 8)
                ObjLocTripleDesSrvProv.IV = FunTruncateHash("", ObjLocTripleDesSrvProv.BlockSize \ 8)

                ' Create the encoder to write to the stream.
                Dim CrypLocEncStream As New CryptoStream(ObjLocMs, ObjLocTripleDesSrvProv.CreateEncryptor(), CryptoStreamMode.Write)
                ' Use the crypto stream to write the byte array to the stream.
                CrypLocEncStream.Write(ByLocPlainTextBytes, 0, ByLocPlainTextBytes.Length)
                CrypLocEncStream.FlushFinalBlock()
                Dim StrLocReturn As String = Convert.ToBase64String(ObjLocMs.ToArray)

                ObjLocMs.Close()
                CrypLocEncStream.Close()
                ObjLocTripleDesSrvProv = Nothing
                ' Convert the encrypted stream to a printable string.
                Return StrLocReturn
            Catch ObjLocError As Exception
                Throw
            End Try
        End Function

Private Function FunTruncateHash(ByVal StrLocParKey As String, ByVal IntLocLength As Integer) As Byte()
        Try
            Dim ObjLocSha1SrvProv As New SHA1CryptoServiceProvider
            ' Hash the key.
            Dim ByLocKeyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(StrLocParKey)
            Dim ByLocHash() As Byte = ObjLocSha1SrvProv.ComputeHash(ByLocKeyBytes)

            ' Truncate or pad the hash.
            ReDim

0 个答案:

没有答案