DES简介:参考知乎 https://www.zhihu.com/question/36767829 和博客https://www.cnblogs.com/idreamo/p/9333753.html

BASE64简介:

DES加密代码实现

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Configuration;
  4. 4 using System.IO;
  5. 5 using System.Linq;
  6. 6 using System.Security.Cryptography;
  7. 7 using System.Text;
  8. 8 using System.Threading.Tasks;
  9. 9
  10. 10 namespace ConsoleApp2
  11. 11 {
  12. 12 public static class DESJiaMi
  13. 13 {
  14. 14
  15. 15 /// <summary>
  16. 16 /// 使用配置中的KEY,进行DES加密。
  17. 17 /// </summary>
  18. 18 /// <param name="pToEncrypt">要加密的字符串。</param>
  19. 19 /// <returns>以Base64格式返回的加密字符串。</returns>
  20. 20 public static string Encrypt(string pToEncrypt)
  21. 21 {
  22. 22 return Encrypt(pToEncrypt, ConfigurationManager.AppSettings["DESKey"] ?? "88888888");
  23. 23 }
  24. 24
  25. 25 /// <summary>
  26. 26 /// 使用配置中的KEY,进行DES解密。
  27. 27 /// </summary>
  28. 28 /// <param name="pToDecrypt">要解密的以Base64</param>
  29. 29 /// <returns>已解密的字符串。</returns>
  30. 30 public static string Decrypt(string pToDecrypt)
  31. 31 {
  32. 32 return Decrypt(pToDecrypt, ConfigurationManager.AppSettings["DESKey"] ?? "88888888");
  33. 33 }
  34. 34
  35. 35 /// <summary>
  36. 36 /// 进行DES加密。
  37. 37 /// </summary>
  38. 38 /// <param name="pToEncrypt">要加密的字符串。</param>
  39. 39 /// <param name="sKey">密钥,且必须为8位。</param>
  40. 40 /// <returns>以Base64格式返回的加密字符串。</returns>
  41. 41 public static string Encrypt(string pToEncrypt, string sKey)
  42. 42 {
  43. 43 using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
  44. 44 {
  45. 45 byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
  46. 46 des.Key = ASCIIEncoding.UTF8.GetBytes(sKey);
  47. 47 des.IV = ASCIIEncoding.UTF8.GetBytes(sKey);
  48. 48 des.Mode = CipherMode.CBC;
  49. 49 des.Padding = PaddingMode.PKCS7;
  50. 50 des.BlockSize = 64;
  51. 51 System.IO.MemoryStream ms = new System.IO.MemoryStream();
  52. 52 using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
  53. 53 {
  54. 54 cs.Write(inputByteArray, 0, inputByteArray.Length);
  55. 55 cs.FlushFinalBlock();
  56. 56 cs.Close();
  57. 57 }
  58. 58 string str = Convert.ToBase64String(ms.ToArray());
  59. 59 ms.Close();
  60. 60 return str;
  61. 61 }
  62. 62 }
  63. 63
  64. 64
  65. 65 /// <summary>
  66. 66 /// C# DES加密方法 返回base64字符串
  67. 67 /// Mode=CBC,PaddingMode=PKCS7,BlockSize=KeySize=64,Iv=Key长度
  68. 68 /// </summary>
  69. 69 /// <param name="encryptedValue">要加密的字符串</param>
  70. 70 /// <param name="key">密钥 为8位</param>
  71. 71 /// <returns>加密后的字符串</returns>
  72. 72 public static string DESEncrypt_Base64(string originalValue, string key)
  73. 73 {
  74. 74 try
  75. 75 {
  76. 76 if (string.IsNullOrEmpty(originalValue))
  77. 77 return "";
  78. 78 using (DESCryptoServiceProvider sa = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(key) })
  79. 79 {
  80. 80 using (ICryptoTransform ct = sa.CreateEncryptor())
  81. 81 {
  82. 82 byte[] by = Encoding.UTF8.GetBytes(originalValue);
  83. 83 using (var ms = new MemoryStream())
  84. 84 {
  85. 85 using (var cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
  86. 86 {
  87. 87 cs.Write(by, 0, by.Length);
  88. 88 cs.FlushFinalBlock();
  89. 89 }
  90. 90 return Convert.ToBase64String(ms.ToArray());
  91. 91 }
  92. 92 }
  93. 93 }
  94. 94 }
  95. 95 catch { }
  96. 96 return "";
  97. 97 }
  98. 98
  99. 99
  100. 100 /// <summary>
  101. 101 /// C# DES解密方法返回UTF-8格式的字符串
  102. 102 /// Mode=CBC,PaddingMode=PKCS7,BlockSize=KeySize=64,Iv=Key长度
  103. 103 /// </summary>
  104. 104 /// <param name="encryptedValue">待解密的字符串</param>
  105. 105 /// <param name="key">密钥 为8位</param>
  106. 106 /// <returns>解密后的字符串</returns>
  107. 107 public static string DESDecrypt_Base64(string encryptedValue, string key)
  108. 108 {
  109. 109 try
  110. 110 {
  111. 111 if (string.IsNullOrEmpty(encryptedValue))
  112. 112 return null;
  113. 113 using (DESCryptoServiceProvider sa = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(key) })
  114. 114 {
  115. 115 using (ICryptoTransform ct = sa.CreateDecryptor())
  116. 116 {
  117. 117 byte[] byt = Convert.FromBase64String(encryptedValue);
  118. 118
  119. 119 using (var ms = new MemoryStream())
  120. 120 {
  121. 121 using (var cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
  122. 122 {
  123. 123 cs.Write(byt, 0, byt.Length);
  124. 124 cs.FlushFinalBlock();
  125. 125 }
  126. 126 return Encoding.UTF8.GetString(ms.ToArray());
  127. 127 }
  128. 128 }
  129. 129 }
  130. 130 }
  131. 131 catch (Exception ex) { }
  132. 132 return "";
  133. 133 }
  134. 134
  135. 135 /**/
  136. 136 /// <summary>
  137. 137 /// 进行DES解密。
  138. 138 /// </summary>
  139. 139 /// <param name="pToDecrypt">要解密的以Base64</param>
  140. 140 /// <param name="sKey">密钥,且必须为8位。</param>
  141. 141 /// <returns>已解密的字符串。</returns>
  142. 142 public static string Decrypt(string pToDecrypt, string sKey)
  143. 143 {
  144. 144 byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
  145. 145 using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
  146. 146 {
  147. 147 des.Key = ASCIIEncoding.UTF8.GetBytes(sKey);
  148. 148 des.IV = ASCIIEncoding.UTF8.GetBytes(sKey);
  149. 149 des.Mode = CipherMode.CBC;//加密模式(Mode)=CBC。
  150. 150 des.Padding = PaddingMode.PKCS7;//填充模式(PaddingMode)=PKCS7。
  151. 151 System.IO.MemoryStream ms = new System.IO.MemoryStream();
  152. 152 using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
  153. 153 {
  154. 154 cs.Write(inputByteArray, 0, inputByteArray.Length);
  155. 155 cs.FlushFinalBlock();
  156. 156 cs.Close();
  157. 157 }
  158. 158 string str = Encoding.UTF8.GetString(ms.ToArray());
  159. 159 ms.Close();
  160. 160 return str;
  161. 161 }
  162. 162 }
  163. 163 }
  164. 164 }

