php-關於DES加密 求協助!

來源:互聯網
上載者:User
關鍵字 php 加密 des
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;        }    }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.