封装了一个对称加解密的类,用私钥和密钥加解密

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography; namespace CMD.EDI
{
public class EncryptHandler
{
/// <summary>
/// 加密字符串
/// </summary>
public static string Encrypt(string password, string cleartext)
{
string password2 = "Ahbool"; string cipher;
char[] key = new char[];
if (password.Length > )
{
password = password.Remove();
}
password.CopyTo(, key, , password.Length); char[] iv = new char[];
if (password2.Length > )
{
password2 = password2.Remove();
}
password2.CopyTo(, iv, , password2.Length); if (cleartext == null)
{
return string.Empty;
} SymmetricAlgorithm serviceProvider = new DESCryptoServiceProvider();
serviceProvider.Key = Encoding.ASCII.GetBytes(key);
serviceProvider.IV = Encoding.ASCII.GetBytes(iv); MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, serviceProvider.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter streamWriter = new StreamWriter(cryptoStream); streamWriter.Write(cleartext);
streamWriter.Dispose();
cryptoStream.Dispose(); byte[] signData = memoryStream.ToArray();
memoryStream.Dispose();
serviceProvider.Clear();
cipher = Convert.ToBase64String(signData); return cipher;
} /// <summary>
/// 解密字符串
/// </summary>
public static string Decrypt(string password, string ciphertext)
{
string password2 = "Ahbool"; string cipher = string.Empty; try
{
char[] key = new char[];
if (password.Length > )
{
password = password.Remove();
}
password.CopyTo(, key, , password.Length); char[] iv = new char[];
if (password2.Length > )
{
password2 = password2.Remove();
}
password2.CopyTo(, iv, , password2.Length); if (ciphertext == null)
{
return cipher;
} SymmetricAlgorithm serviceProvider = new DESCryptoServiceProvider();
serviceProvider.Key = Encoding.ASCII.GetBytes(key);
serviceProvider.IV = Encoding.ASCII.GetBytes(iv); byte[] contentArray = Convert.FromBase64String(ciphertext);
MemoryStream memoryStream = new MemoryStream(contentArray);
CryptoStream cryptoStream = new CryptoStream(memoryStream, serviceProvider.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(cryptoStream); cipher = streamReader.ReadToEnd(); streamReader.Dispose();
cryptoStream.Dispose();
memoryStream.Dispose();
serviceProvider.Clear(); }
catch (Exception ex)
{
throw new SystemException("密钥错误,数据包解密失败.");
} return cipher;
} }
}

一个对称加密、解密的方法C#工具类的更多相关文章

  1. 分享两个模拟get和post方法的工具类,让应用能够与服务器进行数据交互

    很久没有码字了,今天跟大家分享一个模拟get和post方法的工具类,在安卓应用中很多都需要跟服务器进行数据交互,这需要两方面的配合,首先服务器端会给应用提供一些数据交互的接口,可是怎样在应用中去调用呢 ...

  2. linux环境下给文件加密/解密的方法

      原文地址:linix环境下给文件加密/解密的方法 作者:oracunix 一. 利用 vim/vi 加密:优点:加密后,如果不知道密码,就看不到明文,包括root用户也看不了:缺点:很明显让别人知 ...

  3. DESEncrypt对称加密解密

    分享一个很好用的DESEncrypt对称加密解密的类 using System; using System.Security.Cryptography; using System.Text; usin ...

  4. AES对称加密解密类

    import java.io.UnsupportedEncodingException; import javax.crypto.Cipher; import javax.crypto.spec.Se ...

  5. 使用jframe编写一个base64加密解密工具

    该工具可以使用exe4j来打包成exe工具(如何打包自己百度) 先上截图功能 运行main方法后,会弹出如下窗口 输入密文 然后点击解密,在点格式化 代码分享 package tools;import ...

  6. 使用Aes对称加密解密Web.Config数据库连接串

    现在很多公司开始为了保证数据库的安全性,通常会对Web.Config的数据库连接字符串进行加密.本文将介绍学习使用Aes加密解密数据库连接字符串.本文采用MySql数据库. AES概念简述 AES 是 ...

  7. 使用java实现对称加密解密(AES),非对称加密解密(RSA)

    对称加密:双方采用同样的秘钥进行加密和解密.特点是速度快,但是安全性没有非对称加密高 非对称加密:接收方生成的公有秘钥公布给发送方,发送方使用该公有秘钥加密之后,发送给接收方,然后接收方使用私有秘钥解 ...

  8. TripleDES对称加密解密 -[C#-JAVA]

    C#代码段 先MD5 再TripleDES加密 using System;using System.Collections.Generic;using System.Linq;using System ...

  9. python AES 双向对称加密解密

    高级加密标准(Advanced Encryption Standard,AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES,已经被多方分 ...

随机推荐

  1. http://blogs.msdn.com/b/pranavwagh/archive/2007/03/03/word-2007-file-seems-to-be-deleted-when-you-open-and-save-it-using-dsoframer.aspx

    http://blogs.msdn.com/b/pranavwagh/archive/2007/03/03/word-2007-file-seems-to-be-deleted-when-you-op ...

  2. #用Python直接写UTF-8文本文件

    当我们这样建立文件时 f = file('x1.txt', 'w') f.write(u'中文') f.colse() 直接结果应该是类似 f.write(u'中文') UnicodeEncodeEr ...

  3. windows服务安装及卸载

    1)安装脚本Install.bat%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe JobSchedule.exeNet ...

  4. Python字典 (dictionary)

    字典dict,是Python唯一的标准mapping类型,也是内置在Python解释器中的. mapping object把一个可哈希的值(hashable value)映射到一个任意的object上 ...

  5. java反射工具类

    package com.yingchao.kgou.core; import java.lang.reflect.Field; import java.lang.reflect.InvocationT ...

  6. datatable把一个LIst的数据放入两个colum防止窜行的做法

    DataColumn objectOne = new DataColumn("objectOne", typeof(object)); dt.Columns.Add(objectO ...

  7. .net 配置ueditor

    添加引用如下: <script src="../Ueditor/ueditor.config.js" type="text/javascript"> ...

  8. Educational Codeforces Round 5 E. Sum of Remainders (思维题)

    题目链接:http://codeforces.com/problemset/problem/616/E 题意很简单就不说了. 因为n % x = n - n / x * x 所以答案就等于 n * m ...

  9. Android 保存联系人,包括部门\职位\传真\地址\照片

    private void toSaveContactInfo() { ContentValues values = new ContentValues(); // 首先向RawContacts.CON ...

  10. android 开源 + 一些素材网站

    ui 设计工具:http://www.sketchcn.com/ 分类汇总: https://github.com/Trinea/android-open-project 直接拿来用!最火的Andro ...