0
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.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • What is the value of `nerd` when `Color.FromArgb` is called? What is your specific problem? You should add more information to your question. – Matt Hogan-Jones Mar 30 '17 at 09:49
  • The more I look at your code, the less I understand it - you're converting a byte array to a base64 encoded string and then converting it to an int32. Does **any** of this actually work? What are your input values? – Matt Hogan-Jones Mar 30 '17 at 09:53

2 Answers2

0

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.

Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35
0

Edit:

First, define methods for encrypting and decrypting a String. There is a good example here: https://stackoverflow.com/a/27484425/7790901

Those methods will return Base64 strings, that are composed only of ASCII characters, whose values can be cast into numbers from 0 to 127, valid for RGB values (0 to 255).

Then,

To mix the characters of the encrypted String into the color values:

public void MixStringIntoImage(String s, Bitmap img)
{
    int i, x, y;
    i = 0;
    x = 0;
    y = 0;
    while(i<s.Length)
    {
        Color pixelColor = img.GetPixel(x, y);
        pixelColor = Color.FromArgb(pixelColor.R, pixelColor.G, (int)s[i]); //Here you mix the blue component of the color with the character value
        img.SetPixel(x, y, pixelColor);
        i++;
        x++;
        if(x == img.Width)
        { 
            x = 0;
            y++;
            if(y == img.Height) throw new Exception("String is too long for this image!!!");
        }
    }
}

To get the encrypted string from the image:

public String GetStringFromMixedImage(int stringLength, Bitmap img)
{
    String s = "";
    int i, x, y;
    i = 0;
    x = 0;
    y = 0;
    while(i<stringLength)
    {
        s = s + (char)(img.GetPixel(x,y).B); //Get the blue component from the pixel color and then cast to char
        i++;
        x++;
        if(x == img.Width)
        { 
            x = 0;
            y++;
            if(y == img.Height) throw new Exception("String is too long for this image!!!");
        }
    }
    return s;
}
Community
  • 1
  • 1
Alexandre
  • 565
  • 3
  • 9
  • So basically u r trying to cast a string value into an int value and putting that value in the blue component of the pixel. Upto this it looks good and easy. But before setting the pixel color i am playing with that value by performing an aes encryption. The problem is how am i supposed to place a mix of alphanumeric characters into the setpixel method which has the limitation of having Int32 parameters? – Harsh Pandey Mar 30 '17 at 11:27
  • You place the characters in the SetPixel method by casting them into int. The characters in ASCII have values from 0 to 127, so you can use them as values for RGB colors. See ASCII table:http://www.asciitable.com/ I'll edit my answer further – Alexandre Mar 30 '17 at 12:41