【问题标题】:VB.Net DES encryption function, to Triple DESVB.Net DES加密功能,转三重DES
【发布时间】:2012-03-30 13:41:15
【问题描述】:
Public Shared Function DESEncrypt(ByVal Data As String, ByVal Key As String) As Byte()
    Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
    Try
        Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(Key, 8))
        Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(Data)
        Dim des As New DESCryptoServiceProvider
        Dim ms As New MemoryStream
        Dim cs As New CryptoStream(ms, des.CreateEncryptor(bykey, IV), CryptoStreamMode.Write)
        cs.Write(InputByteArray, 0, InputByteArray.Length)
        cs.FlushFinalBlock()
        Return ms.ToArray()
    Catch ex As Exception
    End Try
End Function

这是我目前的 DES 加密,但由于我对 VB.Net 还很陌生,我可以弄清楚如何使它使用三重 DES 而不是 DES

【问题讨论】:

    标签: vb.net encryption des tripledes


    【解决方案1】:

    试试这个

           Public Shared Function DESEncrypt(ByVal Data As String, ByVal Key As String) As Byte()
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
        Try
            Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(Key, 24))
    
    
            If String.IsNullOrEmpty(Data) Then
    
                Throw New ArgumentException("No data passed", "input")
    
            ElseIf bykey Is Nothing OrElse bykey.Length <> 24 Then
    
                Throw New ArgumentException("Invalid Key. Key must be 24 bytes length", "key")
    
            End If
    
            Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(Data)
    
            Using ms As New IO.MemoryStream
    
                Using des As New Security.Cryptography.TripleDESCryptoServiceProvider
    
    
                    Using cs As New Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(bykey, IV), Security.Cryptography.CryptoStreamMode.Write)
    
                        cs.Write(InputByteArray, 0, InputByteArray.Length)
                        cs.FlushFinalBlock()
                        Return ms.ToArray()
    
                    End Using
    
                End Using
    
            End Using
    
        Catch ex As Exception
        End Try
    
    End Function
    

    【讨论】:

    • 当我去加密我的文件时,它说“值不能为空参数名称:inArray”
    • 现在试试,刚刚更新了代码看看你是否传递了正确的数据。此外,TripleDES 密钥的长度必须为 24 而不是 8。
    猜你喜欢
    • 2011-10-22
    • 2017-04-21
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多