C#蒸汽网络请求
我是初学者,在尝试登录Steam时遇到了一个问题:C#蒸汽网络请求,c#,web,rsa,steam,C#,Web,Rsa,Steam,我是初学者,在尝试登录Steam时遇到了一个问题: public Steam(string password, string login) { Password = password; Login = login; _client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like
public Steam(string password, string login)
{
Password = password;
Login = login;
_client.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
_rsa = new RSACryptoServiceProvider();
_rsaParameters = new RSAParameters();
_params = new Dictionary<string, string>();
}
public async Task GetRsa()
{
var request = await _client.GetAsync($"https://steamcommunity.com/login/getrsakey?username=" + Login);
var content = await request.Content.ReadAsStringAsync();
Timestamp = content.Between("timestamp\":\"", "\"", StringComparison.InvariantCulture);
TokenGid = content.Between("token_gid\":\"", "\"", StringComparison.InvariantCulture);
Mod = content.Between("publickey_mod\":\"", "\"", StringComparison.InvariantCulture);
Exp = content.Between("publickey_exp\":\"", "\"", StringComparison.InvariantCulture);
_rsaParameters.Modulus = HexToByte(Mod);
_rsaParameters.Exponent = HexToByte(Exp);
_rsa.ImportParameters(_rsaParameters);
var bytePassword = Encoding.ASCII.GetBytes(Password);
var encodedPassword = _rsa.Encrypt(bytePassword, false);
EncryptedBase64Password = Convert.ToBase64String(encodedPassword);
}
public async Task TryLogin()
{
var unixTimestamp = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
//_params["donotcache"] = unixTimestamp + "000";
_params["username"] = Login;
_params["password"] = EncryptedBase64Password;
_params["twofactorcode"] = "";
_params["emailauth"] = "";
_params["loginfriendlyname"] = "";
_params["captchagid"] = "-1";
_params["captcha_text"] = "";
_params["emailsteamid"] = "";
_params["rsatimestamp"] = Timestamp;
_params["remember_login"] = "false";
var responce2 = await _client.PostAsync($"https://steamcommunity.com/login/dologin/", new FormUrlEncodedContent(_params));
var content2 = await responce2.Content.ReadAsStringAsync();
}
我不明白我的错误在哪里。可能是我的RSA密码错误,或者cookies有问题。您的代码看起来不错。一些需要检查的内容: 1.确保您的用户名和密码正确; 2.调用getRsaKey时,检查是否正确解析publickey\u mod&publickey\u exp 看看我的课
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Numerics;
using System.Threading.Tasks;
using Newtonsoft.Json;
class Web
{
public const string STEAM_COMMUNITY = "http://steamcommunity.com";
public const string STEAM_COMMUNITY_GETRSA = "https://steamcommunity.com/login/getrsakey";
public const string STEAM_COMMUNITY_LOGIN = "https://steamcommunity.com/login/dologin/";
HttpClient m_HttpClient;
CookieContainer m_CookieContainer;
public Web()
{
m_CookieContainer = new CookieContainer();
HttpClientHandler msgHandler = new HttpClientHandler { CookieContainer = m_CookieContainer };
m_HttpClient = new HttpClient(msgHandler);
}
public async Task Login(string pUsername, string pPassword)
{
Console.WriteLine("Steamcommunity Login");
//Get RSA
Dictionary<string, string> data = new Dictionary<string, string>();
var request = await m_HttpClient.GetAsync(STEAM_COMMUNITY_GETRSA + "?username=" + pUsername);
var result = await request.Content.ReadAsStringAsync();
RsaKey rsaKey = JsonConvert.DeserializeObject<RsaKey>(result);
if(!rsaKey.success)
{
Console.WriteLine("Unsuccessfull RSA Key request.");
return;
}
RsaParameters rsaParam = new RsaParameters
{
Exponent = rsaKey.publickey_exp,
Modulus = rsaKey.publickey_mod,
Password = pPassword
};
var encrypted = string.Empty;
while (encrypted.Length < 2 || encrypted.Substring(encrypted.Length - 2) != "==")
{
encrypted = EncryptPassword(rsaParam);
}
data.Add("username", pUsername);
data.Add("password", encrypted);
data.Add("twofactorcode", "");
data.Add("emailauth", "");
data.Add("loginfriendlyname", "");
data.Add("captchagid", "-1");
data.Add("captcha_text", "");
data.Add("emailsteamid", "");
data.Add("rsatimestamp", rsaKey.timestamp);
data.Add("remember_login", "false");
request = await m_HttpClient.PostAsync(STEAM_COMMUNITY_LOGIN, new FormUrlEncodedContent(data));
result = await request.Content.ReadAsStringAsync();
LoginResult loginResult = JsonConvert.DeserializeObject<LoginResult>(result);
if(loginResult.success)
{
IEnumerable<Cookie> responseCookies = m_CookieContainer.GetCookies(new Uri(STEAM_COMMUNITY)).Cast<Cookie>();
foreach(var cookie in responseCookies)
{
Console.WriteLine("Name {0}, {1}", cookie.Name, cookie.Value);
}
Console.WriteLine("Successfully logged in.");
//SendCookies
}
else
{
Console.WriteLine("Couldn't login...");
Console.WriteLine(result);
}
}
private string EncryptPassword(RsaParameters rsaParam)
{
// Convert the public keys to BigIntegers
var modulus = CreateBigInteger(rsaParam.Modulus);
var exponent = CreateBigInteger(rsaParam.Exponent);
// (modulus.ToByteArray().Length - 1) * 8
//modulus has 256 bytes multiplied by 8 bits equals 2048
var encryptedNumber = Pkcs1Pad2(rsaParam.Password, (2048 + 7) >> 3);
// And now, the RSA encryption
encryptedNumber = BigInteger.ModPow(encryptedNumber, exponent, modulus);
//Reverse number and convert to base64
var encryptedString = Convert.ToBase64String(encryptedNumber.ToByteArray().Reverse().ToArray());
return encryptedString;
}
public static BigInteger Pkcs1Pad2(string data, int keySize)
{
if (keySize < data.Length + 11)
return new BigInteger();
var buffer = new byte[256];
var i = data.Length - 1;
while (i >= 0 && keySize > 0)
{
buffer[--keySize] = (byte)data[i--];
}
// Padding, I think
var random = new Random();
buffer[--keySize] = 0;
while (keySize > 2)
{
buffer[--keySize] = (byte)random.Next(1, 256);
//buffer[--keySize] = 5;
}
buffer[--keySize] = 2;
buffer[--keySize] = 0;
Array.Reverse(buffer);
return new BigInteger(buffer);
}
public static BigInteger CreateBigInteger(string hex)
{
return BigInteger.Parse("00" + hex, NumberStyles.AllowHexSpecifier);
}
}
public class LoginResult
{
public bool success;
public bool emailauth_needed;
public bool captcha_needed;
public string message;
public string captcha_gid;
public string emailsteamid;
}
public class RsaParameters
{
public string Exponent;
public string Modulus;
public string Password;
}
public class RsaKey
{
public bool success;
public string publickey_mod;
public string publickey_exp;
public string timestamp;
}
使用系统;
使用System.Collections.Generic;
利用制度全球化;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用系统数字;
使用System.Threading.Tasks;
使用Newtonsoft.Json;
班级网页
{
公共const字符串STEAM_COMMUNITY=”http://steamcommunity.com";
public const string STEAM\u COMMUNITY\u GETRSA=”https://steamcommunity.com/login/getrsakey";
public const string STEAM\u COMMUNITY\u LOGIN=”https://steamcommunity.com/login/dologin/";
HttpClient m_HttpClient;
CookieContainer m_CookieContainer;
公共网站()
{
m_CookieContainer=新CookieContainer();
HttpClientHandler msgHandler=新的HttpClientHandler{CookieContainer=m_CookieContainer};
m_HttpClient=新的HttpClient(msgHandler);
}
公共异步任务登录(字符串pUsername、字符串pPassword)
{
Console.WriteLine(“Steamcommunity登录”);
//获取RSA
字典数据=新字典();
var request=wait m_HttpClient.GetAsync(STEAM_COMMUNITY_GETRSA+“?username=“+pUsername);
var result=wait request.Content.ReadAsStringAsync();
RsaKey RsaKey=JsonConvert.DeserializeObject(结果);
如果(!rsaKey.success)
{
Console.WriteLine(“RSA密钥请求未成功”);
返回;
}
RsaParameters rsaParam=新的RsaParameters
{
指数=rsaKey.publickey\u exp,
模数=rsaKey.publickey_mod,
密码=pPassword
};
var encrypted=string.Empty;
while(encrypted.Length<2 | | encrypted.Substring(encrypted.Length-2)!=“=”)
{
加密=加密密码(rsaparm);
}
数据。添加(“用户名”,pUsername);
数据。添加(“密码”,加密);
添加(“twofactorcode”和“);
data.Add(“emailauth”,即“”);
data.Add(“loginfriendlyname”,”);
数据。添加(“captchagid”、“-1”);
添加(“验证码文本”和“);
data.Add(“emailsteamid”,即“);
添加(“rsatimstamp”,rsaKey.timestamp);
添加(“记住登录”、“错误”);
request=wait m_HttpClient.PostAsync(STEAM_COMMUNITY_登录,新表单urlencodedcontent(数据));
结果=wait request.Content.ReadAsStringAsync();
LoginResult LoginResult=JsonConvert.DeserializeObject(结果);
if(loginResult.success)
{
IEnumerable responseCookies=m_CookieContainer.GetCookies(新Uri(STEAM_社区)).Cast();
foreach(响应cookie中的var cookie)
{
WriteLine(“Name{0},{1}”,cookie.Name,cookie.Value);
}
Console.WriteLine(“成功登录”);
//发送饼干
}
其他的
{
Console.WriteLine(“无法登录…”);
控制台写入线(结果);
}
}
私有字符串加密密码(RsaParameters rsaParam)
{
//将公钥转换为大整数
var模数=CreateBigInteger(rsParam.Module);
var指数=CreateBigInteger(rsParam.exponent);
//(模数.ToByteArray().长度-1)*8
//模数为256字节乘以8位等于2048
var encryptedNumber=Pkcs1Pad2(rsparam.Password,(2048+7)>>3);
//现在,RSA加密
encryptedNumber=BigInteger.ModPow(encryptedNumber,指数,模数);
//反向编号并转换为base64
var encryptedString=Convert.ToBase64String(encryptedNumber.ToByteArray().Reverse().ToArray());
返回encryptedString;
}
公共静态BigInteger Pkcs1Pad2(字符串数据,int-keySize)
{
如果(键大小<数据长度+11)
返回新的BigInteger();
var buffer=新字节[256];
var i=数据长度-1;
而(i>=0&&keySize>0)
{
缓冲区[--keySize]=(字节)数据[i--];
}
//我想是填充
var random=新的random();
缓冲区[--keySize]=0;
while(键大小>2)
{
缓冲区[--keySize]=(字节)随机。下一步(1256);
//缓冲区[--keySize]=5;
}
缓冲区[--keySize]=2;
缓冲区[--keySize]=0;
数组。反向(缓冲区);
返回新的BigInteger(缓冲区);
}
公共静态BigInteger CreateBigInteger(字符串十六进制)
{
返回BigInteger.Parse(“00”+十六进制,NumberStyles.AllowHexSpecifier);
}
}
公共类登录结果
{
公共事业的成功;
需要公共文件电子邮件授权;
需要公共bool验证码;
公共字符串消息;
公共字符串验证码;
公共字符串ID;
}
公共类参数
{
公共字符串指数;
公共字符串模;
公共字符串密码;
}
公共类密钥
{
公共事业的成功;
公共字符串publickey_mod;
公共字符串公钥\u exp;
公共字符串时间戳;
}
希望能有帮助
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Numerics;
using System.Threading.Tasks;
using Newtonsoft.Json;
class Web
{
public const string STEAM_COMMUNITY = "http://steamcommunity.com";
public const string STEAM_COMMUNITY_GETRSA = "https://steamcommunity.com/login/getrsakey";
public const string STEAM_COMMUNITY_LOGIN = "https://steamcommunity.com/login/dologin/";
HttpClient m_HttpClient;
CookieContainer m_CookieContainer;
public Web()
{
m_CookieContainer = new CookieContainer();
HttpClientHandler msgHandler = new HttpClientHandler { CookieContainer = m_CookieContainer };
m_HttpClient = new HttpClient(msgHandler);
}
public async Task Login(string pUsername, string pPassword)
{
Console.WriteLine("Steamcommunity Login");
//Get RSA
Dictionary<string, string> data = new Dictionary<string, string>();
var request = await m_HttpClient.GetAsync(STEAM_COMMUNITY_GETRSA + "?username=" + pUsername);
var result = await request.Content.ReadAsStringAsync();
RsaKey rsaKey = JsonConvert.DeserializeObject<RsaKey>(result);
if(!rsaKey.success)
{
Console.WriteLine("Unsuccessfull RSA Key request.");
return;
}
RsaParameters rsaParam = new RsaParameters
{
Exponent = rsaKey.publickey_exp,
Modulus = rsaKey.publickey_mod,
Password = pPassword
};
var encrypted = string.Empty;
while (encrypted.Length < 2 || encrypted.Substring(encrypted.Length - 2) != "==")
{
encrypted = EncryptPassword(rsaParam);
}
data.Add("username", pUsername);
data.Add("password", encrypted);
data.Add("twofactorcode", "");
data.Add("emailauth", "");
data.Add("loginfriendlyname", "");
data.Add("captchagid", "-1");
data.Add("captcha_text", "");
data.Add("emailsteamid", "");
data.Add("rsatimestamp", rsaKey.timestamp);
data.Add("remember_login", "false");
request = await m_HttpClient.PostAsync(STEAM_COMMUNITY_LOGIN, new FormUrlEncodedContent(data));
result = await request.Content.ReadAsStringAsync();
LoginResult loginResult = JsonConvert.DeserializeObject<LoginResult>(result);
if(loginResult.success)
{
IEnumerable<Cookie> responseCookies = m_CookieContainer.GetCookies(new Uri(STEAM_COMMUNITY)).Cast<Cookie>();
foreach(var cookie in responseCookies)
{
Console.WriteLine("Name {0}, {1}", cookie.Name, cookie.Value);
}
Console.WriteLine("Successfully logged in.");
//SendCookies
}
else
{
Console.WriteLine("Couldn't login...");
Console.WriteLine(result);
}
}
private string EncryptPassword(RsaParameters rsaParam)
{
// Convert the public keys to BigIntegers
var modulus = CreateBigInteger(rsaParam.Modulus);
var exponent = CreateBigInteger(rsaParam.Exponent);
// (modulus.ToByteArray().Length - 1) * 8
//modulus has 256 bytes multiplied by 8 bits equals 2048
var encryptedNumber = Pkcs1Pad2(rsaParam.Password, (2048 + 7) >> 3);
// And now, the RSA encryption
encryptedNumber = BigInteger.ModPow(encryptedNumber, exponent, modulus);
//Reverse number and convert to base64
var encryptedString = Convert.ToBase64String(encryptedNumber.ToByteArray().Reverse().ToArray());
return encryptedString;
}
public static BigInteger Pkcs1Pad2(string data, int keySize)
{
if (keySize < data.Length + 11)
return new BigInteger();
var buffer = new byte[256];
var i = data.Length - 1;
while (i >= 0 && keySize > 0)
{
buffer[--keySize] = (byte)data[i--];
}
// Padding, I think
var random = new Random();
buffer[--keySize] = 0;
while (keySize > 2)
{
buffer[--keySize] = (byte)random.Next(1, 256);
//buffer[--keySize] = 5;
}
buffer[--keySize] = 2;
buffer[--keySize] = 0;
Array.Reverse(buffer);
return new BigInteger(buffer);
}
public static BigInteger CreateBigInteger(string hex)
{
return BigInteger.Parse("00" + hex, NumberStyles.AllowHexSpecifier);
}
}
public class LoginResult
{
public bool success;
public bool emailauth_needed;
public bool captcha_needed;
public string message;
public string captcha_gid;
public string emailsteamid;
}
public class RsaParameters
{
public string Exponent;
public string Modulus;
public string Password;
}
public class RsaKey
{
public bool success;
public string publickey_mod;
public string publickey_exp;
public string timestamp;
}