DES加密和base64加密
DES简介:参考知乎 https://www.zhihu.com/question/36767829 和博客https://www.cnblogs.com/idreamo/p/9333753.html
BASE64简介:
DES加密代码实现
- 1 using System;
- 2 using System.Collections.Generic;
- 3 using System.Configuration;
- 4 using System.IO;
- 5 using System.Linq;
- 6 using System.Security.Cryptography;
- 7 using System.Text;
- 8 using System.Threading.Tasks;
- 9
- 10 namespace ConsoleApp2
- 11 {
- 12 public static class DESJiaMi
- 13 {
- 14
- 15 /// <summary>
- 16 /// 使用配置中的KEY,进行DES加密。
- 17 /// </summary>
- 18 /// <param name="pToEncrypt">要加密的字符串。</param>
- 19 /// <returns>以Base64格式返回的加密字符串。</returns>
- 20 public static string Encrypt(string pToEncrypt)
- 21 {
- 22 return Encrypt(pToEncrypt, ConfigurationManager.AppSettings["DESKey"] ?? "88888888");
- 23 }
- 24
- 25 /// <summary>
- 26 /// 使用配置中的KEY,进行DES解密。
- 27 /// </summary>
- 28 /// <param name="pToDecrypt">要解密的以Base64</param>
- 29 /// <returns>已解密的字符串。</returns>
- 30 public static string Decrypt(string pToDecrypt)
- 31 {
- 32 return Decrypt(pToDecrypt, ConfigurationManager.AppSettings["DESKey"] ?? "88888888");
- 33 }
- 34
- 35 /// <summary>
- 36 /// 进行DES加密。
- 37 /// </summary>
- 38 /// <param name="pToEncrypt">要加密的字符串。</param>
- 39 /// <param name="sKey">密钥,且必须为8位。</param>
- 40 /// <returns>以Base64格式返回的加密字符串。</returns>
- 41 public static string Encrypt(string pToEncrypt, string sKey)
- 42 {
- 43 using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
- 44 {
- 45 byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
- 46 des.Key = ASCIIEncoding.UTF8.GetBytes(sKey);
- 47 des.IV = ASCIIEncoding.UTF8.GetBytes(sKey);
- 48 des.Mode = CipherMode.CBC;
- 49 des.Padding = PaddingMode.PKCS7;
- 50 des.BlockSize = 64;
- 51 System.IO.MemoryStream ms = new System.IO.MemoryStream();
- 52 using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
- 53 {
- 54 cs.Write(inputByteArray, 0, inputByteArray.Length);
- 55 cs.FlushFinalBlock();
- 56 cs.Close();
- 57 }
- 58 string str = Convert.ToBase64String(ms.ToArray());
- 59 ms.Close();
- 60 return str;
- 61 }
- 62 }
- 63
- 64
- 65 /// <summary>
- 66 /// C# DES加密方法 返回base64字符串
- 67 /// Mode=CBC,PaddingMode=PKCS7,BlockSize=KeySize=64,Iv=Key长度
- 68 /// </summary>
- 69 /// <param name="encryptedValue">要加密的字符串</param>
- 70 /// <param name="key">密钥 为8位</param>
- 71 /// <returns>加密后的字符串</returns>
- 72 public static string DESEncrypt_Base64(string originalValue, string key)
- 73 {
- 74 try
- 75 {
- 76 if (string.IsNullOrEmpty(originalValue))
- 77 return "";
- 78 using (DESCryptoServiceProvider sa = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(key) })
- 79 {
- 80 using (ICryptoTransform ct = sa.CreateEncryptor())
- 81 {
- 82 byte[] by = Encoding.UTF8.GetBytes(originalValue);
- 83 using (var ms = new MemoryStream())
- 84 {
- 85 using (var cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
- 86 {
- 87 cs.Write(by, 0, by.Length);
- 88 cs.FlushFinalBlock();
- 89 }
- 90 return Convert.ToBase64String(ms.ToArray());
- 91 }
- 92 }
- 93 }
- 94 }
- 95 catch { }
- 96 return "";
- 97 }
- 98
- 99
- 100 /// <summary>
- 101 /// C# DES解密方法返回UTF-8格式的字符串
- 102 /// Mode=CBC,PaddingMode=PKCS7,BlockSize=KeySize=64,Iv=Key长度
- 103 /// </summary>
- 104 /// <param name="encryptedValue">待解密的字符串</param>
- 105 /// <param name="key">密钥 为8位</param>
- 106 /// <returns>解密后的字符串</returns>
- 107 public static string DESDecrypt_Base64(string encryptedValue, string key)
- 108 {
- 109 try
- 110 {
- 111 if (string.IsNullOrEmpty(encryptedValue))
- 112 return null;
- 113 using (DESCryptoServiceProvider sa = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(key) })
- 114 {
- 115 using (ICryptoTransform ct = sa.CreateDecryptor())
- 116 {
- 117 byte[] byt = Convert.FromBase64String(encryptedValue);
- 118
- 119 using (var ms = new MemoryStream())
- 120 {
- 121 using (var cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
- 122 {
- 123 cs.Write(byt, 0, byt.Length);
- 124 cs.FlushFinalBlock();
- 125 }
- 126 return Encoding.UTF8.GetString(ms.ToArray());
- 127 }
- 128 }
- 129 }
- 130 }
- 131 catch (Exception ex) { }
- 132 return "";
- 133 }
- 134
- 135 /**/
- 136 /// <summary>
- 137 /// 进行DES解密。
- 138 /// </summary>
- 139 /// <param name="pToDecrypt">要解密的以Base64</param>
- 140 /// <param name="sKey">密钥,且必须为8位。</param>
- 141 /// <returns>已解密的字符串。</returns>
- 142 public static string Decrypt(string pToDecrypt, string sKey)
- 143 {
- 144 byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
- 145 using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
- 146 {
- 147 des.Key = ASCIIEncoding.UTF8.GetBytes(sKey);
- 148 des.IV = ASCIIEncoding.UTF8.GetBytes(sKey);
- 149 des.Mode = CipherMode.CBC;//加密模式(Mode)=CBC。
- 150 des.Padding = PaddingMode.PKCS7;//填充模式(PaddingMode)=PKCS7。
- 151 System.IO.MemoryStream ms = new System.IO.MemoryStream();
- 152 using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
- 153 {
- 154 cs.Write(inputByteArray, 0, inputByteArray.Length);
- 155 cs.FlushFinalBlock();
- 156 cs.Close();
- 157 }
- 158 string str = Encoding.UTF8.GetString(ms.ToArray());
- 159 ms.Close();
- 160 return str;
- 161 }
- 162 }
- 163 }
- 164 }
base64位加密代码实现
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp2
- {
- public class Base64
- {
- /// <summary>
- /// AES base64 加密算法
- /// Key 为16位
- /// </summary>
- /// <param name="Data">需要加密的字符串</param>
- /// <param name="Key">Key 为16位 密钥</param>
- /// <param name="Vector">向量</param>
- /// <returns></returns>
- public static string RST_AesEncrypt_Base64(string Data, string Key)
- {
- if (string.IsNullOrEmpty(Data))
- {
- return null;
- }
- if (string.IsNullOrEmpty(Key))
- {
- return null;
- }
- var Vector = Key.Substring(0, 16);
- Byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
- Byte[] bKey = new Byte[32];
- Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
- Byte[] bVector = new Byte[16];
- Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
- Byte[] Cryptograph = null; // 加密后的密文
- Rijndael Aes = Rijndael.Create();
- //add
- Aes.Mode = CipherMode.CBC;//兼任其他语言的des
- Aes.BlockSize = 128;
- Aes.Padding = PaddingMode.PKCS7;
- //add end
- try
- {
- // 开辟一块内存流
- using (MemoryStream Memory = new MemoryStream())
- {
- // 把内存流对象包装成加密流对象
- using (CryptoStream Encryptor = new CryptoStream(Memory,
- Aes.CreateEncryptor(bKey, bVector),
- CryptoStreamMode.Write))
- {
- // 明文数据写入加密流
- Encryptor.Write(plainBytes, 0, plainBytes.Length);
- Encryptor.FlushFinalBlock();
- Cryptograph = Memory.ToArray();
- }
- }
- }
- catch
- {
- Cryptograph = null;
- }
- return Convert.ToBase64String(Cryptograph);
- }
- /// <summary>
- /// AES base64 解密算法
- /// Key为16位
- /// </summary>
- /// <param name="Data">需要解密的字符串</param>
- /// <param name="Key">Key为16位 密钥</param>
- /// <param name="Vector">向量</param>
- /// <returns></returns>
- public static string RST_AesDecrypt_Base64(string Data, string Key)
- {
- try
- {
- if (string.IsNullOrEmpty(Data))
- {
- return null;
- }
- if (string.IsNullOrEmpty(Key))
- {
- return null;
- }
- var Vector = Key.Substring(0, 16);
- Byte[] encryptedBytes = Convert.FromBase64String(Data);
- Byte[] bKey = new Byte[32];
- Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
- Byte[] bVector = new Byte[16];
- Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
- Byte[] original = null; // 解密后的明文
- Rijndael Aes = Rijndael.Create();
- //add
- Aes.Mode = CipherMode.CBC;//兼任其他语言的des
- Aes.BlockSize = 128;
- Aes.Padding = PaddingMode.PKCS7;
- //add end
- try
- {
- // 开辟一块内存流,存储密文
- using (MemoryStream Memory = new MemoryStream(encryptedBytes))
- {
- //把内存流对象包装成加密流对象
- using (CryptoStream Decryptor = new CryptoStream(Memory,
- Aes.CreateDecryptor(bKey, bVector),
- CryptoStreamMode.Read))
- {
- // 明文存储区
- using (MemoryStream originalMemory = new MemoryStream())
- {
- Byte[] Buffer = new Byte[1024];
- Int32 readBytes = 0;
- while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
- {
- originalMemory.Write(Buffer, 0, readBytes);
- }
- original = originalMemory.ToArray();
- }
- }
- }
- }
- catch
- {
- original = null;
- }
- return Encoding.UTF8.GetString(original);
- }
- catch { return null; }
- }
- }
- }
BASE64加密解密
- public sealed class Base64
- {
- /// <summary>
- /// Base64加密
- /// </summary>
- /// <param name="codeName">加密采用的编码方式</param>
- /// <param name="source">待加密的明文</param>
- /// <returns></returns>
- public static string EncodeBase64(Encoding encode, string source)
- {
- byte[] bytes = encode.GetBytes(source);
- try
- {
- encode = Convert.ToBase64String(bytes);
- }
- catch
- {
- encode = source;
- }
- return encode;
- }
- /// <summary>
- /// Base64加密,采用utf8编码方式加密
- /// </summary>
- /// <param name="source">待加密的明文</param>
- /// <returns>加密后的字符串</returns>
- public static string EncodeBase64(string source)
- {
- return EncodeBase64(Encoding.UTF8, source);
- }
- /// <summary>
- /// Base64解密
- /// </summary>
- /// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param>
- /// <param name="result">待解密的密文</param>
- /// <returns>解密后的字符串</returns>
- public static string DecodeBase64(Encoding encode, string result)
- {
- string decode = "";
- byte[] bytes = Convert.FromBase64String(result);
- try
- {
- decode = encode.GetString(bytes);
- }
- catch
- {
- decode = result;
- }
- return decode;
- }
- /// <summary>
- /// Base64解密,采用utf8编码方式解密
- /// </summary>
- /// <param name="result">待解密的密文</param>
- /// <returns>解密后的字符串</returns>
- public static string DecodeBase64(string result)
- {
- return DecodeBase64(Encoding.UTF8, result);
- }
- }
DES加密和base64加密的更多相关文章
- MD5加密,Base64加密/解密,AES加密/解密
1.从github上下载GTMBase64-master和AESCrypt-ObjC-master导入工程,如下图所示. 2.使用前的配置及注意事项: (1) 在build phases中的GTMBa ...
- js的常见的三种密码加密方式-MD5加密、Base64加密和解密和sha1加密详解总结
写前端的时候,很多的时候是避免不了注册这一关的,但是一般的注册是没有任何的难度的,无非就是一些简单的获取用户输入的数据,然后进行简单的校验以后调用接口,将数据发送到后端,完成一个简单的注册的流程,那么 ...
- 步步为营-33-Md5(32)加密与Base64加密
说明: 1:直接贴码 using System; using System.Collections.Generic; using System.ComponentModel; using System ...
- (iOS)Base64加密和DES加密、以及JAVA和iOS中DES加密统一性问题
我们在项目中为了安全方面的考虑,通常情况下会选择一种加密方式对需要安全性的文本进行加密,而Base64加密和DES64加密是常用的加密算法.我记得我在前一个项目中使用的就是这两种加密算法的结合:Bas ...
- C# 加密解密(DES,3DES,MD5,Base64) 类
public sealed class EncryptUtils { #region Base64加密解密 /// <summary> ...
- Java android DES+Base64加密解密
服务器与客户端加密解密传输, 中间遇到各种坑,客户端无论用AES还是DES解密时都会出现错误,后来才看到好多人说要用AES/DES加完密后还要BASE64加密,照做时发现android和java的Ba ...
- python中常用的base64 md5 aes des crc32等的加密解密
1.base64 Python内置的base64模块可以实现base64.base32.base16.base85.urlsafe_base64的编码解码,python 3.x通常输入输出都是二进制形 ...
- Base64加密解密原理以及代码实现(VC++)
Base64加密解密原理以及代码实现 转自:http://blog.csdn.net/jacky_dai/article/details/4698461 1. Base64使用A--Z,a--z,0- ...
- python 加密解密(base64, AES)
1. 使用base64 s1 = base64.encodestring('hello world') s2 = base64.decodestring(s1) print s1, s2 结果 1 2 ...
- Android网络传输中必用的两个加密算法:MD5 和 RSA 及Base64加密总结
(1)commons-codec包简介 包含一些通用的编码解码算法.包括一些语音编码器,Hex,Base64.MD5 一.md5.base64.commons-codec包 commons-codec ...
随机推荐
- @Scheduled cron 定时任务表达式含义,及* ?的区别
好多网友对@Scheduled cron表达式含义做了阐述,个人认为很多对于 * ?的说明不够具体也不算准确,借此本文特别对 * ?做一下说明. cron格式:[秒数][分钟][小时][日期][月份] ...
- 【SpringBoot】 启动后会调用执行的方法的 (五种方式)
在 SpringBoot 工程 启动后, 会调用执行方法的五种方式: 亲自测试, 按照执行顺序如下: 第一种: @Component public class SpringContext1 { @Po ...
- 体验函数计算 FC 3.0,写测评赢取索尼头戴式耳机
11月1日云栖大会,函数计算3.0全新升级,相对函数计算2.0,3.0版本突出易用性.高弹性,并且可以和更多阿里云服务无缝集成.业内首发神龙 Serverless GPU 架构,冷启动大幅优化,全链路 ...
- mybatisplus 查询结果排除某字段实现
数据有Test表,表里有id,name,ip_address,last_time四个字段 通常查询写法,返回结果会把id,name,ip_address,last_time四个字段都返回 public ...
- C#设计模式18——迭代器模式的写法
是什么: 迭代器模式是一种行为型设计模式,它允许客户端通过一种统一的方式遍历集合对象中的元素,而无需暴露集合对象的内部结构. 为什么: 使用迭代器模式可以使得客户端程序与集合对象解耦,从而可以更加灵活 ...
- C#使用正则表达式检查字符串中重复出现的词
private void button1_Click(object sender, EventArgs e) { MatchCollection matches =//使用正则表达式查找重复出现单词的 ...
- Kubernetes APIServer 最佳实践
1. kubernetes 整体架构 kubernetes 由 master 节点和工作节点组成.其中,master 节点的组件有 APIServer,scheduler 和 controller-m ...
- 传说中 PUE 预测精度高达 0.005 的工作
杨震, 赵静洲, 林依挺 等. 数据中心 PUE 能效优化的机器学习方法. 系统工程理论与实践, 2022, 42(3): 811-832 省流: 这是 2020 年的论文,用神经网络进行了认真的 P ...
- Git | git branch 分支操作
假设我们已经有了稳定的代码,现在我想整一些花活.比较安全的一个方式是,在新的分支上整活. 新建 vga 分支:git branch vga,然后切换到 vga 分支:git switch vga,或者 ...
- SV OOP-2
静态变量 继承性(Inheritance) 抽象类和虚方法virtual methods 多态(Ploymorphism) 通过基类的变量可以使用子类的对象 基类中定义的virtual functio ...