Home Backend Development C#.Net Tutorial C# encryption gadget code implementation

C# encryption gadget code implementation

Feb 25, 2017 am 11:09 AM


关键点有以下几个:

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)!



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

See all articles