Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RSA
- {
- /* RSA Encrypt&Decrypt by Eddy^CZ 2018
- * Author: Eddy^CZ 2018
- * Date: 18.12.2018 (23:54)
- */
- /// <summary>
- /// Modded RSA Encryption by Eddy^CZ 2018
- /// </summary>
- /// <param name="data">Input number in Int32</param>
- /// <param name="modulus">RSA</param>
- /// <param name="exponent">RSA</param>
- /// <param name="p">RSA</param>
- /// <param name="q">RSA</param>
- /// <param name="dp">RSA</param>
- /// <param name="dq">RSA</param>
- /// <param name="inverseq">RSA</param>
- /// <param name="d">RSA</param>
- /// <returns>Return data in HEX</returns>
- public string Encrypt(int data , string modulus, string exponent, string p , string q, string dp, string dq, string inverseq, string d)
- {
- /* Input data */
- byte[] data_b = BitConverter.GetBytes(data);
- /* ------------------------------------------------ */
- /* Get RSA Parameters from arguments */
- RSAParameters rsa_params = new RSAParameters();
- rsa_params.Modulus = Encoding.Default.GetBytes(modulus);
- rsa_params.Exponent = Encoding.Default.GetBytes(exponent);
- rsa_params.P = Encoding.Default.GetBytes(p);
- rsa_params.Q = Encoding.Default.GetBytes(q);
- rsa_params.DP = Encoding.Default.GetBytes(dp);
- rsa_params.DQ = Encoding.Default.GetBytes(dq);
- rsa_params.InverseQ = Encoding.Default.GetBytes(inverseq);
- rsa_params.D = Encoding.Default.GetBytes(d);
- /* -----------------------------------------------------------*/
- /* Create instance of RSA and import parameters */
- RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
- rsa.ImportParameters(rsa_params);
- /* ---------------------------------------------------------------- */
- /* RSA Encrypt */
- byte[] data_e = rsa.Encrypt(data_b, false);
- /* ---------------------------------------------- */
- /* Encrypted byte[] to HEX */
- StringBuilder result = new StringBuilder(data_e.Length * 2);
- foreach (byte b in data_e)
- {
- result.AppendFormat("{0:x2}", b);
- }
- return result.ToString();
- /* -------------------------------------------------------- */
- }
- /// <summary>
- /// Modded RSA Decryption by Eddy^CZ 2018
- /// </summary>
- /// <param name="data">RSA</param>
- /// <param name="modulus">RSA</param>
- /// <param name="exponent">RSA</param>
- /// <param name="p">RSA</param>
- /// <param name="q">RSA</param>
- /// <param name="dp">RSA</param>
- /// <param name="dq">RSA</param>
- /// <param name="inverseq">RSA</param>
- /// <param name="d">RSA</param>
- /// <returns>Return decrypted data as Int32</returns>
- public int Decrypt(string data, string modulus, string exponent, string p, string q, string dp, string dq, string inverseq, string d)
- {
- /* Input HEX to byte[] */
- int len = data.Length;
- byte[] bytes = new byte[len / 2];
- for (int i = 0; i < len; i += 2)
- {
- bytes[i / 2] = Convert.ToByte(data.Substring(i, 2), 16);
- }
- /* ---------------------------------------------------------- */
- /* Get RSA Parameters from arguments */
- RSAParameters rsa_params = new RSAParameters();
- rsa_params.Modulus = Encoding.Default.GetBytes(modulus);
- rsa_params.Exponent = Encoding.Default.GetBytes(exponent);
- rsa_params.P = Encoding.Default.GetBytes(p);
- rsa_params.Q = Encoding.Default.GetBytes(q);
- rsa_params.DP = Encoding.Default.GetBytes(dp);
- rsa_params.DQ = Encoding.Default.GetBytes(dq);
- rsa_params.InverseQ = Encoding.Default.GetBytes(inverseq);
- rsa_params.D = Encoding.Default.GetBytes(d);
- /* -----------------------------------------------------------*/
- /* Create instance of RSA and import parameters */
- RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
- rsa.ImportParameters(rsa_params);
- /* -------------------------------------------------------------*/
- /* RSA Decrypt */
- byte[] decoded = rsa.Decrypt(bytes, false);
- /* ------------------------------------------*/
- /* Result byte[] to HEX */
- int result = BitConverter.ToInt32(decoded, 0);
- return result;
- /* -------------------------------------------------------------*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement