Exemplo n.º 1
0
        static async Task SelectFileEncrypted(string[] fileNames, IRoleService roleService,
                                              EncryptionAlgorithmType encryptionAlgorithmType,
                                              Func <string, EncryptionAlgorithmType, Task <string> > readfuncEncrypt)
        {
            bool inputIsValid = false;

            while (!inputIsValid)
            {
                Console.WriteLine("Choose the file you want to read");
                for (int i = 0; i < fileNames.Length; i++)
                {
                    Console.WriteLine($"{fileNames[i]} --> {i}");
                }

                if (int.TryParse(Console.ReadLine(), out int fileInput) && fileInput >= fileNames.Length - 1 && fileInput <= fileNames.Length - 1)
                {
                    inputIsValid = true;
                    string fileName = fileNames[fileInput];
                    if (!roleService.IsAuthorizedToReadFile(fileName))
                    {
                        throw new Exception("You are not authorized to view this file");
                    }

                    Console.WriteLine(await readfuncEncrypt(GetFilePath(fileName), encryptionAlgorithmType));
                }
                else
                {
                    inputIsValid = false;
                    Console.WriteLine("The value entered is incorrect");
                }
            }
        }
        /// <summary>
        /// Creates a new CustomerProvidedKey for use in server-side encryption.
        /// </summary>
        /// <param name="key">The encryption key encoded as a base64 string.</param>
        public CustomerProvidedKey(string key)
        {
            EncryptionKey       = key;
            EncryptionAlgorithm = EncryptionAlgorithmType.AES256;
            using var sha256    = SHA256.Create();
            var encodedHash = sha256.ComputeHash(Convert.FromBase64String(key));

            EncryptionKeyHash = Convert.ToBase64String(encodedHash);
        }
Exemplo n.º 3
0
 public Package Pack(byte[] data, EncryptionAlgorithmType encryptionAlgType, PayloadDataType dataType, DateTime timeStamp)
 {
     return(new Package
     {
         DataType = dataType,
         EncryptionAlgorithm = encryptionAlgType,
         Retention = -1,
         TimeStamp = timeStamp,
         Payload = data
     });
 }
Exemplo n.º 4
0
 public string Decrypt(string text, EncryptionAlgorithmType encryptionAlgorithmType)
 {
     return(encryptionAlgorithmType switch
     {
         EncryptionAlgorithmType.RSA => text,     //decrypt rsa
         EncryptionAlgorithmType.SHA_256 => text, //devrypt sha 256
         EncryptionAlgorithmType.SHA_384 => text, //decrypt sha 384
         EncryptionAlgorithmType.SHA_512 => text, //decrypt sha 512
         EncryptionAlgorithmType.SHA_224 => text, //decrypt sha 224
         _ => text,
     });
Exemplo n.º 5
0
        protected RSACryptoServiceProvider CreateAsymmetricAlgorithm(EncryptionAlgorithmType type)
        {
            switch (type)
            {
            case EncryptionAlgorithmType.RSA:
                return(new RSACryptoServiceProvider());

            default:
                return(null);
            }
        }
Exemplo n.º 6
0
        protected SymmetricAlgorithm CreateSymmetricAlgorithm(EncryptionAlgorithmType type)
        {
            switch (type)
            {
            case EncryptionAlgorithmType.Rijndael:
                return(new RijndaelManaged());

            case EncryptionAlgorithmType.Aes:
                return(new AesManaged());

            default:
                return(null);
            }
        }
Exemplo n.º 7
0
        public async Task <string> ReadEncrypt(string filePath, EncryptionAlgorithmType encryptionAlgorithmType)
        {
            string encryptedText = await Read(filePath);

            return(_encryptService.Decrypt(encryptedText, encryptionAlgorithmType));
        }
 public static Package PreaparePackage(byte[] serializedData, EncryptionAlgorithmType encryptionAlgType, PayloadDataType dataType, DateTime timeStamp)
 {
     return(_pm.Pack(serializedData, encryptionAlgType, dataType, timeStamp));
 }
        protected SymmetricAlgorithm CreateSymmetricAlgorithm(EncryptionAlgorithmType type)
        {
            switch (type)
            {
                case EncryptionAlgorithmType.Rijndael:
                    return new RijndaelManaged();

                case EncryptionAlgorithmType.Aes:
                    return new AesManaged();

                default:
                    return null;
            }
        }
        protected RSACryptoServiceProvider CreateAsymmetricAlgorithm(EncryptionAlgorithmType type)
        {
            switch (type)
            {
                case EncryptionAlgorithmType.RSA:
                    return new RSACryptoServiceProvider();

                default:
                    return null;
            }
        }