Example #1
0
            public static T DecryptObject <T>(byte[] data, byte[] key, byte[] initializationVector, SerializationType serializationType)
            {
                string decryptedText = DecryptData(data, key, initializationVector);

                switch (serializationType)
                {
                case SerializationType.JsonDotNet:
                    return(JsonConvert.DeserializeObject <T>(decryptedText));

                case SerializationType.JsonMicrosoft:
                    return(JsonConvert.DeserializeObject <T>(decryptedText));

                case SerializationType.ProtoBuf:
                    return(ProtobufHelper.Deserialize <T>(Convert.FromBase64String(decryptedText)));

                default:
                    throw new ArgumentOutOfRangeException("serializationType is not understood.");
                }
            }
Example #2
0
            public static byte[] EncryptObject <T>(T data, byte[] key, byte[] initializationVector, SerializationType serializationType)
            {
                string dataString = string.Empty;

                switch (serializationType)
                {
                case SerializationType.JsonDotNet:
                    dataString = JsonConvert.SerializeObject(data);
                    break;

                case SerializationType.JsonMicrosoft:
                    dataString = JsonHelpers.SerializeJson <T>(data);
                    break;

                case SerializationType.ProtoBuf:
                    dataString = Convert.ToBase64String(ProtobufHelper.Serialize <T>(data));
                    break;

                default:
                    throw new ArgumentOutOfRangeException("serializationType is not understood.");
                }

                return(EncryptData(dataString, key, initializationVector));
            }