请教一个问题,C#里怎么给一个字符串进行加密?要可以解密的,不是MD5那种不可逆的
如题,就是我要给一个字符串进行加密,但是这个字符串在代码中都不能看到,但是要在代码中用到....最好给个示例,谢谢大家! --------------------编程问答-------------------- 非对称加密算法RSA http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx对称加密算法DES http://msdn.microsoft.com/en-us/library/system.security.cryptography.descryptoserviceprovider(v=vs.110).aspx --------------------编程问答-------------------- 简单点可以用base64编码,c#提供的,要想有点个性化,就自己进行位运算,搞个key啥的,网上代码很多 --------------------编程问答-------------------- 可是不知道咋用啊..有木有详细的例子?怎么定义怎么调用的那种.... --------------------编程问答-------------------- 给个地址把谢谢! --------------------编程问答--------------------
/// <summary>--------------------编程问答-------------------- 打开链接往下翻有例子的 --------------------编程问答-------------------- 加密算法类
/// 加密
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public string Encode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes("12345678");
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes("12345678");
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public string Decode(string data)
{
if ("" == data)
{
return "";
}
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes("12345678");
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes("12345678");
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return "";
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
(ps:了解原理就知道如何用了) --------------------编程问答-------------------- 非对称加密算法过程
(1)乙方生成两把密钥(公钥和私钥)。公钥是公开的,任何人都可以获得,私钥则是保密的。
(2)甲方获取乙方的公钥,然后用它对信息加密。
(3)乙方得到加密后的信息,用私钥解密
class Program--------------------编程问答-------------------- 需要 using System.Security.Cryptography --------------------编程问答--------------------
{
static void Main(string[] args)
{
Class1 c = new Class1();
c.StartDemo();
}
}
public class Class1
{
/// <summary>
/// RSA的加解密过程:
/// 有 rsa1 和 rsa2 两个RSA对象。
/// 现在要 rsa2 发送一段信息给 rsa1, 则先由 rsa1 发送“公钥”给 rsa2
/// rsa2 获取得公钥之后,用来加密要发送的数据内容。
/// rsa1 获取加密后的内容后,用自己的私钥解密,得出原始的数据内容。
/// </summary>
public void StartDemo()
{
RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider();
RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider();
string publickey = rsa1.ToXmlString(false); //导出 rsa1 的公钥
String plaintext = "你好吗?这是用于测试的字符串。"; //原始数据
Console.WriteLine("原始数据是:\n{0}\n", plaintext);
rsa2.FromXmlString(publickey); //rsa2 导入 rsa1 的公钥,用于加密信息
//rsa2开始加密
byte[] cipherbytes = rsa2.Encrypt(Encoding.UTF8.GetBytes(plaintext), false);
/*//////////////////////////////////////////////*/
Console.WriteLine("加密后的数据是:");
for (int i = 0; i < cipherbytes.Length; i++)
{
Console.Write("{0:X2}", cipherbytes[i]);
}
Console.Write("\n");
/*//////////////////////////////////////////////*/
//rsa1开始解密
byte[] palinbytes = rsa1.Decrypt(cipherbytes, false);
Console.WriteLine("解密后的数据是:");
Console.Write(Encoding.UTF8.GetString(palinbytes));
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
string Str = "1234567ABCDEFG";
string EncryptStr = Encrypt(Str);
Console.WriteLine("密码明文:{0} \n加密:{1}\n解密: {2}", Str, EncryptStr, Decode(EncryptStr));
Console.ReadKey();
}
//加密是将明文转成Unicode,每位加1
static string Encrypt(string str)
{
byte[] byteArray = Encoding.Unicode.GetBytes(str);
byte[] tmpArray = new byte[byteArray.Length];
for (int i = 0; i < byteArray.Length; i++)
{
tmpArray[i] = byte.Parse((int.Parse(byteArray[i].ToString()) + 1).ToString());
}
return Encoding.Unicode.GetString(tmpArray);
}
//解密就是相当每位减1
static string Decode(string str)
{
byte[] byteArray = Encoding.Unicode.GetBytes(str);
byte[] tmpArray = new byte[byteArray.Length];
for (int i = 0; i < byteArray.Length; i++)
{
tmpArray[i] = byte.Parse((int.Parse(byteArray[i].ToString()) - 1).ToString());
}
return Encoding.Unicode.GetString(tmpArray);
}
}
最简单的加密 --------------------编程问答--------------------
Base64加密解密
/// <summary>
/// 把字符串加密返回加密后的字符串
/// </summary>
public string CryptIt(string aString)
{
try
{
Byte[] byteInput = (new UnicodeEncoding()).GetBytes(aString);
//具有随机密钥的 DES 实例
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//从此实例创建 DES 加密器
byte[] rijnKey = null;
byte[] rijnIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
string deencryptKey = "Elisoft";
rijnKey = System.Text.Encoding.UTF8.GetBytes(deencryptKey.Substring(0, 8));
ICryptoTransform desEncrypt = des.CreateEncryptor(rijnKey, rijnIV);
//创建使用 des 加密流
MemoryStream mStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(mStream, desEncrypt, CryptoStreamMode.Write);
cryptoStream.Write(byteInput, 0, byteInput.Length);
cryptoStream.FlushFinalBlock();
byte[] byteOutString = mStream.ToArray();
cryptoStream.Close();
return Convert.ToBase64String(byteOutString);
}
catch (System.Exception ex)
{
throw ex;
}
}
/// <summary>
/// 把加密字符串解密,返回解密后的字符串
/// </summary>
public string DecryptIt(string aString)
{
try
{
//从此实例创建 DES 加密器
byte[] rijnKey = null;
byte[] rijnIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
string deencryptKey = "Elisoft";
rijnKey = System.Text.Encoding.UTF8.GetBytes(deencryptKey.Substring(0, 8));
//具有随机密钥的 DES 实例
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//从此 des 实例创建 DES 解密器
ICryptoTransform desDecrypt = des.CreateDecryptor(rijnKey, rijnIV);
//创建加密流集合以便对传入的字节进行读取并执行 des 解密转换
MemoryStream mStream = new MemoryStream();
CryptoStream cryptoStreamDecr = new CryptoStream(mStream, desDecrypt, CryptoStreamMode.Write);
byte[] byteInstring = Convert.FromBase64String(aString);
cryptoStreamDecr.Write(byteInstring, 0, byteInstring.Length);
cryptoStreamDecr.FlushFinalBlock();
cryptoStreamDecr.Close();
byte[] byteOutString = mStream.ToArray();
return (new UnicodeEncoding()).GetString(byteOutString);
}
catch (System.Exception ex)
{
throw ex;
}
}
补充:.NET技术 , C#