65.9K
CodeProject 正在变化。 阅读更多。
Home

锁定

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.71/5 (8投票s)

2007 年 11 月 27 日

CPOL

1分钟阅读

viewsIcon

46899

downloadIcon

573

LOCK 用于使用对称密钥加密来锁定和解锁文件。


引言

本文展示了一种经典的文件锁定方法,以防止其在任何软件中显示原始内容。为了锁定文件,需要密码。要再次以原始形式查看文件,必须使用用于锁定文件的相同密码使用 LOCK 软件对其进行解锁。

背景

使用的基本方法是对称密钥密码学。文件被转换为 8 字节的块,并与通过密码获得的类似 8 字节的块重复进行异或运算。由于使用的加密是对称密钥密码学,因此相同的过程也用于解锁/解密。

使用代码

以下是主要加密例程的代码。有关更多详细信息,请查看源代码中的注释。

// Replicate the byte array in the key buffer to create
         // an encryption key whose size equals or exceeds the
         // size of the I/O buffer
         int count = (1024 + keyBytes.Length-1)/keyBytes.Length;
                 for( int i=0 ; i<count ; i++)
                 Array.Copy( keyBytes , 0 , keybuf , i*keyBytes.Length , keyBytes.Length);
        // Read the file in bufsize blocks, XOR-encrypt each block,
         // and write the encrypted block back to the file
         long lBytesRemaining = stream.Length;
         while (lBytesRemaining>0)
         {
                 long lPosition = stream.Position ;
                 int nBytesRequested = (int)System.Math.Min(bufSize , lBytesRemaining); 
                 int nBytesRead = reader.Read (buffer, 0, nBytesRequested)

                 for (int i=0; i<nBytesRead; i++)
                          buffer[i] ^= keybuf[i];Stream.Seek (lPosition, SeekOrigin.Begin);
                          writer.Write (buffer, 0, nBytesRead);
                          lBytesRemaining -= nBytesRead;
            }                   

屏幕截图

Screenshot - mainScreenShot.gif

关注点

这是我在 CodeProject 上的第二篇文章。关于处理大数字的第一篇文章,点击这里。我没有太多的文章写作经验。我只是试图尽量减少我在撰写第一篇文章时遇到的不足。欢迎提出关于清晰度、混淆、代码、技术写作的建议。也欢迎评论。您也可以通过 funatlearn@ontheedge.co.in 发送邮件给我,主题为 Lock Code Project Article,以了解相关信息。

历史

这是程序的第一个版本。我有一些关于代码下一个版本的输入。我期待来自代码读者的更多输入。在最终确定所有输入后,我将编写代码的下一个版本。

如果您觉得此代码/文章有任何不足之处,请留下消息,以便这些缺陷不会出现在后续版本中。

© . All rights reserved.