Advertisement
Guest User

Untitled

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