1
   class AES
    {
    public static byte[] rndKeyIv()
    {
    RNGCryptoServiceProvider rnd = new RNGCryptoServiceProvider();
    var key = new byte[32];
    var iv = new byte[16];
    rnd.GetBytes(key);
    rnd.GetBytes(iv);

    return key;
    return iv;
    }
    public static string Encrypt256(string decrypt)
    {
        

        //der Text aus der Box wird in Bytes umgewandelt
        byte[] encbytes = ASCIIEncoding.ASCII.GetBytes(decrypt);
        //die Blocksize von AES beträgt 128bits und die Keysize beträgt 256bits
        AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
        aes.BlockSize = 128;
        aes.KeySize = 256;
        //der Schlüssel von oben wird wieder mit ASCII Encoding in Bytes umgewandelt, das 
 selbe wird mit dem Initialisierungs Vektor gemacht
        aes.Key = aeskey;
        aes.IV = iv;
        aes.Padding = PaddingMode.PKCS7;
        aes.Mode = CipherMode.CBC;

        ICryptoTransform icrypt = aes.CreateEncryptor(aes.Key, aes.IV);

        byte[] enc = icrypt.TransformFinalBlock(encbytes, 0, encbytes.Length);
        icrypt.Dispose();
        //das resultat der verschlüsselung wird in Base64String umgewandelt
        return Convert.ToBase64String(enc);
       
        }

How exactly can I use the rndKeyIv method to use those keys inside the encrypt and decrypt method? Im kind of confused.

If I would use that method in the encryption and decryption method there would be 2 different keys and ivs

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
marz
  • 11
  • 1
  • 1
    Does this answer your question? [Best way to store encryption keys in .NET C#](https://stackoverflow.com/questions/4967325/best-way-to-store-encryption-keys-in-net-c-sharp) – Sinatr Jun 29 '21 at 09:33
  • You would need to share the secret key somehow. That can be done using hybrid cryptography, i.e. RSA encryption or e.g. DH key agreement. Beware that doesn't protect against man-in-the-middle unless some keys can be trusted. If you think this is all a bit much: use TLS (assuming that you require transport security). – Maarten Bodewes Jun 29 '21 at 10:19
  • Every encryption algorithm can be broken. The question is how long would it take to break the code. IT would depend on the algorithm and the speed of the computer used to break to decrypt the code. If it take 100 years to break than it is not an issue. That is why it is important to use latest encryption algorithms with long keys so it take a long time to randomly decrypt. – jdweng Jun 29 '21 at 11:07

0 Answers0