Applications

These are just some simple apps I create for personal use and sometimes for others

AES 256 Encryption between PHP and C#

OK so today I will be sharing with you AES 256 Encryption for use between PHP and C#.
This encryption can be Encrypted in either PHP or C# and Decrypted in the other.
 
 
 
PHP Code:

function addpadding($string, $blocksize = 32)
{
    $len = strlen($string);
    $pad = $blocksize - ($len % $blocksize);
    $string .= str_repeat(chr($pad), $pad);
    return $string;
}

function strippadding($string)
{
    $slast = ord(substr($string, -1));
    $slastc = chr($slast);
    $pcheck = substr($string, -$slast);
    if(preg_match("/$slastc{".$slast."}/", $string)){
        $string = substr($string, 0, strlen($string)-$slast);
        return $string;
    } else {
        return false;
    }
}

function encrypt($string = "")
{
	$keyfile = "./AES.keyz";
	$keyfile = file($keyfile);
    $key = base64_decode($keyfile[0]);
    $iv = base64_decode($keyfile[1]);

	return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, addpadding($string), MCRYPT_MODE_CBC, $iv));
}

function decrypt($string = "")
{
	$keyfile = "./AES.keyz";
	$keyfile = file($keyfile);
    $key = base64_decode($keyfile[0]);
    $iv = base64_decode($keyfile[1]);

	$string = base64_decode($string);

	return strippadding(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string, MCRYPT_MODE_CBC, $iv));
}

 
 
 
C# Code:

        private String AES_encrypt(String Input)
        {
            var aes = new RijndaelManaged();
            aes.KeySize = 256;
            aes.BlockSize = 256;
            aes.Padding = PaddingMode.PKCS7;
            aes.Key = Convert.FromBase64String(AES_Key);
            aes.IV = Convert.FromBase64String(AES_IV);

            var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
            byte[] xBuff = null;
            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
                {
                    byte[] xXml = Encoding.UTF8.GetBytes(Input);
                    cs.Write(xXml, 0, xXml.Length);
                }

                xBuff = ms.ToArray();
            }

            String Output = Convert.ToBase64String(xBuff);
            return Output;
        }

        private String AES_decrypt(String Input)
        {
                RijndaelManaged aes = new RijndaelManaged();
                aes.KeySize = 256;
                aes.BlockSize = 256;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
                aes.Key = Convert.FromBase64String(AES_Key);
                aes.IV = Convert.FromBase64String(AES_IV);

                var decrypt = aes.CreateDecryptor();
                byte[] xBuff = null;
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
                    {
                        byte[] xXml = Convert.FromBase64String(Input);
                        cs.Write(xXml, 0, xXml.Length);
                    }

                    xBuff = ms.ToArray();
                }

                String Output = Encoding.UTF8.GetString(xBuff);
                return Output;
        }

 
 
 
For the AES.Keyz file this is it.

    PSVJQRk9QTEpNVU1DWUZCRVFGV1VVT0ZOV1RRU1NaWQ=
    YWlFLVEZZUFNaWlhPQ01ZT0lLWU5HTFJQVFNCRUJZVA=

    32 chr string (32 * 8 = 256 bit)

For this the IV and Key MUST both be a 32 char (256bit) key. I am using a byte[]

Nice and simple to use and has helped me out more than once.
It also supports PKCS7 padding within the PHP.

Example Application
http://dl.dropbox.com/u/11393837/C%20sharp%20AES%20EnDecryption.rar

EDIT: PHP code updated, thanks to PepLamb for noticing.

[VB.Net] BackgroundWorker

Here is some code I have found on the net to send email’s via SMTP through VB.Net. Untested, so if you use please let me know if it works.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

BackgroundWorker Class

Executes an operation on a separate thread.

Sample Code

1.Create event DoWork.

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
'Sample how to disable button send before send email method after that enabled button.
DisableSendButton()
' to do SendEmail method
EnabledSendButton()
End Sub

2. Show how to invoke method when use BackgroundWorker control.

Private Sub DisableSendButton()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(DisableSendButton))
Else
btnSend.Enabled = False
btnCancelSend.Visible = True
End If
End Sub

Private Sub EnabledSendButton()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(EnabledSendButton))
Else
btnSend.Enabled = True
btnCancelSend.Visible = False
End If
End Sub

3.Start BackgroundWorker

BackgroundWorker1.RunWorkerAsync()

[VB.Net] Send Email By SMTP

Here is some code I have found on the net to send email’s via SMTP through VB.Net. Untested, so if you use please let me know if it works.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Imports System.Net.Mail


Public Sub SendEmail()

    Dim client As New SmtpClient()
    Dim sendTo As New MailAddress("sendToAccount@gmail.com")
    Dim from As MailAddress = New MailAddress("from@address.com")
    Dim message As New MailMessage(from,sendTo)

    message.IsBodyHtml = True
    message.Subject = "Test SMTP"
    message.Body = "Got it!!"

    ' Use the same account in app.config to authenticate.
    Dim basicAuthenticationInfo As New System.Net.NetworkCredential("yourAccount@xxx.com", "YourPassword")


    client.Host = "smtp.xxx.com"
    client.UseDefaultCredentials = False
    client.Credentials = basicAuthenticationInfo

    '' For smtp config
    '' google use EnableSsl=True and Port = 587

    client.EnableSsl = False
    client.Port = 25

    Try

        client.Send(message)
        Console.WriteLine("SUCCESS")

    Catch ex As Exception

        Console.WriteLine("SEND FAIL")

    End Try

End Sub