在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
在 C# 中处理 PDF 时,安全性和数据操作是重要的关注点。 一种有效的轻量级加密和数据转换技术是按位异或(XOR)操作。 这种技术广泛用于逻辑操作、数据混淆和水印。
IronPDF 是一个强大的 C# 库,用于处理 PDF,允许开发人员将按位逻辑操作符集成到 PDF 工作流程中。 通过利用逻辑异或操作符,我们可以对PDF中的文本、图像和元数据进行转换。
在本指南中,我们将探讨如何工作,它如何与布尔操作数交互,以及如何在使用<IronPDF进行PDF处理时应用它。
XOR(又称为逻辑异或运算符)在代码中由'^'符号表示,是一种用于执行按位异或运算的二进制操作。 那么它与逻辑或运算符有何不同? 虽然这两个运算符名称相似,但在 XOR 运算符名称中使用排他这个词使它们区别开来。 逻辑或运算符更多是一个包含性操作数,更像是一个与/或运算符,当两个操作数中的一个或两个为真时,它将返回真。
另一方面,XOR 的工作方式有些不同。 这个按位运算符对布尔值进行评估,只有在提供的两个操作数之一返回true时才返回true。 如果两个选择返回相同的结果,则返回 false。
为了更简化的概述,让我们来看一个展示XOR如何工作的真值表:
在1
in2
出
1
1
1
1
1
1
而OR的工作方式如下:
在1
in2
出
1
1
1
1
1
例如
byte a = 0b10101010; // 170 in decimal
byte b = 0b11001100; // 204 in decimal
byte result = (byte)(a ^ b); // XOR operation
Console.WriteLine(Convert.ToString(result, 2)); // Output: 01100110
byte a = 0b10101010; // 170 in decimal
byte b = 0b11001100; // 204 in decimal
byte result = (byte)(a ^ b); // XOR operation
Console.WriteLine(Convert.ToString(result, 2)); // Output: 01100110
Dim a As Byte = &B10101010 ' 170 in decimal
Dim b As Byte = &B11001100 ' 204 in decimal
Dim result As Byte = CByte(a Xor b) ' XOR operation
Console.WriteLine(Convert.ToString(result, 2)) ' Output: 01100110
损坏的图像
清除替代文本
在布尔表达式中,XOR 可以应用于布尔操作数:
bool a = true;
bool b = false;
bool result = a ^ b; // Logical XOR operator
Console.WriteLine(result); // Output: True
bool a = true;
bool b = false;
bool result = a ^ b; // Logical XOR operator
Console.WriteLine(result); // Output: True
Dim a As Boolean = True
Dim b As Boolean = False
Dim result As Boolean = a Xor b ' Logical XOR operator
Console.WriteLine(result) ' Output: True
从Pixabay添加上传
或拖放图像到此处
清除替代文本
在这里,我们使用按位运算来比较两个操作数。 右操作数不同于左操作数,这确保了输出为真。 如果第二个操作数与第一个相同,我们会看到false。
按位异或操作的运算符优先级低于算术运算符,但高于按位取反 (~) 和逻辑否定 (!)。
例如
int x = 5 ^ 2 + 3;
Console.WriteLine(x); // Output: 0
int x = 5 ^ 2 + 3;
Console.WriteLine(x); // Output: 0
CONVERTER NOT RUNNING
损坏的图像
添加图片替代文本
+(加法)的优先级高于^(按位异或)。
int x = 5 ^ (2 + 3); // Equivalent to 5 ^ 5
int x = 5 ^ (2 + 3); // Equivalent to 5 ^ 5
Dim x As Integer = 5 Xor (2 + 3) ' Equivalent to 5 ^ 5
5 = 00000101
5 = 00000101
-------------
XOR = 00000000 (Decimal 0)
5 = 00000101
5 = 00000101
-------------
XOR = 00000000 (Decimal 0)
CONVERTER NOT RUNNING
由于 XOR 可以通过相同的操作编码和解码数据,它通常用于轻量级加密。 虽然与AES加密相比,这不是一种强有力的安全措施,但它提供了一种快速混淆PDF内容的方法。
XOR 可用于动态切换基于图像的水印的可见性。 例如,水印可以使用异或(XOR)编码,只有在应用已知密钥时才能查看。 相同的方法可以应用于基于文本的水印和印章。
PDF 元数据通常包含敏感信息,例如文档作者、创建日期和其他标识符。 XOR 可以应用于 元数据 字段,使其在解码之前不可读。
在将文本插入PDF之前应用XOR可以提供一种基本形式的混淆。 在下面的示例中,我们仔细研究了此过程所涉及的代码。
using IronPdf;
using IronSoftware.Drawing;
using System;
using System.Text;
class Program
{
static string XorEncryptDecrypt(string text, char key)
{
StringBuilder output = new StringBuilder();
foreach (char c in text)
{
output.Append((char)(c ^ key));
}
return output.ToString();
}
static void Main()
{
var text = "Confidential Information";
char key = 'X'; // Simple XOR key
string encodedText = XorEncryptDecrypt(text, key);
var pdf = new PdfDocument(270, 270);
pdf.DrawText(encodedText, FontTypes.TimesNewRoman.Name, FontSize: 40, PageIndex: 0, X: 150, Y: 300, Color.Black, Rotation: 0);
pdf.SaveAs("XorEncoded.pdf");
Console.WriteLine("PDF with XOR-encoded text created.");
}
}
using IronPdf;
using IronSoftware.Drawing;
using System;
using System.Text;
class Program
{
static string XorEncryptDecrypt(string text, char key)
{
StringBuilder output = new StringBuilder();
foreach (char c in text)
{
output.Append((char)(c ^ key));
}
return output.ToString();
}
static void Main()
{
var text = "Confidential Information";
char key = 'X'; // Simple XOR key
string encodedText = XorEncryptDecrypt(text, key);
var pdf = new PdfDocument(270, 270);
pdf.DrawText(encodedText, FontTypes.TimesNewRoman.Name, FontSize: 40, PageIndex: 0, X: 150, Y: 300, Color.Black, Rotation: 0);
pdf.SaveAs("XorEncoded.pdf");
Console.WriteLine("PDF with XOR-encoded text created.");
}
}
Imports IronPdf
Imports IronSoftware.Drawing
Imports System
Imports System.Text
Friend Class Program
Private Shared Function XorEncryptDecrypt(ByVal text As String, ByVal key As Char) As String
Dim output As New StringBuilder()
For Each c As Char In text
output.Append(ChrW(AscW(c) Xor AscW(key)))
Next c
Return output.ToString()
End Function
Shared Sub Main()
Dim text = "Confidential Information"
Dim key As Char = "X"c ' Simple XOR key
Dim encodedText As String = XorEncryptDecrypt(text, key)
Dim pdf = New PdfDocument(270, 270)
pdf.DrawText(encodedText, FontTypes.TimesNewRoman.Name, FontSize:= 40, PageIndex:= 0, X:= 150, Y:= 300, Color.Black, Rotation:= 0)
pdf.SaveAs("XorEncoded.pdf")
Console.WriteLine("PDF with XOR-encoded text created.")
End Sub
End Class
输出
从Pixabay添加上传
或拖放图像到此处
清除替代文本
在这里,XOR 函数用于在将文本插入 PDF 之前对其进行混淆。 通过再次使用相同的密钥应用 XOR,相同的函数可以对其进行解密。
XOR也可以应用于图像,在将它们嵌入PDF之前更改像素值,以便仅在解码时才能查看。
using IronPdf;
using IronPdf.Editing;
using System;
using System.Drawing;
using System.Text;
class Program
{
static Bitmap XorImage(Bitmap image, byte key)
{
for (int y = 0; y < image.Height; y++)
{
for (int x = 0; x < image.Width; x++)
{
Color pixel = image.GetPixel(x, y);
Color newPixel = Color.FromArgb(pixel.A, pixel.R ^ key, pixel.G ^ key, pixel.B ^ key);
image.SetPixel(x, y, newPixel);
}
}
return image;
}
static void Main()
{
var pdf = new PdfDocument(270, 270);
Bitmap image = new Bitmap("example_image.png");
Bitmap encodedImage = XorImage(image, 0x55);
encodedImage.Save("XorImage.png");
ImageStamper imageStamp = new ImageStamper("XorImage.png")
{
VerticalAlignment = VerticalAlignment.Middle,
};
pdf.SaveAs("XorImagePDF.pdf");
Console.WriteLine("PDF with XOR-modified image created.");
}
}
using IronPdf;
using IronPdf.Editing;
using System;
using System.Drawing;
using System.Text;
class Program
{
static Bitmap XorImage(Bitmap image, byte key)
{
for (int y = 0; y < image.Height; y++)
{
for (int x = 0; x < image.Width; x++)
{
Color pixel = image.GetPixel(x, y);
Color newPixel = Color.FromArgb(pixel.A, pixel.R ^ key, pixel.G ^ key, pixel.B ^ key);
image.SetPixel(x, y, newPixel);
}
}
return image;
}
static void Main()
{
var pdf = new PdfDocument(270, 270);
Bitmap image = new Bitmap("example_image.png");
Bitmap encodedImage = XorImage(image, 0x55);
encodedImage.Save("XorImage.png");
ImageStamper imageStamp = new ImageStamper("XorImage.png")
{
VerticalAlignment = VerticalAlignment.Middle,
};
pdf.SaveAs("XorImagePDF.pdf");
Console.WriteLine("PDF with XOR-modified image created.");
}
}
Imports IronPdf
Imports IronPdf.Editing
Imports System
Imports System.Drawing
Imports System.Text
Friend Class Program
Private Shared Function XorImage(ByVal image As Bitmap, ByVal key As Byte) As Bitmap
For y As Integer = 0 To image.Height - 1
For x As Integer = 0 To image.Width - 1
Dim pixel As Color = image.GetPixel(x, y)
Dim newPixel As Color = Color.FromArgb(pixel.A, pixel.R Xor key, pixel.G Xor key, pixel.B Xor key)
image.SetPixel(x, y, newPixel)
Next x
Next y
Return image
End Function
Shared Sub Main()
Dim pdf = New PdfDocument(270, 270)
Dim image As New Bitmap("example_image.png")
Dim encodedImage As Bitmap = XorImage(image, &H55)
encodedImage.Save("XorImage.png")
Dim imageStamp As New ImageStamper("XorImage.png") With {.VerticalAlignment = VerticalAlignment.Middle}
pdf.SaveAs("XorImagePDF.pdf")
Console.WriteLine("PDF with XOR-modified image created.")
End Sub
End Class
XOR图像输出
从Pixabay添加上传
或拖放图像到此处
清除替代文本
这种方法使用 XOR 改变像素颜色,确保图像在未用正确密钥解码时显得混乱。
PDF 元数据通常包含可能需要混淆的重要信息。 可以将异或(XOR)应用于元数据字段,使其在没有解密密钥的情况下不可读。
using IronPdf;
using System;
using System.Text;
class Program
{
static string XorEncryptDecrypt(string input, char key)
{
StringBuilder output = new StringBuilder();
foreach (char c in input)
{
output.Append((char)(c ^ key));
}
return output.ToString();
}
static void Main()
{
var pdf = new PdfDocument(270, 270);
pdf.MetaData.Author = XorEncryptDecrypt("John Doe", 'K');
pdf.MetaData.Title = XorEncryptDecrypt("Confidential Report", 'K');
pdf.SaveAs("XorMetadata.pdf");
Console.WriteLine("PDF with XOR-encoded metadata created.");
}
}
using IronPdf;
using System;
using System.Text;
class Program
{
static string XorEncryptDecrypt(string input, char key)
{
StringBuilder output = new StringBuilder();
foreach (char c in input)
{
output.Append((char)(c ^ key));
}
return output.ToString();
}
static void Main()
{
var pdf = new PdfDocument(270, 270);
pdf.MetaData.Author = XorEncryptDecrypt("John Doe", 'K');
pdf.MetaData.Title = XorEncryptDecrypt("Confidential Report", 'K');
pdf.SaveAs("XorMetadata.pdf");
Console.WriteLine("PDF with XOR-encoded metadata created.");
}
}
Imports IronPdf
Imports System
Imports System.Text
Friend Class Program
Private Shared Function XorEncryptDecrypt(ByVal input As String, ByVal key As Char) As String
Dim output As New StringBuilder()
For Each c As Char In input
output.Append(ChrW(AscW(c) Xor AscW(key)))
Next c
Return output.ToString()
End Function
Shared Sub Main()
Dim pdf = New PdfDocument(270, 270)
pdf.MetaData.Author = XorEncryptDecrypt("John Doe", "K"c)
pdf.MetaData.Title = XorEncryptDecrypt("Confidential Report", "K"c)
pdf.SaveAs("XorMetadata.pdf")
Console.WriteLine("PDF with XOR-encoded metadata created.")
End Sub
End Class
输出
从Pixabay添加上传
或拖放图像到此处
清除替代文本
在这里,元数据字段经过 XOR 加密,防止轻易访问敏感信息。
XOR是一种简单而有效的技术,用于PDF中的按位逻辑运算、水印添加和元数据处理。 通过对文本、图像和元数据应用异或转换,开发人员可以创建具有可逆混淆的PDF。 然而,对于更高的安全需求,应使用更强的加密方法。
通过了解按位逻辑运算符、运算符优先级和布尔表达式在C#中的工作原理,开发人员可以在各种实际应用中有效地将XOR与IronPDF结合使用。 还没有IronPDF吗? 试用免费试用版,看看IronPDF如何将您的PDF项目提升到一个新水平!