Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.Console;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.nio.ByteBuffer;
- import java.nio.CharBuffer;
- import java.nio.charset.Charset;
- import java.nio.charset.CharsetEncoder;
- import java.security.InvalidAlgorithmParameterException;
- import java.security.InvalidKeyException;
- import java.security.NoSuchAlgorithmException;
- import java.security.spec.InvalidKeySpecException;
- import java.util.Arrays;
- import java.util.Random;
- import javax.crypto.Cipher;
- import javax.crypto.CipherOutputStream;
- import javax.crypto.NoSuchPaddingException;
- import javax.crypto.SecretKey;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.PBEKeySpec;
- import javax.crypto.spec.PBEParameterSpec;
- public class MinecraftLoginEncrypter {
- public static void main(String[] args) throws IOException {
- System.err.println("Security Note: Please check that this program has not been modified to harvest your login before inputting your username-password combination. In the source, look for \"java.net\"; in the binary, look for \"Ljava/net\" in a hex editor.");
- BufferedReader keyboardLines = new BufferedReader(new InputStreamReader(System.in));
- System.err.print("Username: ");
- String user = keyboardLines.readLine();
- char[] pass = null;
- try {
- Console console = System.console();
- if (console != null) {
- System.err.print("Password: ");
- pass = console.readPassword();
- }
- } catch (NoClassDefFoundError ncdfe) {
- // pass = null;
- } catch (NoSuchMethodError nsme) {
- // pass = null;
- }
- if (pass == null) {
- System.err.println("Security Note: No password-hiding functionality is available in this Java VM. The following password prompt will SHOW the password you enter.");
- System.err.print("Password: ");
- pass = keyboardLines.readLine().toCharArray();
- }
- MinecraftLoginEncrypter encrypter = new MinecraftLoginEncrypter(user, pass);
- try {
- encrypter.writeFile(new File(".", "lastlogin"));
- System.err.println("File written.");
- } finally {
- encrypter.close();
- }
- }
- private final String user;
- private final char[] pass;
- public MinecraftLoginEncrypter(final String user, final char[] pass) {
- this.user = user;
- this.pass = pass;
- }
- public void writeFile(File file) throws IOException {
- Cipher cipher;
- try {
- cipher = getCipher(Cipher.ENCRYPT_MODE, "passwordfile");
- } catch (Exception e) {
- throw new IOException("Error occurred while creating the cipher for the Minecraft login file", e);
- }
- DataOutputStream dos;
- if (cipher != null)
- dos = new DataOutputStream(new CipherOutputStream(new FileOutputStream(file), cipher));
- else {
- dos = new DataOutputStream(new FileOutputStream(file));
- }
- dos.writeUTF(user);
- CharsetEncoder c = Charset.forName("UTF-8").newEncoder();
- ByteBuffer bb = ByteBuffer.allocate(pass.length * (int) Math.ceil(c.maxBytesPerChar()));
- try {
- c.encode(CharBuffer.wrap(pass), bb, true);
- c.reset();
- dos.writeShort(bb.position());
- bb.flip();
- byte[] passBytes = new byte[bb.remaining()];
- try {
- bb.get(passBytes);
- dos.write(passBytes);
- } finally {
- // Avoid memory sniffing attacks: zero out the password
- Arrays.fill(passBytes, (byte) 0);
- }
- } finally {
- bb.clear();
- // Avoid memory sniffing attacks: zero out the password
- for (int i = 0; i < bb.limit(); i++) {
- bb.put(i, (byte) 0);
- }
- }
- dos.flush();
- dos.close();
- }
- public void close() {
- // Avoid memory sniffing attacks: zero out the password
- Arrays.fill(pass, '\0');
- }
- protected void finalize() throws Throwable {
- close();
- }
- private Cipher getCipher(int mode, String password) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
- Random random = new Random(43287234L);
- byte[] salt = new byte[8];
- random.nextBytes(salt);
- PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 5);
- SecretKey pbeKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(password.toCharArray()));
- Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
- cipher.init(mode, pbeKey, pbeParamSpec);
- return cipher;
- }
- }
Add Comment
Please, Sign In to add comment