1

I've already tried the another solutions from SO, such as:

String password ="pwd";
        WinCrypt.DATA_BLOB pDataIn = new WinCrypt.DATA_BLOB(password.getBytes(Charset.forName("UTF-16LE")));
        WinCrypt.DATA_BLOB pDataEncrypted = new WinCrypt.DATA_BLOB();
        System.out.println(Crypt32.INSTANCE.CryptProtectData(pDataIn, "psw",
                null, null, null, WinCrypt.CRYPTPROTECT_UI_FORBIDDEN, pDataEncrypted));
        StringBuffer epwsb = new StringBuffer();
        byte[] pwdBytes= new byte [pDataEncrypted.cbData];
        pwdBytes=pDataEncrypted.getData();
        Formatter formatter = new Formatter(epwsb);
        for ( final byte b : pwdBytes ) {
            formatter.format("%02X", b);
        }
        System.out.println("password 51:b:"+ epwsb.toString());

or

Crypt32Util.cryptProtectData("12345".getBytes("UTF-16LE"), null, 0, "psw", null);

But all of them give different results for every time I run them, and they do not match the real password, that was saved by MSTSC or generated by RDP Password Hasher utility. Does anyone know the solution, or CLI-utility that can encrypt password?

artem
  • 16,382
  • 34
  • 113
  • 189
  • "all of them give different results for every time I run them" <-- are you sure you are not generating a _hash_ of this password and not an encrypted form? If it is a hash, then it is pretty normal that it can change with time is this hash is salted... – fge Jun 30 '13 at 02:43

1 Answers1

1

Here's my working solution (you need JNA platform, to get this working):

    private static String ToHexString(byte[] bytes) {   
        StringBuilder sb = new StringBuilder();   
        Formatter formatter = new Formatter(sb);   
        for (byte b : bytes) {
            formatter.format("%02x", b);   
        }
        formatter.close();
        return sb.toString();   
    }  

    private String cryptRdpPassword(String pass) {
        try {
            return ToHexString(Crypt32Util.cryptProtectData(pass.getBytes("UTF-16LE"), null, 0, "psw", null));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "ERROR";
        }
    }
Daniele Vrut
  • 2,835
  • 2
  • 22
  • 32