Advertisement
EddyCZ

RSA-C#-Eddy^CZ

Dec 18th, 2018
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1.  class RSA
  2.  {
  3.  
  4.   /* RSA Encrypt&Decrypt by Eddy^CZ 2018
  5.    * Author: Eddy^CZ 2018
  6.    * Date: 18.12.2018 (23:54)
  7.    */
  8.  
  9.   /// <summary>
  10.   /// Modded RSA Encryption by Eddy^CZ 2018
  11.   /// </summary>
  12.   /// <param name="data">Input number in Int32</param>
  13.   /// <param name="modulus">RSA</param>
  14.   /// <param name="exponent">RSA</param>
  15.   /// <param name="p">RSA</param>
  16.   /// <param name="q">RSA</param>
  17.   /// <param name="dp">RSA</param>
  18.   /// <param name="dq">RSA</param>
  19.   /// <param name="inverseq">RSA</param>
  20.   /// <param name="d">RSA</param>
  21.   /// <returns>Return data in HEX</returns>
  22.   public string Encrypt(int data , string modulus, string exponent, string p , string q, string dp, string dq, string inverseq, string d)
  23.   {
  24.    /* Input data */
  25.    byte[] data_b = BitConverter.GetBytes(data);
  26.    /* ------------------------------------------------ */
  27.  
  28.    /* Get RSA Parameters from arguments */
  29.    RSAParameters rsa_params = new RSAParameters();
  30.    rsa_params.Modulus = Encoding.Default.GetBytes(modulus);
  31.    rsa_params.Exponent = Encoding.Default.GetBytes(exponent);
  32.    rsa_params.P = Encoding.Default.GetBytes(p);
  33.    rsa_params.Q = Encoding.Default.GetBytes(q);
  34.    rsa_params.DP = Encoding.Default.GetBytes(dp);
  35.    rsa_params.DQ = Encoding.Default.GetBytes(dq);
  36.    rsa_params.InverseQ = Encoding.Default.GetBytes(inverseq);
  37.    rsa_params.D = Encoding.Default.GetBytes(d);
  38.    /* -----------------------------------------------------------*/
  39.  
  40.    /* Create instance of RSA and import parameters */
  41.    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  42.    rsa.ImportParameters(rsa_params);
  43.    /* ---------------------------------------------------------------- */
  44.  
  45.    /* RSA Encrypt */
  46.    byte[] data_e = rsa.Encrypt(data_b, false);
  47.    /* ---------------------------------------------- */
  48.  
  49.    /* Encrypted byte[] to HEX */
  50.    StringBuilder result = new StringBuilder(data_e.Length * 2);
  51.    foreach (byte b in data_e)
  52.    {
  53.     result.AppendFormat("{0:x2}", b);
  54.    }  
  55.    return result.ToString();
  56.    /* -------------------------------------------------------- */
  57.   }
  58.  
  59.   /// <summary>
  60.   /// Modded RSA Decryption by Eddy^CZ 2018
  61.   /// </summary>
  62.   /// <param name="data">RSA</param>
  63.   /// <param name="modulus">RSA</param>
  64.   /// <param name="exponent">RSA</param>
  65.   /// <param name="p">RSA</param>
  66.   /// <param name="q">RSA</param>
  67.   /// <param name="dp">RSA</param>
  68.   /// <param name="dq">RSA</param>
  69.   /// <param name="inverseq">RSA</param>
  70.   /// <param name="d">RSA</param>
  71.   /// <returns>Return decrypted data as Int32</returns>
  72.   public int Decrypt(string data, string modulus, string exponent, string p, string q, string dp, string dq, string inverseq, string d)
  73.   {
  74.  
  75.    /* Input HEX to byte[] */
  76.    int len = data.Length;
  77.    byte[] bytes = new byte[len / 2];
  78.    for (int i = 0; i < len; i += 2)
  79.    {
  80.     bytes[i / 2] = Convert.ToByte(data.Substring(i, 2), 16);
  81.    }
  82.    /* ---------------------------------------------------------- */
  83.  
  84.    /* Get RSA Parameters from arguments */
  85.    RSAParameters rsa_params = new RSAParameters();
  86.    rsa_params.Modulus = Encoding.Default.GetBytes(modulus);
  87.    rsa_params.Exponent = Encoding.Default.GetBytes(exponent);
  88.    rsa_params.P = Encoding.Default.GetBytes(p);
  89.    rsa_params.Q = Encoding.Default.GetBytes(q);
  90.    rsa_params.DP = Encoding.Default.GetBytes(dp);
  91.    rsa_params.DQ = Encoding.Default.GetBytes(dq);
  92.    rsa_params.InverseQ = Encoding.Default.GetBytes(inverseq);
  93.    rsa_params.D = Encoding.Default.GetBytes(d);
  94.    /* -----------------------------------------------------------*/
  95.  
  96.    /* Create instance of RSA and import parameters */
  97.    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  98.    rsa.ImportParameters(rsa_params);
  99.    /* -------------------------------------------------------------*/
  100.    
  101.    /* RSA Decrypt */
  102.    byte[] decoded = rsa.Decrypt(bytes, false);
  103.    /* ------------------------------------------*/
  104.  
  105.    /* Result byte[] to HEX */
  106.    int result = BitConverter.ToInt32(decoded, 0);
  107.    return result;
  108.    /* -------------------------------------------------------------*/
  109.   }
  110.  
  111.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement