Exemplo n.º 1
0
        public string GetHMACValue(string encData, string encKey)
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[] keyBuff     = encoding.GetBytes(encKey);
            byte[] hashMessage = null;

            using (var hmacsha256 = new HMACSHA256(keyBuff))
            {
                byte[] dataBytes = encoding.GetBytes(encData);
                hashMessage = hmacsha256.ComputeHash(dataBytes);
            }

            return(HMACCipherCommon.ByteToString(hashMessage));
        }
Exemplo n.º 2
0
 public string DecodeAES256(string cipherText, string cipherKey, string cipherIV, CipherMode cipherMode, PaddingMode paddingMode, DeconvertCipherFormat format)
 {
     byte[] byteKey  = Encoding.UTF8.GetBytes(cipherKey);
     byte[] byteIV   = Encoding.UTF8.GetBytes(cipherIV);
     byte[] byteBuff = HMACCipherCommon.DeConvertCipherText(cipherText, format);
     using (AesCryptoServiceProvider aesCrytoProvider = new AesCryptoServiceProvider())
     {
         try
         {
             aesCrytoProvider.Mode    = cipherMode;
             aesCrytoProvider.Padding = paddingMode;
             var dec = aesCrytoProvider.CreateDecryptor(byteKey, byteIV).TransformFinalBlock(byteBuff, 0, byteBuff.Length);
             return(Encoding.UTF8.GetString(dec));
         }
         catch (Exception e)
         {
             return(string.Empty);
         }
     }
 }
Exemplo n.º 3
0
        public string EncodeAES256(string plainText, string cipherKey, string cipherIV, CipherMode cipherMode, PaddingMode paddingMode)
        {
            byte[] byteKey = Encoding.UTF8.GetBytes(cipherKey);
            byte[] byteIV  = Encoding.UTF8.GetBytes(cipherIV);

            string strEncode = string.Empty;

            byte[] bytePlainText = Encoding.UTF8.GetBytes(plainText);

            using (AesCryptoServiceProvider aesCryptoProvider = new AesCryptoServiceProvider())
            {
                try
                {
                    aesCryptoProvider.Mode    = cipherMode;
                    aesCryptoProvider.Padding = paddingMode;
                    return(HMACCipherCommon.ByteToString(aesCryptoProvider.CreateEncryptor(byteKey, byteIV).TransformFinalBlock(bytePlainText, 0, bytePlainText.Length)));
                }
                catch
                {
                    return(string.Empty);
                }
            }
        }