补位 string str = "100"; str.PadLeft(5,’0′) 输出:00100 str.PadRight(5, '0') 输出:10000 from:https://www.cnblogs.com/daviddong/p/5949794.html
View Details先问个问题:为什么要使用加密算法? 因为数据在网络中传输时面临4个问题: 窃听 假冒 篡改 事后否认 加密技术就是用来解决“窃听”这个问题的。通常分为两大类:对称加密和非对称加密。 对称加密 概念:对称加密就是加密和解密使用同一个密钥,通常称之为“Session Key ”,这种加密技术在当今被广泛采用,如美国政府所采用的DES加密标准就是一种典型的对称加密算法,它的Session Key长度为56bits。 优点:算法公开、计算量小、加密速度快、加密效率高。适用于需要加密大量数据的场景。 缺点:由于加解密使用同一个密钥,密钥传输的过程不安全,且容易被破解,密钥管理也比较麻烦。例如:不适用于浏览器到服务器的通信,因为密钥一旦发送到浏览器端就很容易暴露。 常见算法:AES,DES,3DES,TDEA,Blowfish,RC5,IDEA 非对称加密 概念:非对称加密就是加密和解密所使用的不是同一个密钥,通常有两个密钥,称为“公钥”和“私钥”,它们两个必需配对使用。“公钥”是指可以对外公布的,“私钥”则不能,只能由持有人一个人知道。 优点:由于加解密是用不同的密钥,私钥并不公开,这样就保证了信息在网络传输中的安全性:即使被拦截也无法解密。非常适用于客户端到服务端的数据传输。例如:支付宝的支付请求,数字签名等。 缺点:加密速度慢,比较耗资源。只适用于少量敏感信息的加密。如果加密大量消息则效率会变得低下。另外,如果动态生成公钥和私钥也比较耗资源。 常见算法:RSA,Elgamal,背包算法,Rabin,D-H,ECC 有人可能会问,上面两类加密方式怎么没有MD5?MD5……严格意义上说不是一种加密算法,因为它不能解密。它只是一种密码散列算法,只是为了校验信息用的:保证信息没有篡改。如:你下载软件或游戏时,在下载页面官方都会提供一个“MD5字符串”,你下载完成后可以用md5工具对下载到的软件md5校验,如果得到的md5串和下载页面的一致,就说明软件或游戏没有被篡改过,可放心使用。 下面我们就一块来实战几种常见的加密方式和散列算法: DES 分类:对称加密 DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1977年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),并授权在非密级政府通信中使用,随后该算法在国际上广泛流传开来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace EncryptionPractice.Utils { public class DESUtils { /// <summary> /// 加密数据 /// </summary> /// <param name="text"></param> /// <param name="sessionKey"></param> /// <returns></returns> public static string Encrypt(string text, string sessionKey) { try { var des = GetDESProvider(sessionKey); var bytes = Encoding.Default.GetBytes(text); using var memoryStream = GetMemoryStream(des.CreateEncryptor(), bytes); var result = new StringBuilder(); foreach (var byteItem in memoryStream.ToArray()) { result.AppendFormat("{0:X2}", byteItem); } return result.ToString(); } catch { return string.Empty; } } /// <summary> /// 解密数据 /// </summary> /// <param name="ciphertext"></param> /// <param name="sessionKey"></param> /// <returns></returns> public static string Decrypt(string ciphertext, string sessionKey) { try { var length = ciphertext.Length / 2; var bytes = new byte[length]; for (var i=0; i<length; i++) { var j = Convert.ToInt32(ciphertext.Substring(i * 2, 2), 16); bytes[i] = (byte) j; } var des = GetDESProvider(sessionKey); using var memoryStream = GetMemoryStream(des.CreateDecryptor(), bytes); return Encoding.Default.GetString(memoryStream.ToArray()); } catch { return string.Empty; } } /// <summary> /// 获取DES引擎 /// </summary> /// <param name="sessionKey"></param> /// <returns></returns> private static DESCryptoServiceProvider GetDESProvider(string sessionKey) { var md5Key = MD5Utils.Get32(sessionKey).Substring(8, 8); var des = new DESCryptoServiceProvider { Key = Encoding.ASCII.GetBytes(md5Key), IV = Encoding.ASCII.GetBytes(md5Key) }; return des; } /// <summary> /// 获取内存流 /// </summary> /// <param name="cryptoTransform"></param> /// <param name="bytes"></param> /// <returns></returns> private static MemoryStream GetMemoryStream(ICryptoTransform cryptoTransform, byte[] bytes) { var memoryStream = new MemoryStream(); using var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write); cryptoStream.Write(bytes, 0, bytes.Length); cryptoStream.FlushFinalBlock(); return memoryStream; } } } |
AES 分类:对称加密 高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES(Data Encryption Standard),已经被多方分析且广为全世界所使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace EncryptionPractice.Utils { public class AESUtils { /// <summary> /// 默认Key /// </summary> const string KEY = "E10ADC3949BA59AB"; /// <summary> /// 默认偏移量 /// </summary> const string IV = "BE56E057F20F883E"; /// <summary> /// 加密 /// </summary> /// <param name="text"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <returns></returns> public static string Encrypt(string text, string key = KEY, string iv = IV) { var bytes = Encoding.UTF8.GetBytes(text); var result = Origin(bytes, false, key, iv); return Convert.ToBase64String(result, 0, result.Length); } /// <summary> /// 解密 /// </summary> /// <param name="ciphertext"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <returns></returns> public static string Decrypt(string ciphertext, string key = KEY, string iv = IV) { var bytes = Convert.FromBase64String(ciphertext); var result = Origin(bytes, true, key, iv); if (result.Length < 1) return string.Empty; return Encoding.UTF8.GetString(result); } /// <summary> /// 底层方法 /// </summary> /// <param name="bytes"></param> /// <param name="isDecrypt"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <returns></returns> private static byte[] Origin(byte[] bytes, bool isDecrypt, string key = KEY, string iv = IV) { try { var keyBytes = Encoding.UTF8.GetBytes(key); var ivBytes = Encoding.UTF8.GetBytes(iv); var rijndaelManaged = new RijndaelManaged { BlockSize = 128, KeySize = 256, FeedbackSize = 128, Padding = PaddingMode.PKCS7, Key = keyBytes, IV = ivBytes, Mode = CipherMode.CBC }; var cryptoTransform = isDecrypt ? rijndaelManaged.CreateDecryptor() : rijndaelManaged.CreateEncryptor(); var result = cryptoTransform.TransformFinalBlock(bytes, 0, bytes.Length); return result; } catch { return new byte[] { }; } } } } |
RSA 分类:非对称加密 RSA是1977年由罗纳德·李维斯特(Ron Rivest)、阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的。RSA就是他们三人姓氏开头字母拼在一起组成的。RSA公开密钥密码体制是一种使用不同的加密密钥与解密密钥,“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace EncryptionPractice.Utils { public class RSAUtils { /// <summary> /// 加密 /// </summary> /// <param name="text">明文</param> /// <param name="publicKey">公钥</param> /// <returns>密文</returns> public static string Encrypt(string text, string publicKey) { var blockSize = 256; // 加密块大小,越大越慢 var bytes = Encoding.UTF8.GetBytes(text); var length = bytes.Length; // rsa实例 var rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(publicKey); var offSet = 0; // 游标 var i = 0; byte[] cache; var ms = new MemoryStream(); while (length - offSet > 0) { var len = length - offSet > blockSize ? blockSize : length - offSet; var temp = new byte[len]; Array.Copy(bytes, offSet, temp, 0, len); cache = rsa.Encrypt(bytes, false); ms.Write(cache, 0, cache.Length); i++; offSet = i * blockSize; } var cipherBytes = ms.ToArray(); return Convert.ToBase64String(cipherBytes); } /// <summary> /// RSA解密 /// </summary> /// <param name="ciphertext">密文</param> /// <param name="privateKey">私钥</param> /// <returns>明文</returns> public static string Decrypt(string ciphertext, string privateKey) { var blockSize = 256; var bytes = Convert.FromBase64String(ciphertext); var length = bytes.Length; // rsa实例 var rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(privateKey); var offSet = 0; // 游标 var i = 0; byte[] cache; var ms = new MemoryStream(); while (length - offSet > 0) { var len = length - offSet > blockSize ? blockSize : length - offSet; var temp = new byte[len]; Array.Copy(bytes, offSet, temp, 0, len); cache = rsa.Decrypt(temp, false); ms.Write(cache, 0, cache.Length); i++; offSet = i * blockSize; } var cipherBytes = ms.ToArray(); return Encoding.UTF8.GetString(cipherBytes); } } } |
生成公钥/密钥的方法(只适用于C#,其他编程语言使用需要转换)
1 2 3 |
var rsa = new RSACryptoServiceProvider(); var publicKey = rsa.ToXmlString(false); var privateKey = rsa.ToXmlString(true); |
MD5 分类:散列算法 MD5信息摘要算法(MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致。MD5由美国密码学家罗纳德·李维斯特(Ronald Linn Rivest)设计,于1992年公开。1996年后该算法被证实存在弱点,可以被加以破解,对于需要高度安全性的数据,专家一般建议改用其他算法,如SHA-2。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
using System; using System.Security.Cryptography; using System.Text; namespace EncryptionPractice.Utils { public class MD5Utils { /// <summary> /// 获取16位散列值 /// </summary> /// <param name="text"></param> /// <returns></returns> public static string Get16(string text) { return Get32(text).Substring(7, 16); } /// <summary> /// 获取32位散列值 /// </summary> /// <param name="text"></param> /// <returns></returns> public static string Get32(string text) { using var md5 = MD5.Create(); var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(text)); var result = BitConverter.ToString(hash); return result.Replace("-", "").ToUpper(); } /// <summary> /// 获取16位散列值 v2 /// </summary> /// <param name="text"></param> /// <returns></returns> public static string Get16v2(string text) { return Get32v2(text).Substring(7, 16); } /// <summary> /// 获取32位散列值 /// </summary> /// <param name="text">字符串</param> /// <returns></returns> public static string Get32v2(string text) { var md5 = new MD5CryptoServiceProvider(); var hash = md5.ComputeHash(Encoding.Default.GetBytes(text)); var resutl = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { resutl.Append(hash[i].ToString("x2")); } return resutl.ToString().ToUpper(); } } } |
SHA-2 分类:散列算法 SHA-2,名称来自于安全散列算法2(英语:Secure Hash Algorithm 2)的缩写,一种密码散列函数算法标准,由美国国家安全局研发,由美国国家标准与技术研究院(NIST)在2001年发布。属于SHA算法之一,是SHA-1的后继者。其下又可再分为六个不同的算法标准,包括了:SHA-224、SHA-256、SHA-384、SHA-512、SHA-512/224、SHA-512/256。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System.Security.Cryptography; using System.Text; namespace EncryptionPractice.Utils { public class SHAUtils { /// <summary> /// SHA256加密 /// </summary> /// <param name="data"></param> /// <returns></returns> public static string Encrypt(string data) { var bytes = Encoding.UTF8.GetBytes(data); var hash = SHA256.Create().ComputeHash(bytes); var result = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { result.Append(hash[i].ToString("x2")); } return result.ToString(); } } } |
欢迎转载,请注明出处:龙生时代
View Details问题: 之前在VS2012、VS2015中,可直接在应用程序中Console.WriteLine输出信息来调试bug。最近换到VS2017后,却发现怎么也无法输出 解决: 这个问题的原因是VS2017开始已经不再使用Hosting Process模式,在Hosting Process时可以将信息输出至 Output窗口。 在VS2017及以后的版本可以使用Trace.WriteLine来将调试信息输出至Output窗口。 另外VS2017对CPU的要求也高了,我的老机器一打开没多久就出现很多ServiceHub.Host.CLR.x86.exe的进程,导致写代码卡顿,不得已换了新机器,升级到i5后没再出现卡顿现象了。一起典型的软件倒逼硬件更新的例子,诸君勿笑. from:https://blog.csdn.net/edcvf3/article/details/104827175
View Details//参数直接写参数名 string Sql = "select * from [table] where field like @field"; //参数值里面加上通配符,varchar类型查询也不加单引号 var Param = new {field = "%{ParamValue}%"}; //执行查询 Dapper.Query<T>(Sql,Param); from:https://www.cnblogs.com/tian2008/p/8491633.html
View Details转成 Base64 形式的 System.String:
1 2 3 4 5 |
string a = "base64字符串与普通字符串互转"; byte[] b = System.Text.Encoding.Default.GetBytes(a); //转成 Base64 形式的 System.String a = Convert.ToBase64String(b); Response.Write(a); |
转回到原来的 System.String:
1 2 3 |
byte[] c = Convert.FromBase64String(a); a = System.Text.Encoding.Default.GetString(c); Response.Write(a);<br> |
from:https://www.cnblogs.com/daimaxuejia/p/12893207.html
View Details
1 2 3 4 5 6 |
public static bool IsPropertyExist(dynamic data, string propertyname) { if (data is ExpandoObject) return ((IDictionary<string, object>)data).ContainsKey(propertyname); return data.GetType().GetProperty(propertyname) != null; } |
https://www.cnblogs.com/94cool/p/8135579.html
View Details客户端验证对用户向表单中输入的数据给出一个即时反馈。 服务器验证,主要是因为来自网络的信息都是不能信任的。 一、 为验证注解订单 1 、验证注解的使用,自定义错误提示消息 数据注解特性定义在 System.ComponentModel.DataAnnotations 中(但有一个特性不在这个命名空间),它提供了服务器端验证,当模型属性上使用这些特性时,框架也支持客户端验证。在命名空间DataAnnotations中,有4个特性可以用来应对一般验证场合。 (1)、字符非空,最大长度,值范围,两个属性相同,正则表达式 //字段非空,最大长度160 [Required(ErrorMessage="FirstName不能为空")] [StringLength(160, ErrorMessage = "FirstName太长了")] public string FirstName { get; set; } //正则表达式验证必须是电子邮件 [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage="邮件地址不正确")] public string Email { get; set; } //字段非空,数值类型最小值,最大值 [Required] [Range(typeof(decimal), "0.00", "49.99")] public decimal Total { get; set; } //对象两个属性拥有相同的值 [Compare("Email")] public string EmailConfirm { get; set; } […]
View Details未启用卷共享。 启用“Docker CE for Windows”设置中的卷共享(仅 Linux 容器) 若要解决此问题,请执行以下操作: 右键单击通知区域中的“Docker for Windows” ,并选择“设置” 。 选择“共享驱动器” ,并共享系统驱动器和项目所在的驱动器。 备注 如果文件显示“已共享”,可能仍需要单击对话框底部的“重置凭据…”链接,以便重新启用卷共享。 若要在重置凭据后继续,可能必须重启 Visual Studio。 提示 如果未配置共享驱动器,Visual Studio 2017 版本 15.6 之后的版本会发出提示 。 容器类型 向项目添加 Docker 支持后,请选择 Windows 或 Linux 容器。 Docker 主机必须运行类型相同的容器。 要更改正在运行的 Docker 实例中的容器类型,请右键单击系统托盘中的 Docker 图标,再选择“切换到 Windows 容器…”或“切换到 Linux 容器…” 。 无法开始调试 其中一个原因可能与在用户配置文件的文件夹中有过时调试组件有关。 请执行以下命令来删除这些文件夹,以便在下次调试会话上下载最新调试组件。 del %userprofile%\vsdbg del %userprofile%\onecoremsvsmon 调试应用程序时特定于网络的错误 尝试执行可从清理容器主机网络下载的脚本,此操作会刷新主机上的网络相关组件。 装载被拒绝 使用 Docker for macOS 时,可能会遇到引用文件夹 /usr/local/share/dotnet/sdk/NuGetFallbackFolder 错误。 将文件夹添加到 Docker 中的“文件共享”选项卡 Docker 用户组 使用容器时,可能会在 Visual Studio 中遇到以下错误: 复制
1 2 3 |
The current user must be in the 'docker-users' group to use Docker Desktop. Add yourself to the 'docker-users' group and then log out of Windows. |
必须是“docker-users”组的成员,才有使用 Docker 容器的权限。 若要将自己添加到 Windows 10 中的组,请执行以下步骤: 从“开始”菜单中,打开“计算机管理” 。 展开“本地用户和组”,并选择“组” 。 找到“docker-users”组,右键单击并选择“添加到组” 。 添加用户帐户或帐户。 注销后再次登录,以使更改生效。 还可以在管理员命令提示符下使用 net localgroup 命令向特定组添加用户。 cmd复制
1 2 |
<span class="hljs-built_in">net</span> localgroup docker-users DOMAIN\username /add |
[…]
View Details【问题】网站 项目里经常用到DateTime类型变量,要求所有日期时间类型格式标准为“yyyy-MM-dd HH:mm:ss”,但默认Tostring格式为 yyyy-M-d H:m:s ,没有前置0, 需要修改 【分析】 1、修改代码里所有DateTime类型的Tostring为 ToString("yyyy-MM-dd HH:mm:ss"),不方便 2、找见统一的修改地方, Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN", true) { DateTimeFormat = { ShortDatePattern = "yyyy-MM-dd", FullDateTimePattern = "yyyy-MM-dd HH:mm:ss" }; 发现不起作用 经分析DateTimeFormat 类的属性,发现 LongTimePattern 起作用 【结论】 增加 LongTimePattern 属性
1 2 3 |
Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN", true) { DateTimeFormat = { ShortDatePattern = "yyyy-MM-dd", FullDateTimePattern = "yyyy-MM-dd HH:mm:ss", LongTimePattern ="HH:mm:ss"} }; |
from:https://blog.csdn.net/onemetre/article/details/52042907
View Details