Example #1
0
        public static string ToHMACString(this string encData, string encKey,
                                          DeconvertCipherFormat deconvertCipherFormat)
        {
            var encoding = new UTF8Encoding();
            var keyBuff  = encoding.GetBytes(encKey);

            byte[] hashMessage = null;

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

            return(hashMessage.jToHMACString());
        }
Example #2
0
        public static byte[] DeConvertCipherText(string cipherText, DeconvertCipherFormat outputFormat)
        {
            byte[] decodeText = null;
            switch (outputFormat)
            {
            case DeconvertCipherFormat.HEX:
                decodeText = HexStringToByte(cipherText);
                break;

            case DeconvertCipherFormat.Base64:
                decodeText = Convert.FromBase64String(cipherText);
                break;

            default:
                throw new Exception("not implement exception.");
            }

            return(decodeText);
        }
Example #3
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);
         }
     }
 }
Example #4
0
        public static string ToDecAES256(this string cipherText, string cipherKey, string cipherIV,
                                         CipherMode cipherMode, PaddingMode paddingMode, DeconvertCipherFormat format)
        {
            var byteKey  = Encoding.UTF8.GetBytes(cipherKey);
            var byteIV   = Encoding.UTF8.GetBytes(cipherIV);
            var byteBuff = cipherText.jToHMACBytes(format);

            using (var 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));
                }
                finally {
                    aesCrytoProvider.Dispose();
                }
            }
        }
 public CryptoHMAC(DeconvertCipherFormat deconvertCipherFormat)
 {
     _deconvertCipherFormat = deconvertCipherFormat;
 }