C# 在图像中实现AES加密 if(i

C# 在图像中实现AES加密 if(i,c#,aescryptoserviceprovider,C#,Aescryptoserviceprovider,在这里,我尝试在我想要从用户输入的字符串上实现AES加密,然后将这些加密值存储在整数数组中。我的问题是,即使在将字符串转换为int变量后,我也无法将该int值放入setpixel()方法。for颜色。FromArgb(int alpha,int red,int green,int blue)表示: 尽管此方法允许为每个对象传递32位值 组件,每个组件的值限制为8位 当您运行代码时,您没有说明nerd的值是什么,也没有说明问题是什么,但这是使用此方法但不了解此限制存在的人的常见问题 检查代码,调试

在这里,我尝试在我想要从用户输入的字符串上实现AES加密,然后将这些加密值存储在整数数组中。我的问题是,即使在将字符串转换为int变量后,我也无法将该int值放入setpixel()方法。

for
颜色。FromArgb(int alpha,int red,int green,int blue)
表示:

尽管此方法允许为每个对象传递32位值 组件,每个组件的值限制为8位

当您运行代码时,您没有说明
nerd
的值是什么,也没有说明问题是什么,但这是使用此方法但不了解此限制存在的人的常见问题

检查代码,调试并在调用
Color.FromArgb
时设置断点,并注意此限制。

编辑:

首先,定义加密和解密字符串的方法。这里有一个很好的例子:

这些方法将返回仅由ASCII字符组成的Base64字符串,其值可以转换为0到127之间的数字,对RGB值(0到255)有效

那么

要将加密字符串的字符混合到颜色值中,请执行以下操作:

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));
}
public void MixStringIntoImage(字符串s,位图img)
{
int i,x,y;
i=0;
x=0;
y=0;

虽然(I调用
Color.FromArgb
时,
nerd
的值是多少?您的具体问题是什么?您应该为您的问题添加更多信息。我看代码越多,对代码的理解就越少-您正在将字节数组转换为base64编码的字符串,然后将其转换为int32。这其中有任何一项实际有效吗?W你的输入值是什么?因此,基本上你正在尝试将字符串值转换为int值,并将该值放入像素的蓝色部分。到目前为止,这看起来很简单。但在设置像素颜色之前,我正在通过执行aes加密来处理该值。问题是我应该如何放置字母数字字符的混合进入setpixel方法,该方法具有Int32参数的限制?您可以通过将字符转换为int来将字符放入setpixel方法。ASCII中的字符具有0到127的值,因此您可以将其用作RGB颜色的值。请参阅ASCII表:我将进一步编辑我的答案
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!!!");
        }
    }
}
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;
}