Encryption using Symmetric keys

Scenario:

Encryption using Symmetric keys

Solution:

          Symmetric encryption is fast and for large amounts of data and requires a shared key.
     
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    public sealed class AESEncryption
            {
               public (string key, string IVBase64) SymmetricEncryptionKeyIV()
                {
                    //256 byte
                    var key = GetRandomText(32);
    
                    var cipher = CreateCipher(key);
    
                    var IVBase64 = Convert.ToBase64String(cipher.IV);
    
                    return (key, IVBase64);
                }
    
                private Aes CreateCipher(string keyBase64)
                {
                    var cipher = Aes.Create();
                    cipher.Mode = CipherMode.CBC;
    
                    cipher.Padding = PaddingMode.ISO10126;
    
                    cipher.Key = Convert.FromBase64String(keyBase64);
    
                    return cipher;
                }
    
                private string GetRandomText(int length)
                {
                    var base64String = Convert.ToBase64String(GenerateRandomByte(length));
    
                    return base64String;
                }
    
                private byte[] GenerateRandomByte(int length)
                {
                    var byteArray = new byte[length];
                    RandomNumberGenerator.Fill(byteArray);
    
                    return byteArray;
                }
    
                public string Encrypt (string text, string IV, string key)
                {
                    var cipher = CreateCipher(key);
                    cipher.IV = Convert.FromBase64String(IV);
    
                    var crytoTransform = cipher.CreateEncryptor();
    
                    var plaintext = Encoding.UTF8.GetBytes(text);
    
                    var cyphText = crytoTransform.TransformFinalBlock(plaintext, 0, plaintext.Length);
    
                    return Convert.ToBase64String(cyphText);
                }
    
                public string Decrypt(string encryptedText, string IV, string key)
                {
                    var cipher = CreateCipher(key);
                    cipher.IV = Convert.FromBase64String(IV);
    
                    var crytoTransform = cipher.CreateDecryptor();
    
                    var enc = Convert.FromBase64String(encryptedText);
    
                    var plainBytes = crytoTransform.TransformFinalBlock(enc, 0, enc.Length);
    
                    return Encoding.UTF8.GetString(plainBytes);
                }
            }

          Using 128 byte salt and 256 byte key and iterations of 10000, create a hashed password. If the                  iterations has been updated then use the new one to rehash the password.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
            static void Main(string[] args)
            {
                var data = "User Data";
                var s = new AESEncryption();
    
                var (Key, IVBase64) = s.SymmetricEncryptionKeyIV();
    
                var encryptedData = s.Encrypt(data, IVBase64, Key);
                Console.WriteLine(encryptedData);
    
                var decryptedData = s.Decrypt(encryptedData, IVBase64, Key);
                Console.WriteLine(decryptedData);
    
                Console.ReadLine();
            }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...