.NET DES 加密
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Security;
- using System.IO;
- using System.Security.Cryptography;
- namespace DLProject
- {
- class EncryptAndDecrypt
- {
- /// <summary>
- /// 对字符串进行DES加密
- /// </summary>
- /// <param >将要加密的字符串</param>
- /// <param >密钥值(需为8位字符串)</param>
- /// <returns></returns>
- public string EncryptString(string sInputString, string sKey)
- {
- byte[] data = Encoding.ASCII.GetBytes(sInputString);
- DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
- DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
- DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
- MemoryStream ms = new MemoryStream(); //创建其支持存储区为内存的流。
- CryptoStream cs = new CryptoStream(ms, DES.CreateEncryptor(), CryptoStreamMode.Write);//将数据流连接到加密转换流
- cs.Write(data, 0, data.Length);
- cs.FlushFinalBlock(); //用缓冲区的当前状态更新基础数据源或储存库,随后清除缓
- StringBuilder ret = new StringBuilder();
- foreach (byte b in ms.ToArray())
- {
- ret.AppendFormat("{0:X2}", b);
- }
- return ret.ToString();
- }
- // DES解密字符串
- public string DecryptString(string sInputString, string sKey)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- //Put the input string into the byte array
- byte[] inputByteArray = new byte[sInputString.Length / 2];
- for (int x = 0; x < sInputString.Length; x += 2)
- {
- int i = Convert.ToInt32(sInputString.Substring(x, 2), 16);
- inputByteArray[x / 2] = (byte)i;
- }
- des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
- des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
- //Flush the data through the crypto stream into the memory stream
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- //Get the decrypted data back from the memory stream
- //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
- StringBuilder ret = new StringBuilder();
- return System.Text.Encoding.UTF8.GetString(ms.ToArray());
- }
- }
- }
.NET DES 加密的更多相关文章
- Android数据加密之Des加密
前言: 端午节前有个同事咨询我有关Android DES加密的相关实现,简单的实现了一下,今天来总结一下. 其他几种加密方式: Android数据加密之Rsa加密 Android数据加密之Aes加密 ...
- php使用openssl进行Rsa长数据加密(117)解密(128) 和 DES 加密解密
PHP使用openssl进行Rsa加密,如果要加密的明文太长则会出错,解决方法:加密的时候117个字符加密一次,然后把所有的密文拼接成一个密文:解密的时候需要128个字符解密一下,然后拼接成数据. 加 ...
- DES加密
接口等加密字段 import java.security.SecureRandom; import javax.crypto.Cipher;import javax.crypto.SecretKey; ...
- c# DES加密解密
class DESHelper { string _iv = "9AUP"; string _key = "9d"; /// <summary> / ...
- (iOS)Base64加密和DES加密、以及JAVA和iOS中DES加密统一性问题
我们在项目中为了安全方面的考虑,通常情况下会选择一种加密方式对需要安全性的文本进行加密,而Base64加密和DES64加密是常用的加密算法.我记得我在前一个项目中使用的就是这两种加密算法的结合:Bas ...
- PYTHON实现DES加密及base64源码
要求是实现DES加密,解密,我是用python实现的,还是有挺多坑的,改bug就改了挺久,加密实现后,解密过程就比较轻松. 另外,附加base64编码源码 要求:输入秘钥为64位二进制数(有效位为56 ...
- des加密解密——java加密,php解密
最近在做项目中,遇到des加密解密的问题. 场景是安卓app端用des加密,php这边需要解密.之前没有接触过des这种加密解密算法,但想着肯定会有demo.因此百度,搜了代码来用.网上代码也是鱼龙混 ...
- DES加密解密
加密后生成Base64字符串,并去除'='字符. 加密后替换掉'+',这样加密后的字符串可以作为url参数传递. using System; using System.IO; using System ...
- iOS 开发之路(AES/DES加密实现) 三
最近接触的这个项目由于以前服务器上用的是DES/CBC/PKCS5Padding加密方式,为了让在iOS上的加密结果与服务器端保持一致,我做了很多尝试,现在分享给大家.PS:现在不推荐用DES了,只是 ...
- php des 加密解密实例
des加密是对称加密中在互联网应用的比较多的一种加密方式,php 通过mcrypt扩展库来支持des加密,要在Php中使用des加密,需要先安装mcrypt扩展库 $iv_size = mcrypt_ ...
随机推荐
- SSH环境搭建步骤解析
一.建立Java web project:AngelSSH 二.引入jar包,必要清单如下 2.1,Struts2 commons-fileupload 文件上传组件 commons-io io ...
- spm3安装和使用
readme : 因为我在在spm3中主要用到的是spm build这个命令,因此本文简单的介绍一下如何安装spm3和使用其中的spm build命令 一.安装 1.安装nodejs 直接去官网下载n ...
- Linux下多线程编程
一.为什么要引入线程? 使用多线程的理由之一是和进程相比,它是一种非常"节俭"的多任务操作方式.在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维 ...
- 七、Action动作类
七.Action动作类(一般用**Action结尾) 1.编写动作类的三种方式 动作类不实现.也不继承任何的接口和类.即动作类是一个非常普通的JavaBean. public class HelloA ...
- Linux下发包处理
Linux下发包处理: 1.用top分析工具来查看哪个进程占用的CPU资源比较大 2. 通过命令来查看都是那些端口被占用了 netstat -antp | more 3.在top里面查看到的异 ...
- Clearing Search Values
Use the SearchClear() method which is part of the Field class to clear search values on a particul ...
- js-布尔值
1.任何JavaScript的值都可以转换为布尔值 下面这些将会转换为false(假值): undefined null 0 -0 NaN "" //空字符串 所有其他值,包括所有 ...
- synchronized的重入
/** * synchronized的重入 * */ public class SyncDubbo1 { public synchronized void method1(){ System.out. ...
- MongoDB工具介绍
在Windows下面,mongodb就只有一个bin目录以及bin目录以外的三个文件,相对bin目录中包括了如下文件: bsondump.exe 用于将导出的BSON文件格式转换为JSON格式 mon ...
- spark-submit工具参数说明
执行时需要传入的参数说明 Usage: spark-submit [options] <app jar | python file> [app options] 参数名称 含义 --mas ...