Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSA decipher, using only D, P, Q, U parametrs (GnuPG)

Tags:

c#

rsa

gnupg

I have a RSA private key, which consists of only D, P, Q, U parametrs.

Keys notation http://www.gnupg.org/documentation/manuals/gcrypt/RSA-key-parameters.html

JavaScript does the decryption

<html>
<head>
<SCRIPT SRC="http://eu.static.mega.co.nz/crypto_1.js"></SCRIPT>
<SCRIPT SRC="http://eu.static.mega.co.nz/rsa_1.js"></SCRIPT>
<SCRIPT SRC="http://eu.static.mega.co.nz/hex_1.js"></SCRIPT>
<SCRIPT>
function t()
{
// hex2s - conversion functions: hex to string
// mpi2b - conversion functions: num array <-> multi precision integer (mpi) mpi: 2 octets with length in bits + octets in big endian order
    t = mpi2b(hex2s('080043e8950c95dbc8fdea8fd39dfbc65f4ff31ee4824d8e8ceabdf45cbb928b29863e80154b656f04510e983810889f5d12cee85369743db65b0b54949ff5320c9ed98f3bb6b12a2c26e16a1b8414955034c0ed35b21e9bbc58d8447db85f9c760057656bb3a7b5c80726d287b5bc312525bff7864ba2dc2c627cd50e3d752cd585cb301d327f209974e29ddcd1ac51e99209f1d1f96c9fef5ef810b32a54555b5d7cc69c2a107d717744b5167a8962ca08aaac717768e8235795b21dcd186ba348317e685f07f9a2cac14089242b6edb989c8818054de2f69de34653c3e2d8b70947278f44a8995eb5c3568460dbe8e7a1cf31466556454703b045cef1c61e8d09'));   
    p = mpi2b(hex2s('04009868b3fea798829975abd91d27982462b66aced52f1472f243dadbf9e2e551388bb55750764cfc6ff5b89345285c0faa18924ee392f729d7980a6e1f62bff79584eee9e23d23d53a5034198b44dce9791eb367243a9ec6973eb09b574b65c4156a999fdf48770035c488efc568ad80b5dc73e9ca6c67bf9f09bdb31d175f25e5'));
    q = mpi2b(hex2s('0400d8ff209b2a325f66deee452e11b0306e7bc21dd99cdb8d175c8a08973f8b105b51a7a95b38d09ce72bf81178fbc902b3da791490d80bb0b6a8fa5e1c08636c57be45dbcc7b8c0da95390974f07456da5ca522f126b29b5226047432889482d983eb57679fe4e8d90aef2e06d8dbd0b6fa280b69ffddbd9c16bcd5f5569cef1f1'));
    d = mpi2b(hex2s('07fd1e65b3aa438c9cb5ea6b14590d32945f907d1fb6053541e3c250b5a60b67a477e5bfd2e79d0d4bdb6bde9403ab908723aeee25bfe69665fecc028c2fe4df2c51c8579235cc25f888055dc1da529017c4f4e38b95486b036345bb4c03f75b37c305002916a7f2ffe7a03b1b72bb7836560d556627b7bfff9357cdc73e6ed790f3bf79c6cd0fcd98ef2d96e369e780b3b213d3a5b7df8a373585bda0d22da00f7737ecf59ec0fc68056e65eda05c5940fbb3109d97db8ff135b472117467ec47620736b9e39a70723f33d59dea5a98d18bae8c89aa1be77daf692f5407c8433bd0b85cc13de12e7f8b85d27b147e2e8e337fc1157b0cb76ef90e479b5059ba27f1'));
    u = mpi2b(hex2s('03fe233721f3bda1e05d44de087b67749f4daafe02af79c183b2f41e040b6364a870c0896b84073de0fc1b7b2faee9100a250773282b6d9d935d30c1db2c7ef3daa017519dbb8d94edd32bde9ee0d460cb09d782b88f11552b810a595f6d26d12c38ee74c36d830b9d496947eeb878e6459b120bf747a28761926094797a4a5c9b68'));

    sid = base64urlencode(b2s(RSAdecrypt(t,d,p,q,u)).substr(0,43));

    if (sid=='tajetAbW0qTQGFlwp8iD5lQ0TFV1QUZJZFVvjRX7Xx-bPzYBoau7qog09w')
        console.log("Decrypting work");
}
</script>
</head>
<body onload="t();" />
</html>

I found the BouncyCastle library and I calc all parameters for RSAParameters. But I can not decrypt the data. I get an exception in the method RSACryptoServiceProvider.ImportParameters

using System;
using System.Globalization;
using System.Security.Cryptography;
using Org.BouncyCastle.Math;

