I created a basic file encryption method, that seems pretty secure to me (based on an answer below, it seems to be an implementation of the Vigenère cipher) , but one of my colleagues claims that it can be cracked if you know the length of the password.
The way the encryption works, is that it reads the byte value of the file, puts it into an array, and then takes the password, encrypts it with MD5, and subtracts each Unicode value of the current character of the hash that it is looking at (it uses a loop with an index, so that in, for example, a 3 letter password, it subtracts the value of the first character from the first byte, the second character from the second byte, the third character from the third byte, and then back to the first character from the 4th byte, only with the hash instead of the password) then it takes the absolute value of that, and puts that into an array which it returns, and then is written to a new file. How crackable is this type of encryption? It seems like it would be difficult, being that it changes for different files and different passwords, but I'm new to encryption.
In theory, how easy/hard would it be to crack an encryption like this (would be best if you listed how hard it is for longer vs shorter passwords)?
The actual source code is bellow (it's written in java, and the method that returns the encrypted array is the encrypt() method) It may help to explain what I am trying to do a little better than how I explained it above.
public ArrayList<Integer> encrypt(String password, ArrayList<Integer> hash) {
ArrayList<Integer> encryptedHash = new ArrayList<Integer>();
ArrayList<Integer> passwordCodeArray = getArrayList(md5(password));
Integer passwordIndex = 0;
for (Integer currentByte : hash) {
Integer currentByteProduct = currentByte - passwordCodeArray.get(passwordIndex);
if (currentByteProduct < 0) currentByteProduct += 255;
encryptedHash.add(currentByteProduct);
if (passwordIndex == (password.length() - 1)) {
passwordIndex = 0;
} else passwordIndex++;
}
return encryptedHash;
}
private ArrayList<Integer> getArrayList(String password) {
ArrayList<Integer> passwordCodeArray = new ArrayList<Integer>();
for (int i = 0; i < password.length(); i++) {
passwordCodeArray.add(password.codePointAt(i));
}
return passwordCodeArray;
}
private String md5(String s) {
try {
MessageDigest m = MessageDigest.getInstance("MD5");
m.update(s.getBytes(), 0, s.length());
BigInteger i = new BigInteger(1,m.digest());
return String.format("%1$032x", i);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
Edit: I was told that in order to make it a decent encryption, I needed to make the password as long as the document. Since I am using the password's hash to encrypt the file, is there a way for me to encrypt the password in such a way, that it will dynamicly create a hash that is as long as the document?