C# encryption gadget code implementation

黄舟
Release: 2017-02-25 11:09:15
Original
1328 people have browsed it


关键点有以下几个:

1、openFileDialog控件的使用
如果通过单击选择按钮来实现选择本地某个文件的功能呢?
具体来说分为以下几步:

a、新建Winform窗体,画好相应的控件。

b、在工具箱中找到openFileDialog,拖到“选择文件”按钮上,此时会出现如下这种效果:

此时openFileDialog会显示在界面的下方,而不是界面上,这样就可以了,通过单击“选择文件”按钮即可实现打开本地某个文件的功能。

同时“选择文件”按钮的Click函数中需要添加下面几行代码:

DialogResult diaResult = this.openFile.ShowDialog();
if (diaResult == DialogResult.OK)
{        
//内部可以获取文件名之类的信息
}
Copy after login

那么如果获取打开路径下的文件名、路径信息呢?

可以通过这几个属性来实现:

a、openFileDialog控件的FileName属性来获取路径信息,此时的路径信息包含文件名及文件拓展名。

比如:E:\Work\Vs\TestEncryption\加密处理\待加密\死亡海岸线.txt

b、如果只想获取“死亡海岸线”这个文件名呢?

Path.GetFileNameWithoutExtension(pathName)即可以实现这个功能,而且此时不带文件拓展名奥,传入参数pathName就是a中的路径信息。

那么如何在选择相应文件后,获取文件大小及拓展名呢?如下代码即可实现:

FileInfo fi = new FileInfo(pathName);
filetype = fi.Extension;//文件拓展名
filesize = fi.Length;//文件信息的字节数
Copy after login

openFileDialog控件设置初始打开路径有三种方式:
1、下面两行代码的效果是一样的(这是两种):

openFileSave.InitialDirectory = "E:\\Work\\Vs\\TestEncryption\\加密处理\\保存文件";
openFileSave.InitialDirectory = @"E:\\Work\\Vs\\TestEncryption\\加密处理\\保存文件";
Copy after login

还有一种方式是通过openFileDialog控件右击属性来实现的。

其中有一个属性是:InitialDirectory,在其后面填写:E:\Work\Vs\TestEncryption\加密处理\待加密\保存文件,效果与上面两行代码一样。

c、其中用到的几个函数为:

        /// <summary>
        /// 读取执行路径下文件信息
        /// </summary>
        /// <param name="filename">指定的路径</param>
        /// <param name="start">起始位置,一般设为零</param>
        /// <param name="length">文件信息的长度</param>
        /// <returns></returns>
        public static byte[] ReadFile(string filename, int start, int length)
        {
            byte[] btFile = null;
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                btFile = new byte[length];
                fs.Seek(start, SeekOrigin.Begin);
                fs.Read(btFile, 0, length);
            }
            return btFile;
        }
Copy after login

//将byte数组转换为string
        public static string BytesToString(byte[] bt)
        {
            return Encoding.UTF8.GetString(bt);
        }
Copy after login

小注:

1、关于C#加密的代码,网上有各种各样,大家可以参考。

2、MD5 并不是加密算法,而是摘要算法。加密算法是可逆的,摘要算法是理论上不可逆的。如果说MD5是加密算法,那还不如称他是超级压缩算法呢,因为你输入任意长度的明文给他,结果都是一个定长16 、32、64。

3、将加密后的信息写入指定文件,可以参考记录文本日志的函数。

4、ComBox控件的使用可以参考:ComBox控件。

 以上就是C# 加密小工具代码实现的内容,更多相关内容请关注PHP中文网(www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!