namespace testConsole
{
    internal class Program
    {
        private static void Main()
        {
            string p_hex = "04009868b3fea798829975abd91d27982462b66aced52f1472f243dadbf9e2e551388bb55750764cfc6ff5b89345285c0faa18924ee392f729d7980a6e1f62bff79584eee9e23d23d53a5034198b44dce9791eb367243a9ec6973eb09b574b65c4156a999fdf48770035c488efc568ad80b5dc73e9ca6c67bf9f09bdb31d175f25e5";
            string q_hex = "0400d8ff209b2a325f66deee452e11b0306e7bc21dd99cdb8d175c8a08973f8b105b51a7a95b38d09ce72bf81178fbc902b3da791490d80bb0b6a8fa5e1c08636c57be45dbcc7b8c0da95390974f07456da5ca522f126b29b5226047432889482d983eb57679fe4e8d90aef2e06d8dbd0b6fa280b69ffddbd9c16bcd5f5569cef1f1";
            string d_hex = "07fd1e65b3aa438c9cb5ea6b14590d32945f907d1fb6053541e3c250b5a60b67a477e5bfd2e79d0d4bdb6bde9403ab908723aeee25bfe69665fecc028c2fe4df2c51c8579235cc25f888055dc1da529017c4f4e38b95486b036345bb4c03f75b37c305002916a7f2ffe7a03b1b72bb7836560d556627b7bfff9357cdc73e6ed790f3bf79c6cd0fcd98ef2d96e369e780b3b213d3a5b7df8a373585bda0d22da00f7737ecf59ec0fc68056e65eda05c5940fbb3109d97db8ff135b472117467ec47620736b9e39a70723f33d59dea5a98d18bae8c89aa1be77daf692f5407c8433bd0b85cc13de12e7f8b85d27b147e2e8e337fc1157b0cb76ef90e479b5059ba27f1";
            string u_hex = "03fe233721f3bda1e05d44de087b67749f4daafe02af79c183b2f41e040b6364a870c0896b84073de0fc1b7b2faee9100a250773282b6d9d935d30c1db2c7ef3daa017519dbb8d94edd32bde9ee0d460cb09d782b88f11552b810a595f6d26d12c38ee74c36d830b9d496947eeb878e6459b120bf747a28761926094797a4a5c9b68";

            BigInteger p = p_hex.HEX2Bytes().MPInteger2BigInteger();
            BigInteger q = q_hex.HEX2Bytes().MPInteger2BigInteger();
            BigInteger d = d_hex.HEX2Bytes().MPInteger2BigInteger();
            BigInteger u = u_hex.HEX2Bytes().MPInteger2BigInteger(); // not using???

            RSAParameters rsaKeyInfo = new RSAParameters
                                           {
                                               Exponent = new byte[] { 1, 0, 1 },
                                               Modulus = p.Multiply(q).ToByteArray(),
                                               P = p.ToByteArray(),
                                               Q = q.ToByteArray(),
                                               DP = d.Remainder(p.Subtract(BigInteger.One)).ToByteArray(),
                                               DQ = d.Remainder(q.Subtract(BigInteger.One)).ToByteArray(),
                                               InverseQ = q.ModInverse(p).ToByteArray(),
                                               D = d.ToByteArray()
                                           };

            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
                rsa.ImportParameters(rsaKeyInfo); //  !!!!!! CryptographicException   (Bad data)
        }
    }

    public static class Extensions
    {
        public static byte[] HEX2Bytes(this string hex)
        {
            if (hex.Length%2 != 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture,
                                                          "The binary key cannot have an odd number of digits: {0}", hex));
            }

            byte[] hexAsBytes = new byte[hex.Length/2];
            for (int index = 0; index < hexAsBytes.Length; index++)
            {
                string byteValue = hex.Substring(index*2, 2);
                hexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
            }

            return hexAsBytes;
        }

        public static BigInteger MPInteger2BigInteger(this byte[] mpi)
        {            
            int length = (((mpi[0] << 8) | mpi[1]+ 7)/8);
            return new BigInteger(1, mpi, 2, length);
        }
    }
}
like image 668
SkyN Avatar asked Jan 29 '13 11:01

SkyN


People also ask

How do I decode an RSA encryption?

To decrypt a ciphertext C using an RSA public key we simply compute the plaintext M as: M = Cd mod N. Note that both RSA encryption and RSA decryption involve a modular exponentiation and so we would be well advised to use the Repeated Squares Algorithm if we want to make these processes reasonably efficient.

Can GnuPG decrypt PGP?

GPG can open and decrypt files encrypted by PGP or Open PGP, meaning it works well with other products.

What is GnuPG used for?

GnuPG allows you to encrypt and sign your data and communications; it features a versatile key management system, along with access modules for all kinds of public key directories. GnuPG, also known as GPG, is a command line tool with features for easy integration with other applications.

Which formula would you use to compute the ciphertext using RSA?

Represents the plaintext message as a positive integer m with 1<m<n [see note 4]. Computes the ciphertext c=memodn.


1 Answers

TO be honest I'm not too familiar with C#; but regarding BigInteger.One I'd try with:

RSAParameters rsaKeyInfo = new RSAParameters
                                           {
                                               Exponent = new byte[] { 1, 0, 1 },
                                               Modulus = p.Multiply(q).ToByteArray(),
                                               P = p.ToByteArray(),
                                               Q = q.ToByteArray(),
                                               DP = d.Remainder(p.Subtract(BigInteger(1))).ToByteArray(),
                                               DQ = d.Remainder(q.Subtract(BigInteger(1))).ToByteArray(),
                                               InverseQ = q.ModInverse(p).ToByteArray(),
                                               D = d.ToByteArray()
                                           };

and of course not without adding

using System.Numerics
like image 165
itsid Avatar answered Nov 05 '22 18:11

itsid