base64位加密代码实现

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApp2
  10. {
  11. public class Base64
  12. {
  13. /// <summary>
  14. /// AES base64 加密算法
  15. /// Key 为16位
  16. /// </summary>
  17. /// <param name="Data">需要加密的字符串</param>
  18. /// <param name="Key">Key 为16位 密钥</param>
  19. /// <param name="Vector">向量</param>
  20. /// <returns></returns>
  21. public static string RST_AesEncrypt_Base64(string Data, string Key)
  22. {
  23. if (string.IsNullOrEmpty(Data))
  24. {
  25. return null;
  26. }
  27. if (string.IsNullOrEmpty(Key))
  28. {
  29. return null;
  30. }
  31. var Vector = Key.Substring(0, 16);
  32. Byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
  33. Byte[] bKey = new Byte[32];
  34. Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
  35. Byte[] bVector = new Byte[16];
  36. Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
  37. Byte[] Cryptograph = null; // 加密后的密文
  38. Rijndael Aes = Rijndael.Create();
  39. //add
  40. Aes.Mode = CipherMode.CBC;//兼任其他语言的des
  41. Aes.BlockSize = 128;
  42. Aes.Padding = PaddingMode.PKCS7;
  43. //add end
  44. try
  45. {
  46. // 开辟一块内存流
  47. using (MemoryStream Memory = new MemoryStream())
  48. {
  49. // 把内存流对象包装成加密流对象
  50. using (CryptoStream Encryptor = new CryptoStream(Memory,
  51. Aes.CreateEncryptor(bKey, bVector),
  52. CryptoStreamMode.Write))
  53. {
  54. // 明文数据写入加密流
  55. Encryptor.Write(plainBytes, 0, plainBytes.Length);
  56. Encryptor.FlushFinalBlock();
  57.  
  58. Cryptograph = Memory.ToArray();
  59. }
  60. }
  61. }
  62. catch
  63. {
  64. Cryptograph = null;
  65. }
  66. return Convert.ToBase64String(Cryptograph);
  67. }
  68.  
  69. /// <summary>
  70. /// AES base64 解密算法
  71. /// Key为16位
  72. /// </summary>
  73. /// <param name="Data">需要解密的字符串</param>
  74. /// <param name="Key">Key为16位 密钥</param>
  75. /// <param name="Vector">向量</param>
  76. /// <returns></returns>
  77. public static string RST_AesDecrypt_Base64(string Data, string Key)
  78. {
  79. try
  80. {
  81. if (string.IsNullOrEmpty(Data))
  82. {
  83. return null;
  84. }
  85. if (string.IsNullOrEmpty(Key))
  86. {
  87. return null;
  88. }
  89. var Vector = Key.Substring(0, 16);
  90. Byte[] encryptedBytes = Convert.FromBase64String(Data);
  91. Byte[] bKey = new Byte[32];
  92. Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
  93. Byte[] bVector = new Byte[16];
  94. Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
  95. Byte[] original = null; // 解密后的明文
  96. Rijndael Aes = Rijndael.Create();
  97. //add
  98. Aes.Mode = CipherMode.CBC;//兼任其他语言的des
  99. Aes.BlockSize = 128;
  100. Aes.Padding = PaddingMode.PKCS7;
  101. //add end
  102. try
  103. {
  104. // 开辟一块内存流,存储密文
  105. using (MemoryStream Memory = new MemoryStream(encryptedBytes))
  106. {
  107. //把内存流对象包装成加密流对象
  108. using (CryptoStream Decryptor = new CryptoStream(Memory,
  109. Aes.CreateDecryptor(bKey, bVector),
  110. CryptoStreamMode.Read))
  111. {
  112. // 明文存储区
  113. using (MemoryStream originalMemory = new MemoryStream())
  114. {
  115. Byte[] Buffer = new Byte[1024];
  116. Int32 readBytes = 0;
  117. while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
  118. {
  119. originalMemory.Write(Buffer, 0, readBytes);
  120. }
  121. original = originalMemory.ToArray();
  122. }
  123. }
  124. }
  125. }
  126. catch
  127. {
  128. original = null;
  129. }
  130. return Encoding.UTF8.GetString(original);
  131. }
  132. catch { return null; }
  133. }
  134.  
  135. }
  136. }

