<?php if(!isset($_POST['op'])) { ?> <form id="form1" name="form1" method="post" action=""> enter text <input name="data" type="text" /> <input type="hidden" value="op" name="op" /> <input type="submit" name="Submit" value="Submit" /> </form> <?php }else { $buffer = $_POST['data']; // get the amount of bytes to pad $extra = 8 - (strlen($buffer) % 8); // add the zero padding if($extra > 0) { for($i = 0; $i < $extra; $i++) { $buffer .= "\0"; } } // very simple ASCII key and IV $key = "passwordDR0wSS@P6660juht"; $iv = "password"; // hex encode the return value echo "Result: ".bin2hex(mcrypt_cbc(MCRYPT_3DES, $key, $buffer, MCRYPT_ENCRYPT, $iv)); } ?>
using System; using System.Security.Cryptography; using System.Text; using System.IO; namespace TestBed { class Program { static void Main(string[] args) { byte[] key = Encoding.ASCII.GetBytes("passwordDR0wSS@P6660juht"); byte[] iv = Encoding.ASCII.GetBytes("password"); byte[] data = Encoding.ASCII.GetBytes("Test"); byte[] enc = new byte[0]; TripleDES tdes = TripleDES.Create(); tdes.IV = iv; tdes.Key = key; tdes.Mode = CipherMode.CBC; tdes.Padding = PaddingMode.Zeros; ICryptoTransform ict = tdes.CreateEncryptor(); enc = ict.TransformFinalBlock(data, 0, data.Length); Console.WriteLine(Bin2Hex(enc)); Console.ReadLine(); } // my bin2hex implementation static string Bin2Hex(byte[] bin) { StringBuilder sb = new StringBuilder(bin.Length * 2); foreach(byte b in bin) { sb.Append(b.ToString("x").PadLeft(2, '0')); } return sb.ToString(); } } }
另一种方法:
PHP代码:
function encrypt($string) { //Key $key = "xxxxxxxx"; //Encryption $cipher_alg = MCRYPT_TRIPLEDES; $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); $encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv); return base64_encode($encrypted_string); return $encrypted_string; } function decrypt($string) { $string = base64_decode($string); //key $key = "xxxxxxxx"; $cipher_alg = MCRYPT_TRIPLEDES; $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); $decrypted_string = mcrypt_decrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv); return trim($decrypted_string); }
C#代码:
using System; using System.Security.Cryptography; using System.Text; public class Crypto3DES { public Crypto3DES() { } private System.Text.Encoding encoding; public string Key { get { return "xxxxxxxx"; } } public System.Text.Encoding Encoding { get { if( encoding == null ) { encoding = System.Text.Encoding.UTF8; } return encoding; } set { encoding = value; } } public string Encrypt3DES( string strString ) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = Encoding.GetBytes( this.Key ); DES.Mode = CipherMode.ECB; DES.Padding = PaddingMode.Zeros; ICryptoTransform DESEncrypt = DES.CreateEncryptor(); byte[] Buffer = encoding.GetBytes(strString); return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); } public string Decrypt3DES( string strString ) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider (); DES.Key = Encoding.UTF8.GetBytes( this.Key ); DES.Mode = CipherMode.ECB; DES.Padding = PaddingMode.Zeros; ICryptoTransform DESDecrypt = DES.CreateDecryptor(); byte[] Buffer = Convert.FromBase64String(strString); return UTF8Encoding.UTF8.GetString( DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length) ); } }
非常 感谢