AES可逆加密算法

分享一段前段时间看到的AES可逆加密算法。

除去常见的MD5等加密方式,如果想要使用一些更加隐蔽的加密方式,则可以使用AES的RijndaelManaged加密算法。

关于加密,有很多复杂的算法,今天只跟大家分享一段摘取的结合动态密钥的对称AES RijndaelManaged加密解密算法,如果大家有兴趣了解更多,我们可以一起深入研究……

static void Main(string[] args)

{

string key = "8H[=}[email protected](";

CryptoHelper helper = new CryptoHelper(key);

if (args.Length == 2 && args[0] == "-e")

{

Console.WriteLine("Encrypt password: {0}", args[1]);

string a = helper.Encrypt(args[1]);

Console.WriteLine("Encrypt result is: {0}",a);

}

else if (args.Length == 2 && args[0] == "-d")

{

Console.WriteLine("Decrypt password: {0}", args[1]);

string a = helper.Decrypt(args[1]);

Console.WriteLine("Decrypt result is: {0}", a);

}

}

 

public class CryptoHelper

{

private const string _DefaultIntializationVector = "%[email protected])5:-";

private RijndaelManaged _crypto;

private static Encoding _encoding = Encoding.ASCII;

private CryptoHelper()

{

}

/// <summary>

/// RijndaelManaged (AES)

/// </summary>

/// <param name="key">the key for encrypt</param>

public CryptoHelper(string key)

{

this._crypto = new RijndaelManaged();

this._crypto.Key = CryptoHelper._encoding.GetBytes(key);

this._crypto.IV = CryptoHelper._encoding.GetBytes("%[email protected])5:-");

}

/// <summary>

/// encrypt the string

/// </summary>

/// <param name="s">encrypt string</param>

/// <returns>encrypt result</returns>

public string Encrypt(string s)

{

byte[] bytes = CryptoHelper._encoding.GetBytes(s);

MemoryStream memoryStream = new MemoryStream();

CryptoStream cryptoStream = new CryptoStream(memoryStream, this._crypto.CreateEncryptor(), CryptoStreamMode.Write);

cryptoStream.Write(bytes, 0, bytes.Length);

cryptoStream.Close();

memoryStream.Close();

byte[] array = memoryStream.ToArray();

if (array == null || array.Length == 0)

{

return "";

}

StringBuilder stringBuilder = new StringBuilder();

byte[] array2 = array;

for (int i = 0; i < array2.Length; i++)

{

byte b = array2[i];

stringBuilder.Append(string.Format("{0:X2}", b));

}

return stringBuilder.ToString();

}

/// <summary>

/// decrypt string

/// </summary>

/// <param name="hexValue"></param>

/// <returns></returns>

public string Decrypt(string hexValue)

{

byte[] array;

if (hexValue == null || hexValue.Length == 0)

{

array = null;

}

else

{

int num = Convert.ToInt32(hexValue.Length / 2);

array = new byte[num];

for (int i = 0; i <= num - 1; i++)

{

array[i] = Convert.ToByte(hexValue.Substring(i * 2, 2), 16);

}

}

MemoryStream stream = new MemoryStream(array, 0, array.Length);

byte[] array2 = new byte[array.Length - 1];

CryptoStream cryptoStream = new CryptoStream(stream, this._crypto.CreateDecryptor(), CryptoStreamMode.Read);

try

{

cryptoStream.Read(array2, 0, array.Length - 1);

}

catch (CryptographicException inner)

{

throw new CryptographicException("Unable to decrypt data. The provided key may be invalid.", inner);

}

finally

{

cryptoStream.Close();

}

int num2 = Array.IndexOf<byte>(array2, 0);

if (num2 >= 0)

{

return CryptoHelper._encoding.GetString(array2, 0, num2);

}

return CryptoHelper._encoding.GetString(array2);

}

}

时间: 2024-08-09 20:13:15

AES可逆加密算法的相关文章

微信小程序及各种平台对接常用可逆加密算法aes256

不同程序之间经常会交换数据,我们经常采用的套路是: 假设要传输的信息是json,我们假设其为json_data,通过http传递信息为 json_data_encode=json_data&sign=md5(json_data+key) 接收方通过验证sign就知道内容有没有被篡改. 但是,这样json_data作为明码传送会让我们不太开心,所以今天的我们要介绍的aes256出马了,他是一强度很高的可逆加密算法! aes256加密出来的内容是二进制的,不好通过http协议传输,所以我们再配合上b

RSA与AES混合加密算法的实现

使用 PVRTC 压缩格式创建纹理(Creating textures in the PVRTC compression format) 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 有关该篇

DES可逆加密算法:可自定义密钥

1 package com.time1.dao; 2 import java.security.Key; 3 import java.security.Security; 4 5 import javax.crypto.Cipher; 6 7 /** 8 * @Title: DES可逆加密算法:可自定义密钥 9 * @Description: 10 * @Author:zhoupk 11 * @Create:Jan 27, 2011 3:02:18 PM 12 * @Version:1.1 13

Java 加密 AES 对称加密算法

题目链接:https://oj.leetcode.com/problems/set-matrix-zeroes/ Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 一个个找肯定会超时,我们可以分别用一个行向量和一个列向量进行维护.这样O(m*n) 能出来 class Solution { public: void setZeroes(vector<vector

AES 可逆性加密算法

AES 可逆性加密算法 package com.lock.demo.service; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; /** * @author niunafei * @function

AES对称加密算法原理

原著:James McCaffrey 翻译:小刀人 原文出处:MSDN Magazine November 2003 (Encrypt It) 本文的代码下载:msdnmag200311AES.exe (143KB) 本文假设你熟悉 C# 和 位(bit)操作. 摘要 AES(The Advanced Encryption Standard)是美国国家标准与技术研究所用于加密电子数据的规范.它被预期能成为人们公认的加密包括金融.电信和政府数字信息的方法.本文展示了AES的概貌并解析了它使用的算法

破解wifi加密的数据帧,aes/tkip加密算法简要分析

一. 抓取空气中传播的wifi信号 (1).使用wireshark抓包 尝试之后发现,wireshare接受的数据是系统过滤时候的数据,系统接受外界信息的时候,第一步就是确实是不是本网卡的的信息,对照mac地址,如果不相同则丢弃该数据帧,得出结论wireshark不能控制无线网卡抓取空气中的wifi信号. (2).使用commview抓包 CommView是一个用来分析WiFi无线网络的软件,在工作之前commview能够做什么? 1.捕获xxxxxxxxxxxxx(包括:包) 2.统计xxxx

16进制可逆加密算法

16进制可逆操作类: public static class Hex16 { /// <summary> /// 作用:将字符串内容转化为16进制数据编码,其逆过程是Decode /// 参数说明: /// strEncode 需要转化的原始字符串 /// 转换的过程是直接把字符转换成Unicode字符,比如数字"3"-->0033,汉字"我"-->U+6211 /// 函数decode的过程是encode的逆过程. /// </sum

AES 对称加密算法 加密\解密实例

package com.soufun.com; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Date; import javax.crypto.BadPaddingExce