Title: Encrypt a Bitmap
Question: Encrypt a Bitmap
Answer:
It is sometimes useful so that nobody modifies your logo or some similar image...
The operation is very simple: to go through the bitmap byte to byte (by means of ScanLine), making a XOR with a byte obtained aleatorily.
This way, if we call once to the function, we will encript the image, if we call it again, we obtain the original image.
Example:
- Put a TImage (Image1) in your form, and load any image.
- Put a TButton (button1) and in its OnClick event, put this code:
procedure TForm1.Button1Click(Sender: TObject);
procedure EncriptaBMP(const BMP:TBitmap;Clave:integer);
var
BytesPorScan : integer;
w,h : integer;
p : pByteArray;
begin
{Hallamos cunto ocupa un Scan en bytes}
try
BytesPorScan:=Abs ( Integer(BMP.ScanLine[1])-
Integer(BMP.ScanLine[0]));
except
raise exception.create('Error');
end;
RandSeed:=Clave;
for h:=0 to BMP.Height-1 do
begin
P:=BMP.ScanLine[h];
for w:=0 to BytesPorScan-1 do
P^[w]:=P^[w] xor Random(256);
end;
end;
begin
EncriptaBMP(Image1.Picture.Bitmap,666);
Image1.Refresh;
end;