客户端代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Security;
using System.Security.Cryptography;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using demo;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
demo.Service aa = new Service();
string info = this.TextBox1.Text;
demo.UserInfo uu = aa.getSOAPHeader();
System.Security.Cryptography.CspParameters param = new CspParameters();
param.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param);
rsa.FromXmlString(uu.RSAPublicKey);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.GenerateKey();
uu.DESKey = rsa.Encrypt(des.Key, false);
des.GenerateIV();
uu.DESIV = rsa.Encrypt(des.IV, false);
ICryptoTransform ict = des.CreateEncryptor();
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
byte[] input = ascii.GetBytes(info);
uu.name = ict.TransformFinalBlock(input, 0, input.GetLength(0));
info = aa.getUserInfo(uu);
this.Label1.Text = info;
}
}
Web服务端代码
//SOAP头信息
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Security.Cryptography;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// UserInfo 的摘要说明
/// </summary>
public class UserInfo : System.Web.Services.Protocols.SoapHeader
{
public byte[] name;
public string RSAPublicKey;
public byte[] DESKey;
public byte[] DESIV;
public UserInfo()
{
//name = "";
}
}
//web服务
using System;
using System.Web;
using System.Security.Cryptography;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public UserInfo uinfo ;
public Service()
{
RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
uinfo = new UserInfo();
uinfo.RSAPublicKey = rsa.ToXmlString(true);
}
[SoapHeader("uinfo")]
[WebMethod]
public string getUserInfo(UserInfo uinfo)
{
RSACryptoServiceProvider rsa= new RSACryptoServiceProvider();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
rsa.FromXmlString(uinfo.RSAPublicKey);
byte[] _DESKey = rsa.Decrypt(uinfo.DESKey,false);
byte[] _DESIV = rsa.Decrypt(uinfo.DESKey, false);
des.Key = _DESKey;
des.IV = _DESIV;
ICryptoTransform ict = des.CreateDecryptor();
byte[] ret = ict.TransformFinalBlock(uinfo.name ,0,uinfo.name.Length);
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
return ascii.GetString(ret,0,ret.Length);
}
[WebMethod]
public UserInfo getSOAPHeader()
{
return uinfo;
}
}
加密的方式,不用看懂,知道怎么调用方法就行了~~~
对soapheader参数加密 调用web service
加密算法我一直没看过,反正也不用你去写。知道怎么用就ok了!
.net内置的加密
单步调试,并输出相应的数据,相信你可以理解。
单步调试吧。
往VS环境里面一扔,什么都现出原型。。。