I am trying to crack an RC4 encryption in java. I am given 8 bytes of an 11 byte
ID: 3743920 • Letter: I
Question
I am trying to crack an RC4 encryption in java. I am given 8 bytes of an 11 byte key. I need to find the remaining 3 bytes,: Pretending I didn't have the last 3 bytes of the key, how would I change to cipher text to plain text. My test code is:
class ARC4CrackerTest {
@org.junit.jupiter.api.Test
void noHintCrack() throws NoSuchPaddingException,
NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException {
// setup the secret
byte secretBytes[] = Arrays.copyOf(ARC4Cracker.keyPrefix, 11);
secretBytes[8] = (byte) 0xd0;
secretBytes[9] = (byte) 0xff;
secretBytes[10] = 1;
SecretKey secretKey = new SecretKeySpec(secretBytes, "ARCFOUR");
// setup the cipher
// encrypt and base64 encode
String plainText = "this is the secret plain text";
String base64CipherText =
Base64.getEncoder().encodeToString(rc4Encrypt(secretKey,
plainText.getBytes()));
String base64KnownString =
Base64.getEncoder().encodeToString("secret".getBytes());
// see if the cracker can give us the answer
ARC4Cracker cracker = new ARC4Cracker();
String base64CrackedText = cracker.crack(base64CipherText,
base64KnownString);
Assertions.assertEquals(plainText, new
String(Base64.getDecoder().decode(base64CrackedText)));
}
private byte[] rc4Encrypt(SecretKey secretKey, byte[] plainText)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher arc4 = Cipher.getInstance("ARCFOUR");
arc4.init(Cipher.ENCRYPT_MODE, secretKey);
return arc4.doFinal(plainText);
}
My code for cracking is:
public class ARC4Cracker {
/**
* the know prefix. (we just need to discover the remaining 3 bytes)
*/
static public byte keyPrefix[] = { 0x13, 0x37, (byte)0xd0, 0x0d, 0x15,
0x50, (byte)0xc0, 0x01};
/**
* this method provides a hint of known plaintext, and the
corresponding cipherText
* @param base64CipherText
* @param base64PlainText base64 encoded known plain text from
* @param position the position in the stream where the text was
known
*/
public void crackedText(String base64CipherText, String
base64PlainText, int position) { }
/**
* the method will crack cipher text by searching for the correct
plain text containing
* the known string
* @param base64CipherText base64 encoded cipher text to crack
* @param base64KnownText a base64 encoded string that is know to
exist in the plain text
* @return the base64 encoded plain text or null if couldn't crack
*/
public String crack(String base64CipherText, String base64KnownText) {
return null; }
}
Thanks
Explanation / Answer
First, lets see how RC4 works
1. RC4 genereates a KeyStream(KS) out of a key(K) and then it uses this keystream to encrypt some message(M). Lets say encrypted message is (E). Internally RC4 will XOR 'KS' with 'M" and gives you 'E'. We should remember that 'KS' is a some random number generated out of 'K'. => E = AKS
So know what is the Key(K), you should know KeyStream(KS). But in your case you don't have some bytes of the key itself.
Can you specify what are the inputs you have -- like Encrypted message, plaing message or two encrypted messages with same key etc.
However, following are the possible things you can try out
1. You can get the KeyStream by XOR of Message(M) with Encrypted Message(E)
KS=EM
In general, it is not recommended that you use same key for encrypting multiple messages. Why?
Lets take an example
KS=RC4(K)
E1=M1KS
E2=M2KS
If an attacker is in possession of M1,E1 and E2, it is trivial to extract the keystream generated by RC4(K) and decrypt the message M2 (at least up to the same length as M1):
M2=E2E1M1
Let's say RC4 produces the following keystream:
With message M1="hello" and M2="secret!", you get the following ciphertexts:
By computing M1E1, you get the first 5 bytes of the keystream, which allows you to decrypt the first 5 bytes of M2. Without the knowledge of the secret key or a longer plaintext-ciphertext pair you will not be able to decrypt the rest of M2.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.