我正在尝试创建一个PHP代码,用于加密文本,另一个用VB进行解密。
PHP代码:
<?php
if (isset($_POST['text'])) {
$buffer = $_POST['text'];
$extra = 8 - (strlen($buffer) % 8);
// add the zero padding
if($extra > 0) {
for($i = 0; $i < $extra; $i++) {
$buffer .= "\0";
}
}
$key = "PHPandVB";
$iv = "password";
$fopen = fopen("merda.enc", "w");
$fwrite = fwrite($fopen, mcrypt_cbc(MCRYPT_3DES, $key, $buffer, MCRYPT_ENCRYPT, $iv));
fclose($fopen);
} else {
echo "
<form action='index.php' method='POST'>
<table>
<tr>
<td>Seu texto:</td>
<td><textarea name='text'></textarea></td>
</tr><tr>
<td colspan='2'><input type='submit' value='Enviar!'></td>
</table>
</form>
";
}
?>
VB代码
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class EncryptionAndDecryptionClass
Public Key(15) As Byte
Function Decrypt(ByVal Data() As Byte) As String
Dim MemoryStream As New MemoryStream(Data)
Dim csDecrypt As New CryptoStream(MemoryStream, New TripleDESCryptoServiceProvider().CreateDecryptor(Key, System.Text.Encoding.UTF8.GetBytes("password")), CryptoStreamMode.Read)
Dim fromEncrypt(Data.Length) As Byte
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
Return New UTF8Encoding().GetString(fromEncrypt)
End Function
Function Encrypt(ByVal Data As String) As Byte()
Dim MemoryStream As New MemoryStream
Dim cStream As New CryptoStream(MemoryStream, New TripleDESCryptoServiceProvider().CreateEncryptor(Key, System.Text.Encoding.UTF8.GetBytes("password")), CryptoStreamMode.Write)
Dim toEncrypt As Byte() = New UTF8Encoding().GetBytes(Data)
cStream.Write(toEncrypt, 0, toEncrypt.Length)
cStream.FlushFinalBlock()
Dim ret As Byte() = MemoryStream.ToArray()
cStream.Close()
MemoryStream.Close()
Return ret
End Function
Sub CreateKey(ByVal strKey As String)
Dim arrByte(15) As Byte
Dim AscEncod As New UTF8Encoding
Dim i As Integer = 0
AscEncod.GetBytes(strKey, i, strKey.Length, arrByte, i)
Dim hashSha As New SHA1CryptoServiceProvider
Dim arrHash() As Byte = hashSha.ComputeHash(arrByte)
For i = 0 To 15
Key(i) = arrHash(i)
Next i
End Sub
End Class
我没有一起使用这些代码,可能是另一种中不存在的加密类型,但不知道如何更改。