C# DES加密解密算法
#region DES Class /// <summary> /// ClassName: DES 加密类 /// DES加密、解密类库,字符串加密结果使用BASE64编码返回,支持文件的加密和解密 /// Time:2010-04-30 /// Auther:Hyey.wl /// DES 的摘要说明。 /// </summary> public sealed class DES { /// <summary> /// myiv is iv /// </summary> string myiv = "Hyey20100430"; /// <summary> /// mykey is key /// </summary> string mykey = "HyeyWl30"; /// <summary> /// DES加密偏移量 /// 必须是>=8位长的字符串 /// </summary> public string IV { get { return myiv; } set { myiv = value; } } /// <summary> /// DES加密的私钥 /// 必须是8位长的字符串 /// </summary> public string Key { get { return mykey; } set { mykey = value; } } public DES() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 对字符串进行DES加密 /// Encrypts the specified sourcestring. /// </summary> /// <param name="sourcestring">The sourcestring.待加密的字符串</param> /// <returns>加密后的BASE64编码的字符串</returns> public string Encrypt(string sourceString) { byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Encoding.Default.GetBytes(sourceString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Convert.ToBase64String(ms.ToArray()); } catch { throw; } } } /// <summary> /// Decrypts the specified encrypted string. /// 对DES加密后的字符串进行解密 /// </summary> /// <param name="encryptedString">The encrypted string.待解密的字符串</param> /// <returns>解密后的字符串</returns> public string Decrypt(string encryptedString) { byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Convert.FromBase64String(encryptedString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Encoding.Default.GetString(ms.ToArray()); } catch { throw; } } } /// <summary> /// Encrypts the file. /// 对文件内容进行DES加密 /// </summary> /// <param name="sourceFile">The source file.待加密的文件绝对路径</param> /// <param name="destFile">The dest file.加密后的文件保存的绝对路径</param> public void EncryptFile(string sourceFile, string destFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile); byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btFile = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write)) { try { using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(btFile, 0, btFile.Length); cs.FlushFinalBlock(); } } catch { throw; } finally { fs.Close(); } } } /// <summary> /// Encrypts the file. /// 对文件内容进行DES加密,加密后覆盖掉原来的文件 /// </summary> /// <param name="sourceFile">The source file.待加密的文件的绝对路径</param> public void EncryptFile(string sourceFile) { EncryptFile(sourceFile, sourceFile); } /// <summary> /// Decrypts the file. /// 对文件内容进行DES解密 /// </summary> /// <param name="sourceFile">The source file.待解密的文件绝对路径</param> /// <param name="destFile">The dest file.解密后的文件保存的绝对路径</param> public void DecryptFile(string sourceFile, string destFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile); byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btFile = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write)) { try { using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(btFile, 0, btFile.Length); cs.FlushFinalBlock(); } } catch { throw; } finally { fs.Close(); } } } /// <summary> /// Decrypts the file. /// 对文件内容进行DES解密,加密后覆盖掉原来的文件. /// </summary> /// <param name="sourceFile">The source file.待解密的文件的绝对路径.</param> public void DecryptFile(string sourceFile) { DecryptFile(sourceFile, sourceFile); } } #endregion
源:http://www.cnblogs.com/Leo_wl/archive/2010/04/30/1725168.html
加密算法:3des填充方式:pkcs5工作模式:ECB注意:要使用PHP的mcrypt扩展进行3DES加密以及密钥要以3个8位为一组的不同字符<?phpfunctionpkcs5_pad($text,$blocksize){$pad=…
目录1.urlencode加密2.unicode加密3.Base64加密4.MD55.PBKDF26.SHA7.HMAC8.DES9.3DES10.AES11.RC412.Rabbit13.RSA1.urlencode加密简介:当url地址…
java支持md5盐值加密和des加密。做项目时,某些模块添加加密功能可以提高用户个人信息安全性,防止信息泄露,其中des支持加密解密,MD5目前只支持加密(多用于用户登录密码验证,所以无需解密展示)。一、MD5盐值加密1.在pom文件中导…
packagemainimport("bytes"//"crypto/aes""crypto/cipher""crypto/des""fmt")//cipher包实现了多个标准的用于包装底层块加密算法的加密算法实现。/*DES的操作加解密实…
转自microAllen最近需要使用C#的DES解密工具类解密字符串,但是要解密的字符串是使用java进行DES加密的,去网上查了关于C#和java关于DES加密解密的资料,发现可以相互加密解密的时候,java进行DES加密一般都会写成如下…
#regionMD5算法publicstringmd5(stringstr,intcode){if(code==32)//32位加密{returnSystem.Web.Security.FormsAuthentication.HashPas…
安全总是很重要的,各个语言对于通用的加密算法都会有实现。前段时间,用Go实现了RSA和DES的加密解密,在这分享一下。(对于RSA和DES加密算法本身,请查阅相关资料)在PHP中,很多功能经常是一个函数解决;而Go中的却不是。本文会通过PH…