Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define ENCRYPTION
- using UnityEngine;
- using System.Collections;
- using System.Security.Cryptography;
- using System.Text;
- using System.IO;
- using ULinq;
- using JsonFx.Json;
- using System.Collections.Generic;
- public class SecurityUtil
- {
- private static int IntKey;
- private static MD5 md5;
- static SecurityUtil()
- {
- System.Random rnd = new System.Random();
- IntKey = Mathf.Abs( rnd.Next() );
- md5 = MD5.Create();
- }
- public static int Decrypt( int v )
- {
- return v ^ IntKey;
- }
- public static int Encrypt( int v )
- {
- return v ^ IntKey;
- }
- public static string Sign( string source )
- {
- return Digest( source + Backend.Instance.Session.digest );
- }
- public static bool CheckSignature( string source, string targetSignature )
- {
- return Digest( source + Backend.Instance.Session.digest ).Equals( targetSignature );
- }
- public static string CreateSessionHandShake( SessionModel session )
- {
- StringBuilder sb = new StringBuilder();
- sb.Append( session.p );
- sb.Append( "|" );
- sb.Append( session.g );
- sb.Append( "|" );
- sb.Append( session.pub_key );
- DiffieHellman.DiffieHellman dh = new DiffieHellman.DiffieHellman( 256 ).GenerateResponse( sb.ToString() );
- session.digest = System.Convert.ToBase64String( dh.Key, System.Base64FormattingOptions.None );
- string publicKey = dh.ToString();
- dh.Dispose();
- return publicKey;
- }
- public static string RsaEncrypt( string text )
- {
- byte[] publicRsaKey = {
- 157, 192, 109, 109, 93, 251, 115, 241, 30, 112, 202, 107, 177, 17, 125, 183, 68, 238, 168,
- 206, 246, 64, 252, 2, 190, 71, 53, 68, 198, 163, 37, 76, 253, 215, 162, 17, 55, 158, 112,
- 128, 12, 221, 215, 179, 245, 34, 254, 129, 134, 206, 52, 19, 212, 142, 66, 131, 116, 48, 122,
- 119, 123, 8, 221, 231, 132, 38, 163, 205, 144, 105, 53, 143, 249, 161, 236, 169, 217, 105, 151,
- 142, 15, 120, 149, 150, 54, 48, 144, 78, 128, 33, 76, 133, 238, 88, 236, 147, 192, 240, 159,
- 248, 49, 121, 109, 9, 35, 145, 239, 73, 251, 93, 156, 180, 84, 22, 62, 131, 126, 146, 73,
- 73, 119, 34, 201, 25, 220, 48, 34, 106, 188, 169, 76, 54, 33, 0, 167, 190, 250, 5, 74,
- 25, 128, 189, 216, 114, 179, 81, 96, 154, 146, 225, 22, 141, 126, 63, 211, 251, 151, 37, 64,
- 78, 48, 81, 246, 63, 53, 4, 201, 7, 138, 3, 67, 235, 142, 177, 163, 119, 142, 150, 56,
- 220, 15, 177, 124, 111, 243, 54, 128, 59, 160, 177, 247, 134, 188, 216, 73, 80, 18, 97, 191,
- 17, 182, 12, 1, 43, 130, 170, 24, 79, 101, 140, 129, 185, 212, 137, 65, 223, 9, 145, 179,
- 198, 47, 102, 239, 148, 22, 208, 169, 133, 166, 223, 14, 6, 230, 41, 165, 116, 93, 73, 191,
- 90, 5, 146, 78, 85, 115, 224, 44, 110, 190, 189, 185, 204, 164, 80, 66, 63
- };
- byte[] exponent = { 1, 0, 1 };
- string encryptedText = "";
- using( RSACryptoServiceProvider rsa = new RSACryptoServiceProvider() )
- {
- RSAParameters rsaKeyInfo = new RSAParameters();
- rsaKeyInfo.Modulus = publicRsaKey;
- rsaKeyInfo.Exponent = exponent;
- rsa.ImportParameters( rsaKeyInfo );
- byte[] textData = UTF8Encoding.UTF8.GetBytes( text );
- byte[] encryptedData = rsa.Encrypt( textData, false );
- encryptedText = System.Convert.ToBase64String( encryptedData, System.Base64FormattingOptions.None );
- var temp = encryptedText.Replace( '+', '-' ).Replace( '/', '_' ).TrimEnd( '=' );
- encryptedText = temp;
- }
- return encryptedText;
- }
- public static WWW SecureWWW( string url, string action )
- {
- #if ENCRYPTION
- action = RsaEncrypt( action );
- #endif
- return new WWW( url + action );
- }
- public static WWW SecureWWW( string url, string action, Dictionary<string, object> form )
- {
- var wwwForm = new WWWForm();
- foreach( var pair in form )
- {
- var key = pair.Key;
- var val = pair.Value;
- if( val is string )
- wwwForm.AddField( key, val as string );
- else if( val is int )
- wwwForm.AddField( key, ( int ) val );
- else
- wwwForm.AddField( key, val.ToString() );
- }
- #if ENCRYPTION
- action = RsaEncrypt( action );
- #endif
- return new WWW( url + action, wwwForm );
- }
- public static string Digest( string source )
- {
- byte[] hash = md5.ComputeHash( Encoding.ASCII.GetBytes( source ) );
- return System.Convert.ToBase64String( hash, System.Base64FormattingOptions.None );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement