예제 #1
0
        /// <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("source");
            key.CheckNotNullOrEmpty("key");
            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            var    des      = new DesHelper(keyBytes);

            return(des.Encrypt(source));
        }
예제 #2
0
        /// <summary>
        ///     加密要发送的数据,包含签名,DES加密,RSA加密DES密钥等步骤
        /// </summary>
        /// <param name="data">要加密的正文明文数据</param>
        /// <param name="privateKey">发送方私钥</param>
        /// <param name="publicKey">接收方公钥</param>
        /// <param name="hashType">摘要哈希方式,值必须为MD5或SHA1</param>
        /// <returns>已加密待发送的密文</returns>
        public static string EncryptData(string data, string privateKey, string publicKey, string hashType)
        {
            data.CheckNotNull("data");
            privateKey.CheckNotNullOrEmpty("privateKey");
            publicKey.CheckNotNullOrEmpty("publicKey");
            hashType.CheckNotNullOrEmpty("hashType");
            hashType = hashType.ToUpper();
            hashType.Required(str => hashType == "MD5" || hashType == "SHA1", Resources.Security_RSA_Sign_HashType);
            //获取正文摘要
            string signData = RsaHelper.SignData(data, hashType, privateKey);

            data = new[] { data, signData }.ExpandAndToString(Separator);
            //使用DES加密 正文+摘要
            var des = new DesHelper();

            data = des.Encrypt(data);
            //RSA加密DES密钥
            string enDesKey = Convert.ToBase64String(RsaHelper.Encrypt(des.Key, publicKey));

            return(new[] { enDesKey, data }.ExpandAndToString(Separator));
        }
예제 #3
0
        /// <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));
        }
예제 #4
0
 /// <summary>
 ///     加密要发送的数据,包含签名,DES加密,RSA加密DES密钥等步骤
 /// </summary>
 /// <param name="data">要加密的正文明文数据</param>
 /// <param name="privateKey">发送方私钥</param>
 /// <param name="publicKey">接收方公钥</param>
 /// <param name="hashType">摘要哈希方式,值必须为MD5或SHA1</param>
 /// <returns>已加密待发送的密文</returns>
 public static string EncryptData(string data, string privateKey, string publicKey, string hashType)
 {
     data.CheckNotNull("data");
     privateKey.CheckNotNullOrEmpty("privateKey");
     publicKey.CheckNotNullOrEmpty("publicKey");
     hashType.CheckNotNullOrEmpty("hashType");
     hashType = hashType.ToUpper();
     hashType.Required(str => hashType == "MD5" || hashType == "SHA1", Resources.Security_RSA_Sign_HashType);
     //获取正文摘要
     string signData = RsaHelper.SignData(data, hashType, privateKey);
     data = new[] {data, signData}.ExpandAndToString(Separator);
     //使用DES加密 正文+摘要
     var des = new DesHelper();
     data = des.Encrypt(data);
     //RSA加密DES密钥
     string enDesKey = Convert.ToBase64String(RsaHelper.Encrypt(des.Key, publicKey));
     return new[] {enDesKey, data}.ExpandAndToString(Separator);
 }
예제 #5
0
 /// <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("source");
     key.CheckNotNullOrEmpty("key");
     byte[] keyBytes = Encoding.UTF8.GetBytes(key);
     var des = new DesHelper(keyBytes);
     return des.Encrypt(source);
 }
예제 #6
0
 /// <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);
 }