Ephraim
Ephraim

Reputation: 8391

how to intentionally corrupt a file in java

Note: Please do not judge this question. To those who think that I am doing this to "cheat"; you are mistaken, as I am no longer in school anyway. In addition, if I was, myself actually trying to cheat, I would simply use services that have already been created for this, instead of recreating the program. I took on this project because I thought it might be fun, nothing else. Before you down-vote, please consider the value of the question it's self, and not the speculative uses of it, as the purpose of SO is not to judge, but simply give the public information.


I am developing a program in java that is supposed intentionally corrupt a file (specifically a .doc, txt, or pdf, but others would be good as well)

I initially tried this:

public void corruptFile (String pathInName, String pathOutName) {
    curroptMethod method  = new curroptMethod();
    ArrayList<Integer> corruptHash = corrupt(getBytes(pathInName));
    writeBytes(corruptHash, pathOutName);
    new MimetypesFileTypeMap().getContentType(new File(pathInName));
    //  "/home/ephraim/Desktop/testfile"
}

public ArrayList<Integer> getBytes(String filePath) {
    ArrayList<Integer> fileBytes = new ArrayList<Integer>();
    try {
        FileInputStream myInputStream = new FileInputStream(new File(filePath));
        do {
            int currentByte = myInputStream.read();
            if(currentByte == -1) {
                System.out.println("broke loop");
                break;
            }
            fileBytes.add(currentByte);
        } while (true);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(fileBytes);
    return fileBytes;
}

public void writeBytes(ArrayList<Integer> hash, String pathName) {
    try {
        OutputStream myOutputStream = new FileOutputStream(new File(pathName));
        for (int currentHash : hash) {
            myOutputStream.write(currentHash);
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //System.out.println(hash);
}

public ArrayList<Integer> corrupt(ArrayList<Integer> hash) {
    ArrayList<Integer> corruptHash = new ArrayList<Integer>();
    ArrayList<Integer> keywordCodeArray = new ArrayList<Integer>();
    Integer keywordIndex = 0;
            String keyword = "corruptthisfile";

    for (int i = 0; i < keyword.length(); i++) {
        keywordCodeArray.add(keyword.codePointAt(i));
    }

    for (Integer currentByte : hash) {


        //Integer currentByteProduct = (keywordCodeArray.get(keywordIndex) + currentByte) / 2;
        Integer currentByteProduct =  currentByte - keywordCodeArray.get(keywordIndex);
        if (currentByteProduct < 0) currentByteProduct += 255;
        corruptHash.add(currentByteProduct);

        if (keywordIndex == (keyword.length() - 1)) {
            keywordIndex = 0;
        } else keywordIndex++;
    }

    //System.out.println(corruptHash);
    return corruptHash;
}

but the problem is that the file is still openable. When you open the file, all of the words are changed (and they may not make any sense, and they may not even be letters, but it can still be opened)

so here is my actual question:

Is there a way to make a file so corrupt that the computer doesn't know how to open it at all (ie. when you open it, the computer will say something along the lines of "this file is not recognized, and cannot be opened")?

Upvotes: 2

Views: 6734

Answers (3)

emory
emory

Reputation: 10891

There are better ways:

  1. Your professor accepts Word documents. Infect it with a macro virus before sending.
  2. "Forget" to attach the file to the email.
  3. Forge the send date on your email. If your prof is the kind that accepts Word docs, this may work.

Upvotes: 0

thkala
thkala

Reputation: 86333

The only way to fully corrupt an arbitrary file is to replace all of its contents with random garbage. Even then, there is an infinitely small probability that the random garbage will actually be something meaningful.

Depending on the file type, it may be possible to recover from limited - or even from not so limited - corruption. E.g.:

  • Streaming media codecs are designed with network packet loss take into account. Limited corruption may show up as picture artifacts, or even as a few lost frames, but the content is usually still viewable.

  • Block-based compression algorithms, such as bzip2, allow undamaged blocks to be recovered.

  • File-based compression systems such as rar and zip may be able to recover those files whose compressed data has not been damaged, regardless of damage to the rest of the archive.

  • Human-readable text, such as text files and source code files, is still viewable in a text editor, even if parts of it are corrupt - not to mention its size that does not change. Unless you corrupted the whole thing, any casual reader would be able to tell whether an assignment was done and whether the retransmitted file was the same as the one that got corrupted.

Apart from the ethical issue, have you considered that this would be a one-time thing only? Data corruption does happen, but it's not that frequent and it's never that convenient...

If you are that desperate for more time, you would be better off breaking your leg and getting yourself admitted to a hospital.

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200148

I think you want to look into the RandomAccessFile. Also, it is almost always the case that a program recognizes its file by its very start. So open the file and scramble the first 5 bytes.

Upvotes: 3

Related Questions