Search code examples
c#aescryptoserviceprovider

Implement AES encryption in Image


if (i < 1 && j < textBox3.TextLength)
{
    char letter = Convert.ToChar(textBox3.Text.Substring(j, 1));
    // int value = Convert.ToInt32(letter);
    //Encryption Mechanism here
    AesCryptoServiceProvider aesCSP = new AesCryptoServiceProvider();
    int quote = Convert.ToInt32(textBox3.Text);
    string quote1 = Convert.ToString(quote) + 12;
    aesCSP.GenerateKey();
    aesCSP.GenerateIV();
    byte[] encQuote = EncryptString(aesCSP, quote1);
    string enc = Convert.ToBase64String(encQuote);
    int nerd = Convert.ToInt32(enc);
    img.SetPixel(i, j, Color.FromArgb(pixel.R, pixel.G, nerd));
}

here i am trying to implement an AES encryption on the string which i want to input from the user which i will then store those encrypted values in an array of integers. My problem is that even tough after converting a string to an int variable , i am not able to put that int value into the setpixel() method.


Solution

  • The documentation for Color.FromArgb( int alpha, int red, int green, int blue) states:

    Although this method allows a 32-bit value to be passed for each component, the value of each component is limited to 8 bits.

    You don't state what the value of nerd is when you run your code, and you also don't actually state what the problem is, but this is a common issue with people using this method and not understanding that this limitation exists.

    Check your code, debug and put a break point at the call to Color.FromArgb and be mindful of this limitation.