public void RSA_Encrypt() { string modulus = "E41855047AC427ABD0480E7DFA8396AAC3FC9167F5C8735519250D5A7C633B36B65065F945DAF0EA4964F314EC6E7A3E52511A5B7FEBE275138706417970D208D55BFDD8B112266F5FDDBA2A3C3FE27D82A575CC0C91733740B175233FBAC43A2EB6121AB2868C5204AA173B50DA9093279370D8E1A72BDD0D064D081E96BB100D9C6878E97EADA7CB931EB04011BE57DF16E10C57EA8DE1263618F1DE84540AF4BA24966AB60F8ADFE1D877B220E9AA414223E2D75B20E64A9D322898052F294A8F9F12B9123B60033A3786F0E0C803D6D7DDF2A5A2C3DCEA75DC3C7B39118FF6FA40E7BE7CED1FA02EE53F9D0B77D7BDCA40CC6C74AC66402F68555B426D1B"; string exponent = "010001"; RSAHelper rsa = new RSAHelper(); rsa.ImportParameters( new RSAParameters { E = exponent.HexToByteArray(), N = modulus.HexToByteArray() } ); string password = "******"; byte[] bytePassword = Encoding.UTF8.GetBytes( password ); byte[] encodedPassword = rsa.Encrypt( bytePassword ); string encryptedBase64Password = Convert.ToBase64String( encodedPassword ); }
public void RSA_EncryptDecrypt() { string modulus = "d2c0a39114f3f9bc6a638f04d9872178c2c2006f5a4c5151e930071df72a30a06434ed37623321323076f39a94d5755a815a7c1cd2066a85789ab5ccf64692126055781a05e09436ab5ad9f61cb0e779ae03902dcfcb213d0a7cc85c7e7cc551a11544a9b8331451a7f5f4d5ad88641c5d6939f4407c7bcc3c279b6d46630ba3"; string exponent = "010001"; string privateKey = "067fc44b840ee603a6703d87d3c17409ca4fbb3db3d628a7d2fe152a1a6625abbc8b59495cf0e0b430846a8cb8cc405b3323fc31d3543952b65e66fed4156709b5b556d3b048294f86045d93aba21974525676c71fe1048905f91d9ee610e10df930e2ffa1c80e973b78bd9a66f107437df3a7fdc21de86a6be0d293f464da69"; RSAHelper rsa = new RSAHelper(); rsa.ImportParameters( new RSAParameters { E = exponent.HexToByteArray(), N = modulus.HexToByteArray(), D = privateKey.HexToByteArray() } ); string password = "******"; byte[] bytePassword = Encoding.UTF8.GetBytes( password ); byte[] encodedPassword = rsa.Encrypt( bytePassword ); string encryptedBase64Password = Convert.ToBase64String( encodedPassword ); byte[] decrypted = rsa.Decrypt( Convert.FromBase64String( encryptedBase64Password ), privateKey.HexToByteArray() ); }
/// <summary> /// Queries Steam API with user credentials and returns a valid access token for use in API calls. /// </summary> /// <param name="username">Username of the user requesting authentication.</param> /// <param name="password">Password for the user requesting authentication.</param> /// <param name="steamGuardAnswer"></param> /// <param name="captchaAnswer"></param> /// <returns>Access token which can then be used with the UserAuthenticator.ForProtectedResource method.</returns> public static SteamAccessRequestResult GetAccessTokenForUser( string username, string password, SteamGuardAnswer steamGuardAnswer = null, CaptchaAnswer captchaAnswer = null ) { RSAValues publicKey = GetRSAKeyValues( username ); // RSA Encryption RSAHelper rsa = new RSAHelper(); rsa.ImportParameters( new RSAParameters { E = publicKey.PublicKeyExponent.HexToByteArray(), N = publicKey.PublicKeyModulus.HexToByteArray() } ); byte[] cipherPassword = rsa.Encrypt( Encoding.UTF8.GetBytes( password ) ); string encodedCipherPassword = Convert.ToBase64String( cipherPassword ); SteamClient client = new SteamClient( "https://steamcommunity.com/" ); SteamRequest request = new SteamRequest( "mobilelogin/dologin", HttpMethod.Post ); request.AddParameter( "username", username, ParameterType.QueryString ); request.AddParameter( "password", encodedCipherPassword, ParameterType.QueryString ); request.AddParameter( "rsatimestamp", publicKey.Timestamp, ParameterType.QueryString ); request.AddParameter( "oauth_client_id", "DE45CD61", ParameterType.QueryString ); request.AddParameter( "oauth_scope", "read_profile write_profile read_client write_client", ParameterType.QueryString ); if( captchaAnswer != null ) { request.AddParameter( "captchagid", captchaAnswer.GID, ParameterType.QueryString ); request.AddParameter( "captcha_text", captchaAnswer.SolutionText, ParameterType.QueryString ); } if( steamGuardAnswer != null ) { request.AddParameter( "emailsteamid", steamGuardAnswer.ID, ParameterType.QueryString ); request.AddParameter( "emailauth", steamGuardAnswer.SolutionText, ParameterType.QueryString ); } ISteamResponse response = client.Execute( request ); if( !response.IsSuccessful ) throw new SteamRequestException( "User authentication failed. Request to procure Steam access token failed (HTTP request not successful).", response ) { IsRequestIssue = true }; SteamTokenResult result; try { result = JsonConvert.DeserializeObject<SteamTokenResult>( response.Content ); } catch( Exception e ) { throw new SteamRequestException( "Unable to deserialize the token response from Steam.", e ) { IsDeserializationIssue = true }; } if( !result.IsSuccessful ){ return new SteamAccessRequestResult { IsSuccessful = false, SteamResponseMessage = result.Message, IsCaptchaNeeded = result.IsCaptchaNeeded, CaptchaURL = ( String.IsNullOrEmpty( result.CaptchaGID ) ) ? null : "https://steamcommunity.com/public/captcha.php?gid=" + result.CaptchaGID, CaptchaGID = ( String.IsNullOrEmpty( result.CaptchaGID ) ) ? null : result.CaptchaGID, IsSteamGuardNeeded = result.IsEmailAuthNeeded, SteamGuardID = ( String.IsNullOrEmpty( result.EmailSteamID ) ) ? null : result.EmailSteamID, SteamGuardEmailDomain = ( String.IsNullOrEmpty( result.EmailDomain ) ) ? null : result.EmailDomain }; } if( result.OAuthParams == null ) throw new SteamRequestException( "Login was successful did the response did not contain expected OAuth access information.", response ); OAuthParameters oauthParams = JsonConvert.DeserializeObject<OAuthParameters>( result.OAuthParams ); SteamUser user = new SteamUser { SteamID = new SteamID( oauthParams.SteamID ), OAuthAccessToken = oauthParams.OAuthToken }; return new SteamAccessRequestResult { IsSuccessful = true, IsLoginComplete = result.IsLoginComplete, User = user }; }
public static string GetAccessTokenForUser( SteamUser user ) { SecureRandom sr = new SecureRandom(); var sessionKey = new byte[32]; sr.NextBytes( sessionKey ); RSAHelper rsa = new RSAHelper(); rsa.ImportParameters( new RSAParameters { N = UniversePublicKeys.GetPublicKey( SteamUniverse.Public ) } ); byte[] encSessionKey = rsa.Encrypt( sessionKey ); byte[] loginKey = Encoding.UTF8.GetBytes( user.AuthCookieLoginKey ); byte[] encLoginKey = AESHelper.Encrypt( loginKey, sessionKey ); SteamClient client = new SteamClient( "http://api.steampowered.com/" ); SteamRequest request = new SteamRequest( "ISteamUserAuth/AuthenticateUser/v0001", HttpMethod.Post ); request.DataFormat = PostDataFormat.FormUrlEncoded; request.AddParameter( "steamid", user.SteamID.ToString(), ParameterType.GetOrPost ); request.AddParameter( "sessionkey", StringFormat.UrlEncode( encSessionKey ), ParameterType.GetOrPost, true ); request.AddParameter( "encrypted_loginkey", StringFormat.UrlEncode( encLoginKey ), ParameterType.GetOrPost, true ); request.AddParameter( "format", "json", ParameterType.GetOrPost ); var response = client.Execute( request ); return null; }