加密算法实现代码 20
设计程序是Borland C++Builder 6
在对话框里有2个输入框,两个单选按钮和一个按钮。单选按钮是选择加密算法的,有md5和sha-1。第一个输入框是输入明文的,第二个输入框是输出密文的,现在要点击按钮时进行加密的代码。注意:只要的是点击按纽后对明文加密的代码。 展开
这个是界面效果,我不是用C++写的,是用C#写的可以参考下:
实现的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Security.Cryptography;
using System.Security;
namespace Key
{
public partial class frmKey : Form
{
private string key; //默认密钥 "yupengcheng"
private byte[] sKey;
private byte[] sIV;
public frmKey()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button4.Enabled = false;
}
/// <summary>
/// 选择输入路径
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
//openFileDialog1.Filter = "所有文件(*.*)|*.*";
openFileDialog1.ShowDialog();
string filename = openFileDialog1.FileName;
int i = filename.LastIndexOf(".");
if (i != -1)
{
string filetype = filename.Substring(filename.LastIndexOf("."));
if (this.radioButton1.Checked)
{
filename = filename.Replace("(解密文件)", "");
textBox2.Text = filename.Substring(0, filename.LastIndexOf(".")) + "(加密文件)" + filetype;
}
else
{
filename = filename.Replace("(加密文件)", "");
textBox2.Text = filename.Substring(0, filename.LastIndexOf(".")) + "(解密文件)" + filetype;
}
}
if (filename != "")
{
textBox1.Text = openFileDialog1.FileName;
}
}
/// <summary>
/// 选择输出路径
/// </summary>
private void button4_Click(object sender, EventArgs e)
{
this.saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
this.textBox2.Text = saveFileDialog1.FileName;
}
}
/// <summary>
/// 加密radioButton
/// </summary>
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
button3.Text = "开始加密";
}
/// <summary>
/// 解密radioButton
/// </summary>
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
button3.Text = "开始解密";
}
/// <summary>
/// 明密/暗密
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
if (button2.Text=="明密")
{
textBox3.PasswordChar = Convert.ToChar(0);
button2.Text = "暗密";
}
else
{
textBox3.PasswordChar = char .Parse ("*");
button2.Text = "明密";
}
}
/// <summary>
/// 开始加密/开始解密
/// </summary>
private void button3_Click(object sender, EventArgs e)
{
if (this.textBox1.Text == "" || this.textBox2.Text == "")
{
MessageBox.Show("文件路径不能为空!", "警告提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (textBox3.Text != "yupengcheng")
{
MessageBox.Show("输入的密码不正确,请重新输入!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox3.Text = "";
return;
}
else
{
key = "yupengcheng";
if (button3.Text == "开始加密")
{
statusBar1.Visible = true;
statusBar1.Text = "正在加密文件,请等待.....";
if (EncryptFile(this.textBox1.Text, this.textBox2.Text, textBox3.Text))
{
statusBar1.Text = "加密完成。";
MessageBox.Show("文件加密成功!", "成功提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
statusBar1.Visible = false;
}
else
{
statusBar1.Visible = true;
statusBar1.Text = "正在解密文件,请等待.....";
if (DecryptFile(this.textBox1.Text, this.textBox2.Text, textBox3.Text))
{
statusBar1.Text = "解密完成。";
MessageBox.Show("文件解密成功!", "成功提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
statusBar1.Visible = false;
}
}
}
/// <summary>
/// 取消
/// </summary>
private void button5_Click(object sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// 加密方法
/// </summary>
/// <param name="filePath">加密输入路径</param>
/// <param name="savePath">加密输出路径</param>
/// <param name="keyStr">密匙</param>
/// <returns></returns>
public bool EncryptFile(string filePath, string savePath, string keyStr)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if (keyStr == "")
keyStr = key;
try
{
FileStream fs = File.OpenRead(filePath);
byte[] inputByteArray = new byte[fs.Length];
fs.Read(inputByteArray, 0, (int)fs.Length);
fs.Close();
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
SHA1 ha = new SHA1Managed();
byte[] hb = ha.ComputeHash(keyByteArray);
sKey = new byte[8];
sIV = new byte[8];
for (int i = 0; i < 8; i++)
sKey[i] = hb[i];
for (int i = 8; i < 16; i++)
sIV[i - 8] = hb[i];
des.Key = sKey;
des.IV = sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close();
return true;
}
catch
{
MessageBox.Show("找不到指定的文件,请重新输入!", "警告提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
}
/// <summary>
/// 解密方法
/// </summary>
/// <param name="filePath">解密输入路径</param>
/// <param name="savePath">解密输出路径</param>
/// <param name="keyStr">密匙</param>
/// <returns></returns>
public bool DecryptFile(string filePath, string savePath, string keyStr)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if (keyStr == "")
keyStr = key;
try
{
FileStream fs = File.OpenRead(filePath);
byte[] inputByteArray = new byte[fs.Length];
fs.Read(inputByteArray, 0, (int)fs.Length);
fs.Close();
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
SHA1 ha = new SHA1Managed();
byte[] hb = ha.ComputeHash(keyByteArray);
sKey = new byte[8];
sIV = new byte[8];
for (int i = 0; i < 8; i++)
sKey[i] = hb[i];
for (int i = 8; i < 16; i++)
sIV[i - 8] = hb[i];
des.Key = sKey;
des.IV = sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close();
return true;
}
catch
{
MessageBox.Show("找不到指定的文件,请重新输入!", "警告提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox1.Text == null)
{
button4.Enabled = false;
}
else
{
button4.Enabled = true;
}
}
}
}
2024-09-19 广告