Example #1
0
        public static EncryptedMessage Create(object value, string base64PubKey)
        {
            //base64Token is a public key in base64 string format

            try
            {
                EncryptedMessage result = new EncryptedMessage();

                RijndaelManaged rm = new RijndaelManaged();

                var encryptedMessage = RijndaelHelper.AesEncrypt(Newtonsoft.Json.JsonConvert.SerializeObject(value), rm.Key, rm.IV);

                result.Token = CryptoRSAHelper.RsaEncrypt(Convert.ToBase64String(rm.Key), base64PubKey);

                result.IV = Convert.ToBase64String(rm.IV);

                result.Message = encryptedMessage;

                return(result);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Example #2
0
        public T Decrypt <T>(string base64PriKey) where T : class
        {
            try
            {
                var decryptedMessage = CryptoRSAHelper.RsaDecrypt(Message, base64PriKey);

                return(JsonConvert.DeserializeObject <T>(decryptedMessage));
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Example #3
0
        public static RsaMessage Create(object value, string base64PubKey)
        {
            try
            {
                RsaMessage result = new RsaMessage();

                var message = JsonConvert.SerializeObject(value);

                result.Message = CryptoRSAHelper.RsaEncrypt(message, base64PubKey);

                return(result);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Example #4
0
        public T Decrypt <T>(string base64PriKey) where T : class
        {
            try
            {
                byte[] key = Convert.FromBase64String(CryptoRSAHelper.RsaDecrypt(Token, base64PriKey));

                byte[] iv = Convert.FromBase64String(IV);

                var decryptedParameters = RijndaelHelper.AesDecrypt(Message, key, iv);

                return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(decryptedParameters));
            }
            catch (Exception ex)
            {
                return(null);
            }
        }