Exemple #1
0
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="toEncryptString">待加密字符串</param>
        /// <param name="publicKey">公钥</param>
        /// <returns></returns>
        public static string Encrypt(string toEncryptString, byte[] publicKey)
        {
            var dataToEncrypt = Encoding.UTF8.GetBytes(toEncryptString);
            var data          = Encrypt(dataToEncrypt, publicKey);

            return(FormatConvertor.ToHexString(data));
        }
Exemple #2
0
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="hexCode">加密后数据</param>
        /// <param name="privateKey">私钥</param>
        /// <returns></returns>
        public static string Decrypt(string hexCode, byte[] privateKey)
        {
            byte[] encryptedData = FormatConvertor.FromHexString(hexCode);
            var    data          = Decrypt(encryptedData, privateKey);

            return(Encoding.UTF8.GetString(data));
        }
Exemple #3
0
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="toEncryptString">待加密字符串</param>
        /// <param name="publicKey">公钥</param>
        /// <returns></returns>
        public static string Encrypt(string toEncryptString, string publicKey)
        {
            var dataToEncrypt = Encoding.UTF8.GetBytes(toEncryptString);
            var keyData       = ConvertToBlob(publicKey, false);
            var data          = Encrypt(dataToEncrypt, keyData);

            return(FormatConvertor.ToHexString(data));
        }
Exemple #4
0
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="hexCode">加密后数据</param>
        /// <param name="privateKey">私钥</param>
        /// <returns></returns>
        public static string Decrypt(string hexCode, string privateKey)
        {
            byte[] encryptedData = FormatConvertor.FromHexString(hexCode);
            var    keyData       = ConvertToBlob(privateKey, true);
            var    data          = Decrypt(encryptedData, keyData);

            return(Encoding.UTF8.GetString(data));
        }
Exemple #5
0
 public static string Encrypt(System.IO.Stream inputStream)
 {
     using (var md5 = new MD5CryptoServiceProvider())
     {
         byte[] outputData = md5.ComputeHash(inputStream);
         string result     = FormatConvertor.ToHexString(outputData);
         return(result);
     }
 }
Exemple #6
0
 public static string Encrypt(string text)
 {
     using (var md5 = new MD5CryptoServiceProvider())
     {
         byte[] inputData  = Encoding.UTF8.GetBytes(text);
         byte[] outputData = md5.ComputeHash(inputData);
         string result     = FormatConvertor.ToHexString(outputData);
         return(result);
     }
 }
Exemple #7
0
 private static string SHA(string text, HashAlgorithm sha)
 {
     using (sha)
     {
         //将mystr转换成byte[]
         byte[] dataToHash = Encoding.UTF8.GetBytes(text);
         //Hash运算
         byte[] dataHashed = sha.ComputeHash(dataToHash);
         //将运算结果转换成string
         return(FormatConvertor.ToHexString(dataHashed));
     }
 }
Exemple #8
0
 public static string Decrypt(string toDecrypt, string key = "")
 {
     if (string.IsNullOrEmpty(toDecrypt))
     {
         return(string.Empty);
     }
     byte[] keyArray       = string.IsNullOrEmpty(key) ? defaultKey : Encoding.UTF8.GetBytes(key);
     byte[] toDecryptArray = FormatConvertor.FromHexString(toDecrypt);
     using (RijndaelManaged rDel = new RijndaelManaged())
     {
         using (ICryptoTransform cTransform = rDel.CreateDecryptor(keyArray, defaultIV))
         {
             byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
             return(Encoding.UTF8.GetString(resultArray));
         }
     }
 }
Exemple #9
0
 public static string Decrypt(string sInputString, string sKey = "")
 {
     if (string.IsNullOrEmpty(sInputString))
     {
         return(string.Empty);
     }
     byte[] data = FormatConvertor.FromHexString(sInputString);
     using (DESCryptoServiceProvider DES = new DESCryptoServiceProvider())
     {
         byte[] key_iv = GetKeyBytes(sKey);
         DES.Key = key_iv;
         DES.IV  = key_iv;
         using (ICryptoTransform desencrypt = DES.CreateDecryptor())
         {
             byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
             return(Encoding.UTF8.GetString(result));
         }
     }
 }