BASE64加密解密

  1. public sealed class Base64
  2. {
  3. /// <summary>
  4. /// Base64加密
  5. /// </summary>
  6. /// <param name="codeName">加密采用的编码方式</param>
  7. /// <param name="source">待加密的明文</param>
  8. /// <returns></returns>
  9. public static string EncodeBase64(Encoding encode, string source)
  10. {
  11. byte[] bytes = encode.GetBytes(source);
  12. try
  13. {
  14. encode = Convert.ToBase64String(bytes);
  15. }
  16. catch
  17. {
  18. encode = source;
  19. }
  20. return encode;
  21. }
  22.  
  23. /// <summary>
  24. /// Base64加密,采用utf8编码方式加密
  25. /// </summary>
  26. /// <param name="source">待加密的明文</param>
  27. /// <returns>加密后的字符串</returns>
  28. public static string EncodeBase64(string source)
  29. {
  30. return EncodeBase64(Encoding.UTF8, source);
  31. }
  32.  
  33. /// <summary>
  34. /// Base64解密
  35. /// </summary>
  36. /// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param>
  37. /// <param name="result">待解密的密文</param>
  38. /// <returns>解密后的字符串</returns>
  39. public static string DecodeBase64(Encoding encode, string result)
  40. {
  41. string decode = "";
  42. byte[] bytes = Convert.FromBase64String(result);
  43. try
  44. {
  45. decode = encode.GetString(bytes);
  46. }
  47. catch
  48. {
  49. decode = result;
  50. }
  51. return decode;
  52. }
  53.  
  54. /// <summary>
  55. /// Base64解密,采用utf8编码方式解密
  56. /// </summary>
  57. /// <param name="result">待解密的密文</param>
  58. /// <returns>解密后的字符串</returns>
  59. public static string DecodeBase64(string result)
  60. {
  61. return DecodeBase64(Encoding.UTF8, result);
  62. }
  63. }

