diff --git a/common/src/main/java/com/microsoft/alm/helpers/StringHelper.java b/common/src/main/java/com/microsoft/alm/helpers/StringHelper.java index 0067a62a..efb15ffb 100644 --- a/common/src/main/java/com/microsoft/alm/helpers/StringHelper.java +++ b/common/src/main/java/com/microsoft/alm/helpers/StringHelper.java @@ -8,6 +8,8 @@ public class StringHelper { private static final Charset UTF8 = Charset.forName("UTF-8"); + private static final Charset UTF16LE = Charset.forName("UTF-16LE"); + public static final String Empty = ""; public static boolean endsWithIgnoreCase(final String haystack, final String needle) { @@ -193,6 +195,17 @@ public static byte[] UTF8GetBytes(final String value) { return result; } + /** + * Encodes all the characters in the specified string into a sequence of UTF-16LE bytes. + * + * @param value The string containing the characters to encode. + * @return A byte array containing the results of encoding the specified set of characters. + */ + public static byte[] UTF16LEGetBytes(final String value) { + final byte[] result = value.getBytes(UTF16LE); + return result; + } + /** * Decodes all the bytes in the specified byte array into a string. * @@ -204,6 +217,17 @@ public static String UTF8GetString(final byte[] bytes) { return result; } + /** + * Decodes all the bytes in the specified byte array into a string. + * + * @param bytes The byte array containing the sequence of bytes to decode. + * @return A string that contains the results of decoding the specified sequence of bytes. + */ + public static String UTF16LEGetString(final byte[] bytes) { + final String result = new String(bytes, UTF16LE); + return result; + } + /** * Decodes a range of bytes from a byte array into a string. * @@ -216,4 +240,17 @@ public static String UTF8GetString(final byte[] bytes, final int index, final in final String result = new String(bytes, index, count, UTF8); return result; } + + /** + * Decodes a range of bytes from a byte array into a string. + * + * @param bytes The byte array containing the sequence of bytes to decode. + * @param index The index of the first byte to decode. + * @param count The number of bytes to decode. + * @return A string that contains the results of decoding the specified sequence of bytes. + */ + public static String UTF16LEGetString(final byte[] bytes, final int index, final int count) { + final String result = new String(bytes, index, count, UTF16LE); + return result; + } } diff --git a/storage/src/main/java/com/microsoft/alm/storage/windows/internal/CredManagerBackedSecureStore.java b/storage/src/main/java/com/microsoft/alm/storage/windows/internal/CredManagerBackedSecureStore.java index 4b4f2cff..ea31537e 100644 --- a/storage/src/main/java/com/microsoft/alm/storage/windows/internal/CredManagerBackedSecureStore.java +++ b/storage/src/main/java/com/microsoft/alm/storage/windows/internal/CredManagerBackedSecureStore.java @@ -87,7 +87,7 @@ public E get(String key) { final CredAdvapi32.CREDENTIAL credential = new CredAdvapi32.CREDENTIAL(pcredential.credential); byte[] secretBytes = credential.CredentialBlob.getByteArray(0, credential.CredentialBlobSize); - final String secret = StringHelper.UTF8GetString(secretBytes); + final String secret = StringHelper.UTF16LEGetString(secretBytes); final String username = credential.UserName; cred = create(username, secret); @@ -159,7 +159,7 @@ public boolean add(String key, E secret) { final String username = getUsername(secret); final String credentialBlob = getCredentialBlob(secret); - byte[] credBlob = StringHelper.UTF8GetBytes(credentialBlob); + byte[] credBlob = StringHelper.UTF16LEGetBytes(credentialBlob); final CredAdvapi32.CREDENTIAL cred = buildCred(key, username, credBlob);