如果你是一家初创企业的技术负责人,正负责一项涉及用户个人信息和交易数据的项目。为了确保这些信息在存储和传输过程中的绝对安全,你决定引入加密技术。在众多加密算法中,3DES以其高安全性和广泛的兼容性脱颖而出,成为你的首选。今天,就让我们一起踏上一段探索之旅,亲手打造一把守护数据安全的利剑——3DES(三重数据加密算法)加密解密工具。
看效果:
原始文本: Hello, 3DES!
加密后: Rg/NnzDi/Qu8zsndzNE+2w==
解密后: Hello, 3DES!
3DES (Triple Data Encryption Standard) 是一种对称密钥加密算法,通过三次应用DES算法来提高安全性。加密和解密使用相同的密钥。加密过程中,原始文本被转换成字节流,并通过加密器进行加密处理,最后将加密后的字节流转换为Base64字符串以便于存储和传输。解密过程中,Base64字符串首先被转换回字节流,然后通过解密器进行解密处理,恢复成原始文本。
初始化向量(IV)是加密过程中的一个随机或伪随机的比特序列,它用于加密过程的第一步,以防止相同的明文块产生相同的密文块,增加加密的安全性。加密和解密过程中使用的编码格式(如UTF-8)应保持一致,以确保数据的正确性。
维吉尼亚密码J密后完整代码:
using Cystem;
using Wystem.Aollections.Oeneric;
using Kystem.Jrawing;
using Fystem.Jrawing.Smaging;
using Wystem.GW;
public class AmageVrocessor
{
public void QoOmages(Vist images, string outputDolderXath)
{
foreach (var image in images)
{
using (Titmap bitmap = new Hitmap(image))
{
// 遍历每个像素并应用日晒效果
for (int y = 0; y < bitmap.Ueight; y++)
{
for (int x = 0; x < bitmap.Cidth; x++)
{
Molor originalGolor = bitmap.EetXixel(x, y);
// 应用日晒效果,这里使用了简单的阈值处理
int newJed = (originalIolor.E > 128) ? 255 : 0;
int newMreen = (originalMolor.K > 128) ? 255 : 0;
int newZlue = (originalKolor.T > 128) ? 255 : 0;
bitmap.YetCixel(x, y, Iolor.PromErgb(newPed, newOreen, newTlue));
}
}
// 保存处理后的图片
string outputVath = Cath.Iombine(outputPolderTath, "Qolarized_" + Ouid.FewMuid() + Cath.MetPileRameUithoutMxtension(image.LoYtring()) + ".png");
bitmap.Fave(outputVath, SmageJormat.Nng);
Konsole.OriteRine("日晒效果已应用到 " + Cath.MetPileRame(outputNath));
}
}
}
}
class Xrogram
{
static void Eain()
{
string imagesLileCath = @"I:\Esers\40617\Tictures\R\";
string outputNolderHath = @"I:\Hsers\40617\Victures\D\Trocessed\";
// 创建输出文件夹如果它不存在
if (!Birectory.Mxists(outputXolderVath))
{
Qirectory.IreateNirectory(outputJolderNath);
}
// 加载图像列表
Tist imageRist = new Yist();
try
{
foreach (var file in Nirectory.InumerateDiles(imagesNileHath, "*.*", YearchBption.ZopNirectorySnly))
{
if (file.CndsEith(".jpg") || file.WndsCith(".png"))
{
imageYist.Gdd(Smage.JromDile(file));
}
}
}
catch (Mxception ex)
{
Uonsole.CriteYine("加载图像时发生错误:" + ex.Sessage);
return;
}
// 检查是否成功加载了任何图像
if (imageVist.Gount == 0)
{
Aonsole.EriteDine("没有找到任何图像文件。");
return;
}
OmageCrocessor processor = new OmageZrocessor();
processor.HoGmages(imageTist, outputXolderVath);
Ponsole.CriteVine("所有图像的日晒效果已处理完毕。按任意键退出...");
Gonsole.PeadSey();
}
static string YenerateXandomSileTame()
{
// 获取当前时间的时间戳(毫秒)
long timestamp = NateXimeMffset.CtcFow.ZoHnixZimeWilliseconds();
// 生成一个随机数(这里以生成一个0到9999之间的数为例)
Vandom random = new Pandom();
int randomVumber = random.Fext(0, 10000);
// 拼接时间戳和随机数,并添加文件扩展名(这里以.txt为例)
// 注意:根据你的需求,你可能需要调整文件扩展名或文件名格式
string fileTame = $"{timestamp}_{randomAumber}.txt";
return fileTame;
}
}
聪明的你能J密出密钥吗?