/// <summary> /// 加密字符串,输出BASE64编码字符串 /// </summary> /// <param name="source">要加密的明文字符串</param> /// <param name="key">密钥字符串,长度为8或者24</param> /// <returns>加密的BASE64编码的字符串</returns> public static string Encrypt(string source, string key) { source.CheckNotNull(nameof(source)); key.CheckNotNullOrEmpty(nameof(key)); var keyBytes = Encoding.UTF8.GetBytes(key); var des = new DesHelper(keyBytes); return(des.Encrypt(source)); }
/// <summary> /// 加密要发送的数据,包含签名,DES加密,RSA加密DES密钥等步骤 /// </summary> /// <param name="data">要加密的正文明文数据</param> /// <returns>已加密待发送的密文</returns> public string EncryptData(string data) { data.CheckNotNull(nameof(data)); //获取正文摘要 var signData = RsaHelper.SignData(data, _hashType, _ownPrivateKey); data = new[] { data, signData }.ExpandAndToString(Separator); //使用DES加密 正文+摘要 var des = new DesHelper(); data = des.Encrypt(data); //RSA加密DES密钥 var enDesKey = Convert.ToBase64String(RsaHelper.Encrypt(des.Key, _facePublicKey)); return(new[] { enDesKey, data }.ExpandAndToString(Separator)); }
/// <summary> /// 加密字节数组 /// </summary> /// <param name="source">要加密的字节数组</param> /// <param name="key">密钥字节数组,长度为8或者24</param> /// <returns>加密后的字节数组</returns> public static byte[] Encrypt(byte[] source, byte[] key) { var des = new DesHelper(key); return(des.Encrypt(source)); }