2013-09-23 89 views
0

我已經解碼的字符串「測試」使用下面的函數在Visual Basic中3DES:TripleDes的解密問題

Imports System.IO 
Imports System.Text 
Imports System.Security.Cryptography 

Friend Class cTripleDES 

    ' define the triple des provider 
    Private m_des As New TripleDESCryptoServiceProvider 

    ' define the string handler 
    Private m_utf8 As New ASCIIEncoding 

    ' define the local property arrays 
    Private m_key() As Byte 
    Private m_iv() As Byte 

    Public Sub New(ByVal key() As Byte, ByVal iv() As Byte) 
     Me.m_key = key 
     Me.m_iv = iv 
    End Sub 

    Public Function Encrypt(ByVal input() As Byte) As Byte() 
     Return Transform(input, m_des.CreateEncryptor(m_key, m_iv)) 
    End Function 

    Public Function Decrypt(ByVal input() As Byte) As Byte() 
     Return Transform(input, m_des.CreateDecryptor(m_key, m_iv)) 
    End Function 

    Public Function Encrypt(ByVal text As String) As String 
     Dim input() As Byte = m_utf8.GetBytes(text) 
     Dim output() As Byte = Transform(input, _ 
         m_des.CreateEncryptor(m_key, m_iv)) 
     Return Convert.ToBase64String(output) 
    End Function 

    Public Function Decrypt(ByVal text As String) As String 
     Dim input() As Byte = Convert.FromBase64String(text) 
     Dim output() As Byte = Transform(input, _ 
         m_des.CreateDecryptor(m_key, m_iv)) 
     Return m_utf8.GetString(output) 
    End Function 

    Private Function Transform(ByVal input() As Byte, _ 
     ByVal CryptoTransform As ICryptoTransform) As Byte() 
     ' create the necessary streams 
     Dim memStream As MemoryStream = New MemoryStream 
     Dim cryptStream As CryptoStream = New _ 
      CryptoStream(memStream, CryptoTransform, _ 
      CryptoStreamMode.Write) 
     ' transform the bytes as requested 
     cryptStream.Write(input, 0, input.Length) 
     cryptStream.FlushFinalBlock() 
     ' Read the memory stream and convert it back into byte array 
     memStream.Position = 0 
     Dim result(CType(memStream.Length - 1, System.Int32)) As Byte 
     memStream.Read(result, 0, CType(result.Length, System.Int32)) 
     ' close and release the streams 
     memStream.Close() 
     cryptStream.Close() 
     ' hand back the encrypted buffer 
     Return result 
    End Function 

我把它叫做使用此功能

Imports System.Security.Cryptography 
Imports System.IO 
Imports System.Text 
Module tripledes 

    Function Encrypt(ByVal text As String) As String 
     ' define the local key and vector byte arrays 
     Dim key() As Byte = Encoding.ASCII.GetBytes("MyKeygoeshere12345678901") 
     Dim iv() As Byte = Encoding.ASCII.GetBytes("Heretheiv") 

     ' instantiate the class with the arrays 
     Dim des As New cTripleDES(key, iv) 

     ' the value of decryptedData should be "test", 
     ' but for our example purposes, let's re-encrypt it 
     Return des.Encrypt(text) 
    End Function 
    Function Decrypt(ByVal text As String) As String 
     ' define the local key and vector byte arrays 
     Dim key() As Byte = Encoding.ASCII.GetBytes("MyKeygoeshere12345678901") 
     Dim iv() As Byte = Encoding.ASCII.GetBytes("Heretheiv") 

     ' instantiate the class with the arrays 
     Dim des As New cTripleDES(key, iv) 

     ' the value of decryptedData should be "test", 
     ' but for our example purposes, let's re-encrypt it 
     Return des.Decrypt(text) 
    End Function 
End Module 

但是,如果我嘗試解密它與以下PHP函數,輸出是agkk,而不是測試作爲我的輸入,因爲我例外。

function decrypt($content){ 
$key = "MyKeygoeshere12345678901"; 
$iv = "Heretheiv"; 
return mcrypt_cbc(MCRYPT_3DES, $key, base64_decode($content), MCRYPT_DECRYPT, $iv); 
} 

有人能告訴我我做錯了什麼嗎?

回答

0

有兩件事情:

  1. 使用MCRYPT_TripleDES CYPHER代替。
  2. 生成有效的IV。

您還應該包括如何生成加密值。

無論如何,這裏是一個你可以使用的實例,只要你認爲合適。

<?php 

$content = "your_secret_string"; 
$secret = "your_secret_key"; 

$iv = mcrypt_create_iv(mcrypt_get_block_size(MCRYPT_TripleDES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM); 

function encrypt($iv, $content, $secret) 
{ 
    return base64_encode(mcrypt_cbc(MCRYPT_TripleDES, $secret, $content, MCRYPT_ENCRYPT, $iv)); 
} 

function decrypt($iv, $content, $secret) 
{ 
    return mcrypt_cbc(MCRYPT_TripleDES, $secret, base64_decode($content), MCRYPT_DECRYPT, $iv); 
} 

$encrypted = encrypt($iv, $content, $secret); 

echo 'Encrypted: '. encrypt($iv, $content, $secret) . '<br>';; 
echo 'Decrypted: '. decrypt($iv, $encrypted, $secret) . '<br>'; 
+0

請記住,從5.5開始已棄用。建議使用mcrypt_generic()代替。 –

+0

這也行不通,現在我得到如下動態狗屎:ZÚêçv:Y或S«qx¢ IV必須與VB應用程序中的相同,不是嗎? – Flole

+0

是的,兩者之間的IV必須相同。我不知道爲什麼你會得到不同的結果。你如何檢索和組裝你的VB生成的字節到PHP腳本? –