2012-03-14 79 views
0
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而不是DESVB.Net DES加密功能,三重DES

回答

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 
+0

当我去隐窝我的文件,它说:“值不能为空参数名:inArray” – Epicblood 2012-03-14 05:11:23

+0

现在就来试试,刚刚更新的代码,看看如果你通过正确的数据。还TripleDES键必须是24长度不是8. – 2012-03-14 05:42:34

+0

好吧,我会尝试当我回家 – Epicblood 2012-03-14 14:53:33