C#的ASP.NET的程序,发现字符串的一个问题:
我的一个函数返回值是string的,比如为"1234",显示在TextBox上是
正常的,但是如果把该值赋给一个string的变量,则变成这样:
"1\02\03\04\0"
这是为什么?该怎么处理?
我的一个函数返回值是string的,比如为"1234",显示在TextBox上是
正常的,但是如果把该值赋给一个string的变量,则变成这样:
"1\02\03\04\0"
这是为什么?该怎么处理?
string tmp, newStr;
for(int i=0;i<srcStr.length; i++)
{
tmp = srcStr[i];
if (tmp != "\0")
newStr += tmp;
}
tmp=srcStr[i].ToString();
这是一个字符串对称加密的小程序,很怪的是字符串显示和实际怎么会不一样?
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace WebApplication1
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
//加密函数,可以不看,我是网上下载的
public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[]
inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
//Write the byte array into the crypto stream
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
//解密函数,可以不用看,我是网上下载的
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//Put the input string into the byte array
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for(int x = 0; x < pToDecrypt.Length / 2;x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
//建立加密对象的密钥和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,
des.CreateDecryptor(),CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0,inputByteArray.Length);
cs.FlushFinalBlock();
//Get the decrypted data back from the memory stream
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder();
return
System.Text.Encoding.Default.GetString(ms.ToArray());
} private void Button1_Click(object sender, System.EventArgs e)
{
TextBox1.Text=Encrypt("123456","database");//用密钥为"database"加密"123456",注意密钥固定为8字节
} private void Button2_Click(object sender, System.EventArgs e)
{
string s="";
string d="";
s=Decrypt(TextBox1.Text,"database");//用密钥为"database"解密字符串,s显示为"1\02\03\04\05\06\0"
if(s=="123456")//显然不会相等
{
d="good";
}
TextBox2.Text=s;//但是显示在TextBox上却是正确的,什么原因?
}
}
}
没有人能够帮我解释一下吗?