[點晴永久免費OA]自定義的加密與解密
using System; using System.Security.Cryptography; using System.IO; using System.Text; class EncryptionAndDissection { //加密: public static string Encrypting(string strSource) { //把字符串放到byte數(shù)組中 byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource); //建立加密對象的密鑰和偏移量 byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定義偏移量 byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定義密鑰 //實例DES加密類 DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider(); mobjCryptoService.Key = iv; mobjCryptoService.IV = key; ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor(); //實例MemoryStream流加密密文件 System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); cs.Write(bytIn, 0, bytIn.Length); cs.FlushFinalBlock(); return System.Convert.ToBase64String(ms.ToArray()); } //解密: public static string Decrypting(string Source) { try { //將解密字符串轉(zhuǎn)換成字節(jié)數(shù)組 byte[] bytIn = System.Convert.FromBase64String(Source); //給出解密的密鑰和偏移量,密鑰和偏移量必須與加密時的密鑰和偏移量相同 byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定義偏移量 byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定義密鑰 DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider(); mobjCryptoService.Key = iv; mobjCryptoService.IV = key; //實例流進行解密 System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length); ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader strd = new StreamReader(cs, Encoding.Default); return strd.ReadToEnd(); } catch (Exception ex) { throw new Exception("在文件解密的時候出現(xiàn)錯誤!錯誤提示: \n" + ex.Message); } } }
該文章在 2020/3/3 1:52:04 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |