summaryrefslogtreecommitdiffstats
path: root/src/main/java/de/bwlehrpool/bwlp_guac/WeakCrypto.java
blob: b7d8a11b0f4b508ac603acf184d3c7ca1e6cc05c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package de.bwlehrpool.bwlp_guac;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class WeakCrypto {

	private static byte[] reverseBits(byte[] b) {
		byte[] result = new byte[b.length];
		for (int i = 0; i < b.length; i++) {
			result[i] = reverseBits(b[i]);
		}
		return result;
	}

	private static byte reverseBits(byte input) {
		byte result = 0x00;
		for (int i = 0; i < 8; i++) {
			result |= ((byte) ((input & (0x01 << i)) >>> i) << 7 - i);
		}
		return result;
	}

	public static byte[] vncEncrypt(byte[] pw_bytes, byte[] challenge) {
		Cipher des;
		try {
			des = Cipher.getInstance("DES/ECB/NoPadding");
			des.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(reverseBits(pw_bytes), 0, pw_bytes.length, "DES"));
			return des.doFinal(challenge);
		} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new byte[16];
	}

}