/// <summary>
/// 加密字符串
/// 注意:密钥必须为8位
/// </summary>
/// <param name="strText">字符串</param>
/// <param name="encryptKey">密钥</param>
/// <param name="encryptKey">返回加密后的字符串</param>
public string DesEncrypt(string inputString, string encryptKey)
{
byte[] byKey = null;
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(inputString);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (System.Exception error)
{
//return error.Message;
return null;
}
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="this.inputString">加了密的字符串</param>
/// <param name="decryptKey">密钥</param>
/// <param name="decryptKey">返回解密后的字符串</param>
public string DesDecrypt(string inputString, string decryptKey)
{
byte[] byKey = null;
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = new Byte[inputString.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(inputString);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch (System.Exception error)
{
//return error.Message;
return null;
}
}

C#字符串加密解密的更多相关文章

  1. C# 字符串加密解密函数

    原文:C# 字符串加密解密函数 using System; using System.Text;using System.Security.Cryptography; using System.IO; ...

  2. 简单的JavaScript字符串加密解密

    简单的JavaScript字符串加密解密 <div> <input type="text" id="input" autofocus=&quo ...

  3. java字符串加密解密

    java字符串加密解密 字符串加密解密的方式很多,每一种加密有着相对的解密方法.下面要说的是java中模拟php的pack和unpack的字符串加密解密方法. java模拟php中pack: /** ...

  4. C# 字符串加密解密方法

    这个是加密的算法的命名空间,使用加密算法前要引用该程序集  System.Security.Cryptography using System;using System.Data;using Syst ...

  5. 从网上整理的一些delphi字符串加密解密方法

    function Encode(Str: string): string; var //加密 TmpChr: AnsiChar; i, Len: integer; begin Result := St ...

  6. NET实现RSA AES DES 字符串 加密解密以及SHA1 MD5加密

    本文列举了    数据加密算法(Data Encryption Algorithm,DEA) 密码学中的高级加密标准(Advanced EncryptionStandard,AES)RSA公钥加密算法 ...

  7. php使用内置的mcrypt_encrypt和mcrypt_decrypt进行字符串加密解密

    <?php /*****************************加密*******************************/$key = "miyao";// ...

  8. 在JavaWeb项目中URL中字符串加密解密方案

    URL由来: 一般来说,URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和符号.比如,世界上有英文字母的网址 “http://www.abc.com”,但是没有希腊字母的网址“htt ...

  9. Delphi字符串加密/解密

    unit uEncrypt_Decrypt;   interface   uses SysUtils;   const XorKey: array[0..7] of Byte = ($B2, $09, ...

  10. [Python] 字符串加密解密

    1. 最简单的方法是用base64: import base64 s1 = base64.encodestring('hello world') s2 = base64.decodestring(s1 ...

随机推荐

  1. 【git】之clone(克隆)

    直接克隆 git clone https://github.com/gyjx/test.git 指定克隆某个分支 git clone -b dev https://github.com/gyjx/te ...

  2. computer、methods和watch

    在vue中处理复杂的逻辑的时候,我们经常使用计算属性computer,但是很多时候,我们会把计算属性.方法和侦听器搞混淆,在 w3cplus.com的一篇文章中是这样总结这三者的. methods:正 ...

  3. 导入jar包的方法

    右键项目弹出菜单,进行如下选择: 4.在配置页中,选中Libraries标签页,然后点击Add JARs选择刚才拷贝过来的jar包.最后点击apply and close. 添加完成后图标会发生变化 ...

  4. RPM安装MYSQL5.7

    RPM安装MYSQL5.7 1:YUM安装依赖库 yum install perl libaio numactl 2:下载安装需要的RPM包 https://dev.mysql.com/get/Dow ...

  5. windows server 2008 R2 安装

    微软服务器操作系统大致有: server 2000(简称2K),还有server 2003(2K3),server 2008(2K8),server 2000和2003是基于NT内核的,而2008是基 ...

  6. [UE4]UI之间传递数据

    通过创建对方UI类型的变量引用,初始本控件时赋值该变量,就可以对方UI内的方法了.

  7. Could not determine own NN ID in namespace 'mycluster'

    执行hdfs namenode -bootstrapStandby的时候报错如下 19/03/24 18:00:48 ERROR namenode.NameNode: Failed to start ...

  8. 动态材质实例(Dynamic Material Instance)

    转自:http://blog.csdn.net/panda1234lee/article/details/62041775 本例将通过 “靠近影响椅子的颜色” 来展示什么是 动态材质实例(Dynami ...

  9. MySQL产生随机字符

    MySQL产生随机字符 UUID简介 UUID含义是通用唯一识别码 (Universally Unique Identifier),这是一个软件建构的标准,也是被开源软件基金会 (Open Softw ...

  10. CentOS之Shell文件编写基础

    shell文件以.sh结尾,这是一种习惯而已.第一行以#! /bin/bash开头:表示该文件使用的是bash语法: 如果不设置该行,你的shell脚本也可以执行,但是不符合规范.#表示注释. # v ...