wtuqi
2/23/2020 - 5:36 AM

通用加密解密

public string Decrypt(string data, string Key) //解密
{
    try
     {
        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(Key);
        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(Key);
        byte[] byEnc;
        try
        {
            byEnc = Convert.FromBase64String(data);
        }
        catch
        {
            return null;
        }
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        MemoryStream ms = new MemoryStream(byEnc);
        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
        StreamReader sr = new StreamReader(cst);
        return sr.ReadToEnd();
    }
    catch
    {
        return "加密锁已失效";
    }
}
//在本机加密可在他机解密,通用过程
public string Encryption(string data, string Key) //加密
{
    try
    {
        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(Key);
        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(Key);
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        int i = cryptoProvider.KeySize;
        MemoryStream ms = new MemoryStream();
        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
        StreamWriter sw = new StreamWriter(cst);
        sw.Write(data);
        sw.Flush();
        cst.FlushFinalBlock();
        sw.Flush();
        return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
    }       
    catch
    {
        return "不能识别的加密锁";
    }
}
//文件加密解密
//窗体
Func<string[]> OpenFiles = () =>
{
    string[] FileNames = null;
    OpenFileDialog OFD = new OpenFileDialog();
    OFD.Multiselect = true;//允许多选
    OFD.Filter = "JPG图像|*.jpg|Png图像|*.png|BMP图像|*.bmp|All files|*.*";
    if (OFD.ShowDialog() == DialogResult.OK && OFD.FileNames != null)
    {
        FileNames = OFD.FileNames;
    }
    else { FileNames = null; }
    return FileNames;
};


private void button1_Click(object sender, EventArgs e)
{
    string[] files=OpenFiles();
    if (files == null) { return; }
    foreach (string s in files.ToArray()) //遍历选择文件并加密
    {
        string File =s;
        EncryptFiles.EncryptFile(File, textBox1.Text);
    }
}

private void button2_Click(object sender, EventArgs e)
{
    string[] files = OpenFiles();
    if (files == null) { return; }
    foreach (string s in files.ToArray()) //遍历选择文件并解密
    {
    string File =s;
    EncryptFiles.DecryptFile(File, textBox1.Text);
    }
}

//实现功能类
static class EncryptFiles
{
    public static void EncryptFile(string inputFile,string password)   //加密
    {
        try
        {
            string outputFile = inputFile + "N";
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] key = UE.GetBytes(password);

            string cryptFile = outputFile;
            FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

            RijndaelManaged RMCrypto = new RijndaelManaged();

            CryptoStream cs = new CryptoStream(fsCrypt,
                RMCrypto.CreateEncryptor(key, key),
                CryptoStreamMode.Write);
            FileStream fsIn = new FileStream(inputFile, FileMode.Open);
            int data;
            while ((data = fsIn.ReadByte()) != -1)
                cs.WriteByte((byte)data);

            fsIn.Close();
            cs.Close();
            fsCrypt.Close();
            File.Delete(inputFile);
            File.Move(outputFile, inputFile);
            File.Delete(outputFile);
            MessageBox.Show("Encrypt Source file succeed!", "Msg :");
        }
        catch(Exception ex)
        {
            MessageBox.Show("Source file error!", "Error :");
        }
    }


    public static void DecryptFile(string inputFile, string password)   //解密
    {
        try
        {
            string outputFile = inputFile + "N";
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] key = UE.GetBytes(password);

            FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

            RijndaelManaged RMCrypto = new RijndaelManaged();

            CryptoStream cs = new CryptoStream(fsCrypt,
                RMCrypto.CreateDecryptor(key, key),
                CryptoStreamMode.Read);

            FileStream fsOut = new FileStream(outputFile, FileMode.Create);

            int data;
            while ((data = cs.ReadByte()) != -1)
                fsOut.WriteByte((byte)data);

            fsOut.Close();
            cs.Close();
            fsCrypt.Close();
            File.Delete(inputFile);
            File.Move(outputFile, inputFile);
            File.Delete(outputFile);
            MessageBox.Show("Decrypt Source file succeed!", "Msg :");

        }
        catch (Exception ex)
        {
            MessageBox.Show("Source file error", "Error :");
        }
    }
}