New file |
| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.IO; |
| | | using System.Security.Cryptography; |
| | | using System.Text; |
| | | using Org.BouncyCastle.Crypto.Parameters; |
| | | using Org.BouncyCastle.Security; |
| | | |
| | | namespace ZigBee.Common |
| | | { |
| | | /// <summary> |
| | | /// 安全类 |
| | | /// </summary> |
| | | public static class SecuritySet |
| | | { |
| | | /// <summary> |
| | | /// Aes加密数据 |
| | | /// 采用128位(16字节)分组加密和解密数据,加密模式为CBC,偏移量(iv)和密钥一致,填充模式采用pkcs5或pkcs7,加密结果输出base64编码。 |
| | | /// </summary> |
| | | /// <returns>The encrypt.</returns> |
| | | /// <param name="bytes">P to encrypt.</param> |
| | | /// <param name="key">Key.</param> |
| | | public static string AesEncrypt(byte []bytes, string key) |
| | | { |
| | | if (bytes==null) |
| | | return null; |
| | | |
| | | //配置AES加密Key(密钥、向量、模式、填充) |
| | | var rm = new RijndaelManaged |
| | | { |
| | | Key = Encoding.UTF8.GetBytes(key), |
| | | IV = Encoding.UTF8.GetBytes(key), |
| | | Mode = CipherMode.CBC, |
| | | Padding = PaddingMode.PKCS7 |
| | | }; |
| | | |
| | | //创建AES加密器对象 |
| | | var cTransform = rm.CreateEncryptor(); |
| | | |
| | | //使用AES将明文流转成密文字节数组 |
| | | var resultArray = cTransform.TransformFinalBlock(bytes, 0, bytes.Length); |
| | | |
| | | //将AES生成的密文字节数组转成Base64字符串 |
| | | return Convert.ToBase64String(resultArray, 0, resultArray.Length); |
| | | } |
| | | |
| | | // <summary> |
| | | /// Aes加密数据 |
| | | /// 采用128位(16字节)分组加密和解密数据,加密模式为CBC,偏移量(iv)和密钥一致,填充模式采用pkcs5或pkcs7,加密结果输出base64编码。 |
| | | /// </summary> |
| | | /// <returns>The encrypt.</returns> |
| | | /// <param name="bytes">P to encrypt.</param> |
| | | /// <param name="key">Key.</param> |
| | | public static byte []AesEncryptBytes(byte[] bytes, string key) |
| | | { |
| | | if (bytes == null) |
| | | return null; |
| | | |
| | | //配置AES加密Key(密钥、向量、模式、填充) |
| | | var rm = new RijndaelManaged |
| | | { |
| | | Key = Encoding.UTF8.GetBytes(key), |
| | | IV = Encoding.UTF8.GetBytes(key), |
| | | Mode = CipherMode.CBC, |
| | | Padding = PaddingMode.PKCS7 |
| | | }; |
| | | |
| | | //创建AES加密器对象 |
| | | var cTransform = rm.CreateEncryptor(); |
| | | |
| | | //使用AES将明文流转成密文字节数组 |
| | | return cTransform.TransformFinalBlock(bytes, 0, bytes.Length); |
| | | |
| | | //将AES生成的密文字节数组转成Base64字符串 |
| | | //return Convert.ToBase64String(resultArray, 0, resultArray.Length); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// Aes 解密数据 |
| | | /// </summary> |
| | | /// <returns>The decrypt.</returns> |
| | | /// <param name="pToDecrypt">P to decrypt.</param> |
| | | /// <param name="key">Key.</param> |
| | | public static byte []AesDecryptBytes(byte[] bytes, string key) |
| | | { |
| | | //AES密文Base64转成字符串 |
| | | var toEncryptArray = bytes;// Convert.FromBase64String(System.Text.Encoding.UTF8.GetString(bytes)); |
| | | |
| | | //配置AES加密Key(密钥、向量、模式、填充) |
| | | var rm = new RijndaelManaged |
| | | { |
| | | Key = Encoding.UTF8.GetBytes(key), |
| | | IV = Encoding.UTF8.GetBytes(key), |
| | | //Mode = CipherMode.ECB, |
| | | Mode = CipherMode.CBC, |
| | | Padding = PaddingMode.PKCS7 |
| | | }; |
| | | |
| | | //创建AES解密器对象 |
| | | var cTransform = rm.CreateDecryptor(); |
| | | |
| | | //使用AES将密文流转成明文的字节数组 |
| | | return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); |
| | | |
| | | //转成字符串 |
| | | //return Encoding.UTF8.GetString(resultArray); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// Aes 解密数据 |
| | | /// </summary> |
| | | /// <returns>The decrypt.</returns> |
| | | /// <param name="pToDecrypt">P to decrypt.</param> |
| | | /// <param name="key">Key.</param> |
| | | public static string AesDecrypt(byte []bytes, string key) |
| | | { |
| | | //AES密文Base64转成字符串 |
| | | var toEncryptArray = Convert.FromBase64String(System.Text.Encoding.UTF8.GetString(bytes)); |
| | | |
| | | //配置AES加密Key(密钥、向量、模式、填充) |
| | | var rm = new RijndaelManaged |
| | | { |
| | | Key = Encoding.UTF8.GetBytes(key), |
| | | IV = Encoding.UTF8.GetBytes(key), |
| | | //Mode = CipherMode.ECB, |
| | | Mode = CipherMode.CBC, |
| | | Padding = PaddingMode.PKCS7 |
| | | }; |
| | | |
| | | //创建AES解密器对象 |
| | | var cTransform = rm.CreateDecryptor(); |
| | | |
| | | //使用AES将密文流转成明文的字节数组 |
| | | var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); |
| | | |
| | | //转成字符串 |
| | | return Encoding.UTF8.GetString(resultArray); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// DES进行加密 |
| | | /// </summary> |
| | | /// <param name="passwordToEncrypt"></param> |
| | | /// <param name="key"></param> |
| | | /// <returns></returns> |
| | | public static string DESEncrypt(string passwordToEncrypt, string key) |
| | | { |
| | | try |
| | | { |
| | | using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) |
| | | { |
| | | byte[] inputByteArray = Encoding.UTF8.GetBytes(passwordToEncrypt); |
| | | des.Key = ASCIIEncoding.ASCII.GetBytes(key); |
| | | des.IV = ASCIIEncoding.ASCII.GetBytes(key); |
| | | |
| | | MemoryStream ms = new MemoryStream(); |
| | | |
| | | using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)) |
| | | { |
| | | cs.Write(inputByteArray, 0, inputByteArray.Length); |
| | | cs.FlushFinalBlock(); |
| | | cs.Close(); |
| | | } |
| | | string str = Convert.ToBase64String(ms.ToArray()); |
| | | ms.Close(); |
| | | return str; |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | return "error:加密失败!"; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// DES进行解密 |
| | | /// </summary> |
| | | /// <param name="pToDecrypt"></param> |
| | | /// <param name="key"></param> |
| | | /// <returns></returns> |
| | | public static string DESDecrypt(string pToDecrypt, string key) |
| | | { |
| | | try |
| | | { |
| | | byte[] inputByteArray = Convert.FromBase64String(pToDecrypt); |
| | | using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) |
| | | { |
| | | des.Key = ASCIIEncoding.ASCII.GetBytes(key); |
| | | des.IV = ASCIIEncoding.ASCII.GetBytes(key); |
| | | |
| | | MemoryStream ms = new MemoryStream(); |
| | | using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) |
| | | { |
| | | cs.Write(inputByteArray, 0, inputByteArray.Length); |
| | | cs.FlushFinalBlock(); |
| | | cs.Close(); |
| | | } |
| | | string str = Encoding.UTF8.GetString(ms.ToArray()); |
| | | ms.Close(); |
| | | return str; |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | return "error:解密失败!"; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | ///获取pem公钥正文内容 |
| | | /// </summary> |
| | | /// <param name="filePath">私钥证书路径</param> |
| | | /// <returns></returns> |
| | | public static string publicKeyContent(string content) |
| | | { |
| | | try |
| | | { |
| | | string publickeyConent = content.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "").Replace("\n", "").Replace("\r", "");//去掉证书的头部和尾部 |
| | | return publickeyConent; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | var msg = $"获取pem公钥正文内容抛出异常,{ex.Message}"; |
| | | System.Console.WriteLine(msg); |
| | | return msg; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 把公钥转换成.net的xml格式 |
| | | /// </summary> |
| | | /// <param name="privateKey">java提供的第三方公钥</param> |
| | | /// <returns></returns> |
| | | public static string ConvertToXmlPublicJavaKey(this RSA rsa, string publicJavaKey) |
| | | { |
| | | RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicJavaKey)); |
| | | string xmlpublicKey = string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>", |
| | | Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()), |
| | | Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned())); |
| | | return xmlpublicKey; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// RSA加密 |
| | | /// </summary> |
| | | /// <param name="sSource" >Source string</param> |
| | | /// <param name="sPublicKey" >public key</param> |
| | | /// <returns></returns> |
| | | public static string RSAEncrypt(string publickey, string content) |
| | | { |
| | | try |
| | | { |
| | | var pk = publicKeyContent(publickey); |
| | | var rsa = new RSACryptoServiceProvider(); |
| | | var publickeyTemp = ConvertToXmlPublicJavaKey(rsa, pk); |
| | | |
| | | byte[] cipherbytes; |
| | | rsa.FromXmlString(publickeyTemp); |
| | | cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); |
| | | return Convert.ToBase64String(cipherbytes); |
| | | } |
| | | catch(Exception ex) |
| | | { |
| | | var msg = $"RSA加密失败_{ex.Message}"; |
| | | System.Console.WriteLine(msg); |
| | | return msg; |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |