Encoding.UTF8.GetBytes和Convert.FromBase64String有啥区别

caicaihui 2013-03-14 11:37:19
两个功能都一样吗,都可以将string转换成byte[],分不清到底什么时候用哪个?
...全文
365 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2013-03-14
  • 打赏
  • 举报
回复
引用 楼主 caicaihui 的回复:
两个功能都一样吗,都可以将string转换成byte[],分不清到底什么时候用哪个?
不一样, 第一个,按照编码,获得string的二进制表示 第二个,你需要先了解什么是base64编码, 在不同平台上二进制数据的表示是不一样的。所以在互联网上要满足不同平台的要求,所以要用一种统一的表示方式,就是base64编码。它是一种特殊格式的字符串。表示一段二进制的数据
gxingmin 2013-03-14
  • 打赏
  • 举报
回复
前者是将字符串转成byte数组(即字节)编码为UTF8 后者是将字符串转成Base64的数字
MrCoffee2019 2013-03-14
  • 打赏
  • 举报
回复
给明文加密用:Encoding.UTF8.GetBytes 解密用:Convert.FromBase64String
VS C#2013 DES加密解密完整源代码 测试平台 visual studio 2013 win8.1 依据网上资源整理,并亲测,并写出窗口供调用演示,原代码全部打包奉上,可供学习使用。 public static string EncryptDES(string encryptString, string encryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey,rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch { return encryptString; } } // // DES解密字符串 // // 待解密的字符串 // 解密密钥,要求为8位,和加密密钥相同 // 解密成功返回解密后的字符串,失败返源串 public static string DecryptDES(string decryptString, string decryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey,rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray());
对于一些常用的类进行封装。方便大家使用using System; using System.IO; using System.Text; using System.Security.Cryptography; namespace LQSoft.Common { /// /// 加密 /// public class AES { //默认密钥向量 private static byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F }; public static string Encode(string encryptString, string encryptKey) { encryptKey = Utils.GetSubString(encryptKey, 32, ""); encryptKey = encryptKey.PadRight(32, ' '); RijndaelManaged rijndaelProvider = new RijndaelManaged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32)); rijndaelProvider.IV = Keys; ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor(); byte[] inputData = Encoding.UTF8.GetBytes(encryptString); byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData,0,inputData.Length); return Convert.ToBase64String(encryptedData); } public static string Decode(string decryptString, string decryptKey) { try { decryptKey = Utils.GetSubString(decryptKey, 32, ""); decryptKey = decryptKey.PadRight(32, ' '); RijndaelManaged rijndaelProvider = new RijndaelManaged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey); rijndaelProvider.IV = Keys; ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor(); byte[] inputData = Convert.FromBase64String(decryptString); byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData,0,inputData.Length); return Encoding.UTF8.GetString(decryptedData); } catch { return ""; } } } /// /// 加密 /// public class DES { //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /// /// DES加密字符串 /// /// 待加密的字符串 /// 加密密钥,要求为8位 /// 加密成功返回加密后的字符串,失败返回源串 public static string Encode(string encryptString, string encryptKey) { encryptKey = Utils.GetSubString(encryptKey, 8, ""); encryptKey = encryptKey.PadRight(8, ' '); byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } /// /// DES解密字符串 /// /// 待解密的字符串 /// 解密密钥,要求为8位,和加密密钥相同 /// 解密成功返回解密后的字符串,失败返源串 public static string Decode(string decryptString, string decryptKey) { try { decryptKey = Utils.GetSubString(decryptKey, 8, ""); decryptKey = decryptKey.PadRight(8, ' '); byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return ""; } } } } 这是其中的一段代码
我们总是有太多帐号,常常不得不把密码记到文档。但又想对密码加密,避免别人直接看到密码。这个工具实现对密码的加密和还原。 本程序源码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Security.Cryptography; using System.IO; namespace 密码管理 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private static String RDKey = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; private void button1_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); Random rd = new Random((int)DateTime.Now.Ticks); for (int i = 0; i < 16; i++) sb.Append(RDKey[rd.Next(RDKey.Length)]); OPSW.Text = sb.ToString(); } private void button3_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(Q.Text)) { MessageBox.Show("密保不能为空"); return; } if (string.IsNullOrEmpty(OPSW.Text)) { MessageBox.Show("原始密码不能为空"); return; } PSW1.Text = EncryptDES(OPSW.Text, Q.Text); } private void button2_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(Q.Text)) { MessageBox.Show("密保不能为空"); return; } if (string.IsNullOrEmpty(PSW1.Text)) { MessageBox.Show("加密密码不能为空"); return; } OPSW.Text = DecryptDES(PSW1.Text, Q.Text); } //默认密钥向量 private static byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; public static string EncryptDES(string encryptString, string encryptKey) { try { encryptKey += "--------"; byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));//转换为字节 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();//实例化数据加密标准 MemoryStream mStream = new MemoryStream();//实例化内存流 //将数据流链接到加密转换的流 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch { return encryptString; } } /// /// DES解密字符串 /// /// 待解密的字符串 /// 解密密钥,要求为8位,和加密密钥相同 /// 解密成功返回解密后的字符串,失败返源串 public static string DecryptDES(string decryptString, string decryptKey) { try { decryptKey += "--------"; byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));//转换为字节 byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } } } }

62,052

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