# RSA Decryption? [closed]

I am playing a game in my university where you have different puzzles based on cryptography and now in a puzzle named 'rsa', we have 2 files: 'key', 'secret'.

The task for this is to decrypt the secret message using the key, and of course I am suspecting some RSA encryption but I can't find anything to decrypt this and I think I might have not enough information. I don't really know. Anyway, here is the key:

And here is the message:

UPDATE: No, I'm not sure, don't think that the message is encoded in hexa too.

found out that RSA KEY was actually coded in hexa and the RSA key is basically:

• I'm voting to close this question as off-topic because the question is about cryptography and not reverse-engineering. You should ask this at Crypto SE. – perror Dec 30 '18 at 11:05
• @perror man, really? come on, those guys sent me here – C. Cristi Dec 30 '18 at 11:36
• Well, your question has nothing to do with reverse-engineering... But, okay, I'll help you a bit... – perror Dec 30 '18 at 11:49
• Is this game an assignment for class? What textbook is being used for the class? What is the exact description of the game with all information included? Are you the only one working on it? If not, have you discussed this with your classmates or colleagues? Are any examples from the lectures relevant to this challenge? Have you talked to the professor? Even though crypto challenges are off topic here we can help you out, as perror has done, but it will be up to you to do the work and solve it – julian Dec 30 '18 at 16:13
• No, it's not an assignment, it's a game made by students and it's just fun, it's kind of a capture the flag – C. Cristi Dec 30 '18 at 16:15

From here the structure of a private key looks like

``````RSAPrivateKey ::= SEQUENCE {
version           Version,
modulus           INTEGER,  -- n
publicExponent    INTEGER,  -- e
privateExponent   INTEGER,  -- d
prime1            INTEGER,  -- p
prime2            INTEGER,  -- q
exponent1         INTEGER,  -- d mod (p-1)
exponent2         INTEGER,  -- d mod (q-1)
coefficient       INTEGER,  -- (inverse of q) mod p
otherPrimeInfos   OtherPrimeInfos OPTIONAL
}
``````

If analyzed the base64 data in the ASN.1 decoder

``````SEQUENCE (9 elem)
INTEGER 0
INTEGER (2048 bit) 226871413225308095515915826394392101638242887434378121570998515897193…
INTEGER 65537
INTEGER (2048 bit) 194320763995749647278490303169550120288709068819771754524412632427072…
INTEGER (1024 bit) 134355954175703308272730070638862721054431606752282500957493356998331…
INTEGER (1024 bit) 168858473461189648019451140006978906971949575614563598413430805554687…
INTEGER (1023 bit) 501346546129182660757986157052258700182511397092797741873369248728839…
INTEGER (1023 bit) 467538315673092658034539326878959861744205105375874857990160580678909…
INTEGER (1024 bit) 994358373821297873423986022178929159513641342137481304248109111567867…
``````

All the values n,e(65537), d, p, q, exponent1, exponent2 and coefficient are present with probably proper lengths. So this is a private key. As already mentioned in other answer, see how you can load this in python

``````In [1]    from Crypto.PublicKey import RSA
from Crypto.Util.number import long_to_bytes, bytes_to_long

k = RSA.importKey("-----BEGIN RSA PRIVATE KEY-----\n%s\n-----END RSA PRIVATE KEY-----\n" % k_raw)
``````

Now decryption is simply a `pow` operation

``````print long_to_bytes(pow(bytes_to_long(ciphertext), k.d, k.n))

gFmqI4XpJBjprHFWrVqkaQ==
``````

First, the first sequence of hexadecimal (the key) and the last (`MIIE..`) are exactly the same in different encoding (namely base64 encoding). This blob is either a key or a certificate.

Anyway, we are missing the tags at begin and end of file telling what kind of format it is. Something like:

``````-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCA...
-----END RSA PRIVATE KEY-----
``````

It is formatted as an ASN.1 file format (see here for more).

What you should do:

1. You should first find the kind of data that is encoded here before applying the decryption algorithm.

You say that it is an RSA key, so you might have some clue where to start from.

2. Then, once you identified the key format, you can use `openssl` to decrypt your message (look on the web, there are plenty of tutorials about it).

• Hello, I know they are the same in different encoding but from what I say on the internet is that the RSA keys are usually in the base64 format, and I know that that is the key because as i said in the description that there are 2 files: `key` and `secret` from which the key ofc will contain the key and secret the message that I have to decrypt however after long searches on the internet and watching tutorials, forgive me man, but I can't figure out how to put this togheter, I feel like I've tried everything – C. Cristi Dec 30 '18 at 13:27
• In fact, RSA keys, ECDSA keys and X509 certificates are usually encoded in base64 within this format. So, you need to know what kind of key is hiding in this file. I advise you to read this page to know what you can try to decrypt your message. – perror Dec 30 '18 at 17:55
• Try an ASN.1 decoder here. Read from here about the structure of a private key file. Once you know that this is actually a private key file use `RSA` module from `Crypto.PublicKey` in python to import key and decrypt. – sudhackar Dec 31 '18 at 5:40
• @sudhackar okay how do I deterime if it's a key, do I paste the hex i got into the ASN and then get some other hex over there but in chunks of bytes, right? what do i do with them now? – C. Cristi Dec 31 '18 at 10:53
• @sudhackar Because it just tells me that it's sequence of 9 integers – C. Cristi Dec 31 '18 at 10:54