新手请教,如何加密URL的ID?

hfwang2008 2010-08-16 09:32:56
我还没学过ASP.NET in C#,现在上级给我个任务让我试着做它。
在.NET加密一个ID和它的内容, 重新定向到一个PHP页面并解密它的内容。
上级给我写了个框架,不过我还不知道应该放哪些功能进去,在???处。
非常感谢启蒙!!!

框架见下:
test.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace nettest
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Console.WriteLine("Hello World");

??? // Get id from URL

??? // Encrypt id

??? // Redirect to http://phptest.tbpc13.com/test.php?id=encrypted
}
}
}


test.aspx
<%@ Page Title="About Us" Language="C#" AutoEventWireup="true"
CodeBehind="Test.aspx.cs" Inherits="nettest.Test" %>
<html>
<h2>
About
</h2>
<p>
Put content here.
</p>
</html>
...全文
176 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
smilef9453 2010-08-16
  • 打赏
  • 举报
回复
这个是非对称加密的:适用于短字符串,效率低。安全高:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace myHeFeng.frameWork.components.stringEncrypt
{
/// <summary>
/// 提供非对称加码解密字符串的方法和函数,只适用短字符串,效率低
/// </summary>
public sealed class RSACryption
{
/// <summary>
/// RSA 的密钥产生 产生私钥 和公钥
/// </summary>
/// <param name="xmlKeys"></param>
/// <param name="xmlPublicKey"></param>
public static void RSAKey(out string xmlKeys, out string xmlPublicKey)
{
using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
xmlKeys = rsa.ToXmlString(true);
xmlPublicKey = rsa.ToXmlString(false);
rsa.Clear();
}
}

/// <summary>
/// RSA的加密函数 string
/// </summary>
/// <param name="xmlPublicKey"></param>
/// <param name="m_strEncryptString"></param>
/// <returns></returns>
public string RSAEncrypt(string xmlPublicKey, string input)
{

byte[] PlainTextBArray;
byte[] CypherTextBArray;
string Result;
using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider())
{
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray = (new UnicodeEncoding()).GetBytes(input);
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
}
}
/// <summary>
/// RSA的加密函数 byte[]
/// </summary>
/// <param name="xmlPublicKey"></param>
/// <param name="EncryptString"></param>
/// <returns></returns>
public string RSAEncrypt(string xmlPublicKey, byte[] inputByte)
{

byte[] CypherTextBArray;
string Result;
using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider())
{
rsa.FromXmlString(xmlPublicKey);
CypherTextBArray = rsa.Encrypt(inputByte, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
}
}



/// <summary>
/// RSA的解密函数 string
/// </summary>
/// <param name="xmlPrivateKey"></param>
/// <param name="m_strDecryptString"></param>
/// <returns></returns>
public string RSADecrypt(string xmlPrivateKey, string input)
{
byte[] PlainTextBArray;
byte[] DypherTextBArray;
string Result;
using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider())
{
rsa.FromXmlString(xmlPrivateKey);
PlainTextBArray = Convert.FromBase64String(input);
DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
}

/// <summary>
/// RSA的解密函数 byte[]
/// </summary>
/// <param name="xmlPrivateKey"></param>
/// <param name="DecryptString"></param>
/// <returns></returns>
public string RSADecrypt(string xmlPrivateKey, byte[] inputByte)
{
byte[] DypherTextBArray;
string Result;
using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider())
{
rsa.FromXmlString(xmlPrivateKey);
DypherTextBArray = rsa.Decrypt(inputByte, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
}



#region RSA数字签名

#region 获取Hash描述表
//获取Hash描述表
public bool GetHash(string m_strSource, ref byte[] HashData)
{
//从字符串中取得Hash描述
byte[] Buffer;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
HashData = MD5.ComputeHash(Buffer);

return true;
}

smilef9453 2010-08-16
  • 打赏
  • 举报
回复
基础类:

namespace myHeFeng.frameWork.components.stringEncrypt
{
public class simple3Des : IDisposable
{
private TripleDESCryptoServiceProvider TripleDes = new TripleDESCryptoServiceProvider();

public simple3Des(string key)
{
TripleDes.Key = TruncateHash(key, TripleDes.KeySize / 8);
TripleDes.IV = TruncateHash("", TripleDes.BlockSize / 8);
}

private byte[] TruncateHash(string key, int length)
{

SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] keyBytes = System.Text.Encoding.Unicode.GetBytes(key);
byte[] hash = sha1.ComputeHash(keyBytes);
hash = (byte[])Microsoft.VisualBasic.CompilerServices.Utils.CopyArray((Array)hash, new byte[length - 1 + 1]);
return hash;
}

public string EncryptData(string plaintext)
{
byte[] plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plaintext);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream encStream = new CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
encStream.FlushFinalBlock();
return System.Convert.ToBase64String(ms.ToArray());
}

public string DecryptData(string encryptedtext)
{
byte[] encryptedBytes = System.Convert.FromBase64String(encryptedtext);
System.IO.MemoryStream ms = new System.IO.MemoryStream();

CryptoStream decStream = new CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
decStream.FlushFinalBlock();
return System.Text.Encoding.Unicode.GetString(ms.ToArray());
}

#region IDisposable 成员

public void Dispose()
{
TripleDes.Clear();
}

#endregion
}
}
smilef9453 2010-08-16
  • 打赏
  • 举报
回复
这个是对称加密的方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myHeFeng.frameWork.components.stringEncrypt
{
/// <summary>
/// 提供对称加码解密字符串的方法和函数
/// </summary>
public class DESEncrypt
{
/// <summary>
/// 系统固定字符串加密的钥匙
/// </summary>
private static readonly string DESEncryptKey = "a";

/// <summary>
/// 使用系统固定KEY的字符串加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string encryptString(string input)
{
return __work(input, string.Empty, true);
}
/// <summary>
/// 使用自定义KEY的字符串加密
/// </summary>
/// <param name="input"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string encryptString(string input, string key)
{
return __work(input, key, true);
}
/// <summary>
/// 使用系统固定KEY的字符串解密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string decryptionString(string input)
{
return __work(input, string.Empty, false);
}
/// <summary>
/// 使用自定义KEY的字符串解密
/// </summary>
/// <param name="input"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string decryptionString(string input, string key)
{
return __work(input, key, false);
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="key"></param>
/// <param name="isEncrypt"></param>
/// <returns></returns>
private static string __work(string input, string key, bool isEncrypt)
{
string _key;
if (string.IsNullOrEmpty(key))
{
_key = DESEncryptKey;
}
else
{
_key = key;
}

using (simple3Des sim = new simple3Des(_key))
{
if (isEncrypt)
{
return sim.EncryptData(input);
}
return sim.DecryptData(input);
}
}

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