以前使用asp,使用vb.net做的控件加密字符,现需要改为php,使用php的3DES解密不成功,加密字符结果发现不一样,而且长度也不相同,在下面的代码加密字符>=4个字符结果为24位,而php中>=8个字符才为24位
有高手可以解决吗?Imports System.Security.Cryptography
Public NotInheritable Class MY3Des
Private TripleDes As New TripleDESCryptoServiceProvider
'---指定密钥的哈希创建指定长度的字节数组
Private Function TruncateHash( _
ByVal key As String, _
ByVal length As Integer) _
As Byte() Dim sha1 As New SHA1CryptoServiceProvider
Dim keyBytes() As Byte = _
System.Text.Encoding.Unicode.GetBytes(key)
Dim hash() As Byte = sha1.ComputeHash(keyBytes) ReDim Preserve hash(length - 1)
Return hash
End Function '添加用来初始化 3DES 加密服务提供程序的构造函数。
'key 参数控制 EncryptData 和 DecryptData 方法。
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
Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)
Dim ms As New System.IO.MemoryStream
Dim encStream As New CryptoStream(ms, _
TripleDes.CreateEncryptor(), _
System.Security.Cryptography.CryptoStreamMode.Write)
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
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
Dim ms As New System.IO.MemoryStream
Dim decStream As New CryptoStream(ms, _
TripleDes.CreateDecryptor(), _
System.Security.Cryptography.CryptoStreamMode.Write)
decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
decStream.FlushFinalBlock()
Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
End Function
End Class
有高手可以解决吗?Imports System.Security.Cryptography
Public NotInheritable Class MY3Des
Private TripleDes As New TripleDESCryptoServiceProvider
'---指定密钥的哈希创建指定长度的字节数组
Private Function TruncateHash( _
ByVal key As String, _
ByVal length As Integer) _
As Byte() Dim sha1 As New SHA1CryptoServiceProvider
Dim keyBytes() As Byte = _
System.Text.Encoding.Unicode.GetBytes(key)
Dim hash() As Byte = sha1.ComputeHash(keyBytes) ReDim Preserve hash(length - 1)
Return hash
End Function '添加用来初始化 3DES 加密服务提供程序的构造函数。
'key 参数控制 EncryptData 和 DecryptData 方法。
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
Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)
Dim ms As New System.IO.MemoryStream
Dim encStream As New CryptoStream(ms, _
TripleDes.CreateEncryptor(), _
System.Security.Cryptography.CryptoStreamMode.Write)
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
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
Dim ms As New System.IO.MemoryStream
Dim decStream As New CryptoStream(ms, _
TripleDes.CreateDecryptor(), _
System.Security.Cryptography.CryptoStreamMode.Write)
decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
decStream.FlushFinalBlock()
Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
End Function
End Class
你至少应给出三组 密钥、明文、密文 供测试用你说你用 php 解密有问题,那也得给出你的代码吧