use of com.sun.jna.platform.win32.WinCrypt.DATA_BLOB in project jna by java-native-access.
the class Crypt32Test method testCryptProtectUnprotectData.
public void testCryptProtectUnprotectData() {
DATA_BLOB pDataIn = new DATA_BLOB("hello world");
DATA_BLOB pDataEncrypted = new DATA_BLOB();
try {
assertTrue("CryptProtectData(Initial)", Crypt32.INSTANCE.CryptProtectData(pDataIn, "description", null, null, null, 0, pDataEncrypted));
PointerByReference pDescription = new PointerByReference();
try {
DATA_BLOB pDataDecrypted = new DATA_BLOB();
try {
assertTrue("CryptProtectData(Crypt)", Crypt32.INSTANCE.CryptUnprotectData(pDataEncrypted, pDescription, null, null, null, 0, pDataDecrypted));
assertEquals("description", pDescription.getValue().getWideString(0));
assertEquals("hello world", pDataDecrypted.pbData.getString(0));
} finally {
Kernel32Util.freeLocalMemory(pDataDecrypted.pbData);
}
} finally {
Kernel32Util.freeLocalMemory(pDescription.getValue());
}
} finally {
Kernel32Util.freeLocalMemory(pDataEncrypted.pbData);
}
}
use of com.sun.jna.platform.win32.WinCrypt.DATA_BLOB in project jna by java-native-access.
the class Crypt32Util method cryptProtectData.
/**
* Protect a blob of data.
* @param data
* Data to protect.
* @param entropy
* Optional entropy.
* @param flags
* Optional flags.
* @param description
* Optional description.
* @param prompt
* Prompt structure.
* @return
* Protected bytes.
*/
public static byte[] cryptProtectData(byte[] data, byte[] entropy, int flags, String description, CRYPTPROTECT_PROMPTSTRUCT prompt) {
DATA_BLOB pDataIn = new DATA_BLOB(data);
DATA_BLOB pDataProtected = new DATA_BLOB();
DATA_BLOB pEntropy = (entropy == null) ? null : new DATA_BLOB(entropy);
try {
if (!Crypt32.INSTANCE.CryptProtectData(pDataIn, description, pEntropy, null, prompt, flags, pDataProtected)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return pDataProtected.getData();
} finally {
if (pDataProtected.pbData != null) {
Kernel32Util.freeLocalMemory(pDataProtected.pbData);
}
}
}
use of com.sun.jna.platform.win32.WinCrypt.DATA_BLOB in project jna by java-native-access.
the class Crypt32Util method cryptUnprotectData.
/**
* Unprotect a blob of data.
* @param data
* Data to unprotect.
* @param entropy
* Optional entropy.
* @param flags
* Optional flags.
* @param prompt
* Optional prompt structure.
* @return
* Unprotected blob of data.
*/
public static byte[] cryptUnprotectData(byte[] data, byte[] entropy, int flags, CRYPTPROTECT_PROMPTSTRUCT prompt) {
DATA_BLOB pDataIn = new DATA_BLOB(data);
DATA_BLOB pDataUnprotected = new DATA_BLOB();
DATA_BLOB pEntropy = (entropy == null) ? null : new DATA_BLOB(entropy);
PointerByReference pDescription = new PointerByReference();
Win32Exception err = null;
byte[] unProtectedData = null;
try {
if (!Crypt32.INSTANCE.CryptUnprotectData(pDataIn, pDescription, pEntropy, null, prompt, flags, pDataUnprotected)) {
err = new Win32Exception(Kernel32.INSTANCE.GetLastError());
} else {
unProtectedData = pDataUnprotected.getData();
}
} finally {
if (pDataUnprotected.pbData != null) {
try {
Kernel32Util.freeLocalMemory(pDataUnprotected.pbData);
} catch (Win32Exception e) {
if (err == null) {
err = e;
} else {
err.addSuppressed(e);
}
}
}
if (pDescription.getValue() != null) {
try {
Kernel32Util.freeLocalMemory(pDescription.getValue());
} catch (Win32Exception e) {
if (err == null) {
err = e;
} else {
err.addSuppressed(e);
}
}
}
}
if (err != null) {
throw err;
}
return unProtectedData;
}
use of com.sun.jna.platform.win32.WinCrypt.DATA_BLOB in project jna by java-native-access.
the class Crypt32Test method testCryptProtectUnprotectDataWithEntropy.
public void testCryptProtectUnprotectDataWithEntropy() {
DATA_BLOB pDataIn = new DATA_BLOB("hello world");
DATA_BLOB pEntropy = new DATA_BLOB("entropy");
DATA_BLOB pDataEncrypted = new DATA_BLOB();
try {
assertTrue("CryptProtectData(Initial)", Crypt32.INSTANCE.CryptProtectData(pDataIn, "description", pEntropy, null, null, 0, pDataEncrypted));
PointerByReference pDescription = new PointerByReference();
try {
DATA_BLOB pDataDecrypted = new DATA_BLOB();
try {
// can't decrypt without entropy
assertFalse("CryptUnprotectData(NoEntropy)", Crypt32.INSTANCE.CryptUnprotectData(pDataEncrypted, pDescription, null, null, null, 0, pDataDecrypted));
// decrypt with entropy
assertTrue("CryptUnprotectData(WithEntropy)", Crypt32.INSTANCE.CryptUnprotectData(pDataEncrypted, pDescription, pEntropy, null, null, 0, pDataDecrypted));
assertEquals("description", pDescription.getValue().getWideString(0));
assertEquals("hello world", pDataDecrypted.pbData.getString(0));
} finally {
Kernel32Util.freeLocalMemory(pDataDecrypted.pbData);
}
} finally {
Kernel32Util.freeLocalMemory(pDescription.getValue());
}
} finally {
Kernel32Util.freeLocalMemory(pDataEncrypted.pbData);
}
}
Aggregations