php加密des
對接方des加密 是用.net 寫的
我用的是php 就大俠把.net的加密程式幫我 轉換成 php的
下面是 .NET 的加密程式:
public class DESEncryptDecrypt { private string Key; private string IV; private DESCryptoServiceProvider IDes; private byte[] GetKey; private byte[] GetIV; public DESEncryptDecrypt() { this.Key = "%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7&*^$^&*%&*%$@#@^"; this.IV = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk"; this.Init(); } public DESEncryptDecrypt(string Key) { this.Key = Key; this.IV = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk"; this.Init(); } public DESEncryptDecrypt(string Key,string IV) { this.Key = Key; this.IV = IV; this.Init(); } private void Init() { this.IDes = new DESCryptoServiceProvider(); this.IDes.GenerateKey(); this.GetKey = this.IDes.Key; this.IDes.GenerateIV(); this.GetIV = this.IDes.IV; } /// /// 獲得密鑰 /// /// 密鑰 private byte[] GetLegalKey() { string sTemp = this.Key; byte[] bytTemp = this.GetKey; int KeyLength = bytTemp.Length; if (sTemp.Length > KeyLength) { sTemp = sTemp.Substring(0, KeyLength); } else { if (sTemp.Length < KeyLength) { sTemp = sTemp.PadRight(KeyLength, ' '); } } return ASCIIEncoding.ASCII.GetBytes(sTemp); } /// /// 獲得初始向量IV /// /// 初試向量IV private byte[] GetLegalIV() { string sTemp = this.Key; byte[] bytTemp = this.GetIV; int IVLength = bytTemp.Length; if (sTemp.Length > IVLength) { sTemp = sTemp.Substring(0, IVLength); } else { if (sTemp.Length < IVLength) { sTemp = sTemp.PadRight(IVLength, ' '); } } return ASCIIEncoding.ASCII.GetBytes(sTemp); } /// /// 密碼編譯演算法 /// /// /// public string Encrypt(string data) { //將要加密的資料編碼為位元組數組 byte[] encrypt_data = System.Text.Encoding.UTF8.GetBytes(data); //如果使用Unicode編碼,支援中文加密 //建立流,儲存加密後的資料 MemoryStream ms = new MemoryStream(); //建立加密流 CryptoStream cs = new CryptoStream(ms, this.IDes.CreateEncryptor(this.GetLegalKey(), this.GetLegalIV()), CryptoStreamMode.Write); //調用Write方法將加密後的結果寫到 流 中 cs.Write(encrypt_data, 0, encrypt_data.Length); //將資料從流寫入記憶體(這句不能少,否則在從記憶體匯出到字串的時候會出現字元丟失) cs.FlushFinalBlock(); //ms.ToString(); ==>將流寫入位元組數組中 string result = Convert.ToBase64String(ms.ToArray()); ms.Close(); cs.Close(); return result; } /// /// 解密演算法 /// /// /// public string Decrypt(string data) { byte[] decrypt_data = Convert.FromBase64String(data); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, this.IDes.CreateDecryptor(this.GetLegalKey(), this.GetLegalIV()), CryptoStreamMode.Write); cs.Write(decrypt_data, 0, decrypt_data.Length); cs.FlushFinalBlock(); string result = System.Text.Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); cs.Close(); return result; } }