DES加密和base64加密的更多相关文章

  1. MD5加密,Base64加密/解密,AES加密/解密

    1.从github上下载GTMBase64-master和AESCrypt-ObjC-master导入工程,如下图所示. 2.使用前的配置及注意事项: (1) 在build phases中的GTMBa ...

  2. js的常见的三种密码加密方式-MD5加密、Base64加密和解密和sha1加密详解总结

    写前端的时候,很多的时候是避免不了注册这一关的,但是一般的注册是没有任何的难度的,无非就是一些简单的获取用户输入的数据,然后进行简单的校验以后调用接口,将数据发送到后端,完成一个简单的注册的流程,那么 ...

  3. 步步为营-33-Md5(32)加密与Base64加密

    说明: 1:直接贴码 using System; using System.Collections.Generic; using System.ComponentModel; using System ...

  4. (iOS)Base64加密和DES加密、以及JAVA和iOS中DES加密统一性问题

    我们在项目中为了安全方面的考虑,通常情况下会选择一种加密方式对需要安全性的文本进行加密,而Base64加密和DES64加密是常用的加密算法.我记得我在前一个项目中使用的就是这两种加密算法的结合:Bas ...

  5. C# 加密解密(DES,3DES,MD5,Base64) 类

    public sealed class EncryptUtils     {         #region Base64加密解密         /// <summary>        ...

  6. Java android DES+Base64加密解密

    服务器与客户端加密解密传输, 中间遇到各种坑,客户端无论用AES还是DES解密时都会出现错误,后来才看到好多人说要用AES/DES加完密后还要BASE64加密,照做时发现android和java的Ba ...

  7. python中常用的base64 md5 aes des crc32等的加密解密

    1.base64 Python内置的base64模块可以实现base64.base32.base16.base85.urlsafe_base64的编码解码,python 3.x通常输入输出都是二进制形 ...

  8. Base64加密解密原理以及代码实现(VC++)

    Base64加密解密原理以及代码实现 转自:http://blog.csdn.net/jacky_dai/article/details/4698461 1. Base64使用A--Z,a--z,0- ...

  9. python 加密解密(base64, AES)

    1. 使用base64 s1 = base64.encodestring('hello world') s2 = base64.decodestring(s1) print s1, s2 结果 1 2 ...

  10. Android网络传输中必用的两个加密算法:MD5 和 RSA 及Base64加密总结

    (1)commons-codec包简介 包含一些通用的编码解码算法.包括一些语音编码器,Hex,Base64.MD5 一.md5.base64.commons-codec包 commons-codec ...

随机推荐

  1. @Scheduled cron 定时任务表达式含义,及* ?的区别

    好多网友对@Scheduled cron表达式含义做了阐述,个人认为很多对于 * ?的说明不够具体也不算准确,借此本文特别对 * ?做一下说明. cron格式:[秒数][分钟][小时][日期][月份] ...

  2. 【SpringBoot】 启动后会调用执行的方法的 (五种方式)

    在 SpringBoot 工程 启动后, 会调用执行方法的五种方式: 亲自测试, 按照执行顺序如下: 第一种: @Component public class SpringContext1 { @Po ...

  3. 体验函数计算 FC 3.0,写测评赢取索尼头戴式耳机

    11月1日云栖大会,函数计算3.0全新升级,相对函数计算2.0,3.0版本突出易用性.高弹性,并且可以和更多阿里云服务无缝集成.业内首发神龙 Serverless GPU 架构,冷启动大幅优化,全链路 ...

  4. mybatisplus 查询结果排除某字段实现

    数据有Test表,表里有id,name,ip_address,last_time四个字段 通常查询写法,返回结果会把id,name,ip_address,last_time四个字段都返回 public ...

  5. C#设计模式18——迭代器模式的写法

    是什么: 迭代器模式是一种行为型设计模式,它允许客户端通过一种统一的方式遍历集合对象中的元素,而无需暴露集合对象的内部结构. 为什么: 使用迭代器模式可以使得客户端程序与集合对象解耦,从而可以更加灵活 ...

  6. C#使用正则表达式检查字符串中重复出现的词

    private void button1_Click(object sender, EventArgs e) { MatchCollection matches =//使用正则表达式查找重复出现单词的 ...

  7. Kubernetes APIServer 最佳实践

    1. kubernetes 整体架构 kubernetes 由 master 节点和工作节点组成.其中,master 节点的组件有 APIServer,scheduler 和 controller-m ...

  8. 传说中 PUE 预测精度高达 0.005 的工作

    杨震, 赵静洲, 林依挺 等. 数据中心 PUE 能效优化的机器学习方法. 系统工程理论与实践, 2022, 42(3): 811-832 省流: 这是 2020 年的论文,用神经网络进行了认真的 P ...

  9. Git | git branch 分支操作

    假设我们已经有了稳定的代码,现在我想整一些花活.比较安全的一个方式是,在新的分支上整活. 新建 vga 分支:git branch vga,然后切换到 vga 分支:git switch vga,或者 ...

  10. SV OOP-2

    静态变量 继承性(Inheritance) 抽象类和虚方法virtual methods 多态(Ploymorphism) 通过基类的变量可以使用子类的对象 基类中定义的virtual functio ...