How to encrypt in VB and PHP?
I am trying to create a PHP code that encrypts a text and another in VB to
decrypt.
PHP Code:
<?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 Code
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
I'm not getting to use the codes together, is probably the type of
encryption that exists in a not exists in another, but do not know how to
change.
No comments:
Post a Comment