Search in sources :

Example 21 with BYTE

use of com.sun.jna.platform.win32.WinDef.BYTE 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;
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) DATA_BLOB(com.sun.jna.platform.win32.WinCrypt.DATA_BLOB)

Example 22 with BYTE

use of com.sun.jna.platform.win32.WinDef.BYTE in project jna by java-native-access.

the class Advapi32Test method testWriteEncryptedFileRaw.

public void testWriteEncryptedFileRaw() throws Exception {
    // create an encrypted file
    File file = createTempFile();
    String lpFileName = file.getAbsolutePath();
    assertTrue(Advapi32.INSTANCE.EncryptFile(lpFileName));
    // open file for export
    ULONG ulFlags = new ULONG(0);
    PointerByReference pvContext = new PointerByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lpFileName, ulFlags, pvContext));
    // read encrypted file
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    FE_EXPORT_FUNC pfExportCallback = new FE_EXPORT_FUNC() {

        @Override
        public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONG ulLength) {
            if (pbData == null) {
                throw new NullPointerException("Callback data unexpectedly null");
            }
            byte[] arr = pbData.getByteArray(0, ulLength.intValue());
            try {
                outputStream.write(arr);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return new DWORD(W32Errors.ERROR_SUCCESS);
        }
    };
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.ReadEncryptedFileRaw(pfExportCallback, null, pvContext.getValue()));
    outputStream.close();
    Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
    // open file for import
    String lbFileName2 = System.getProperty("java.io.tmpdir") + File.separator + "backup-" + file.getName();
    ULONG ulFlags2 = new ULONG(CREATE_FOR_IMPORT);
    PointerByReference pvContext2 = new PointerByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lbFileName2, ulFlags2, pvContext2));
    // write encrypted file
    final IntByReference elementsReadWrapper = new IntByReference(0);
    FE_IMPORT_FUNC pfImportCallback = new FE_IMPORT_FUNC() {

        @Override
        public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONGByReference ulLength) {
            int elementsRead = elementsReadWrapper.getValue();
            int remainingElements = outputStream.size() - elementsRead;
            int length = Math.min(remainingElements, ulLength.getValue().intValue());
            pbData.write(0, outputStream.toByteArray(), elementsRead, length);
            elementsReadWrapper.setValue(elementsRead + length);
            ulLength.setValue(new ULONG(length));
            return new DWORD(W32Errors.ERROR_SUCCESS);
        }
    };
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.WriteEncryptedFileRaw(pfImportCallback, null, pvContext2.getValue()));
    Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext2.getValue());
    file.delete();
    new File(lbFileName2.toString()).delete();
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) IntByReference(com.sun.jna.ptr.IntByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) Pointer(com.sun.jna.Pointer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ULONGByReference(com.sun.jna.platform.win32.WinDef.ULONGByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) FE_EXPORT_FUNC(com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC) File(java.io.File) FE_IMPORT_FUNC(com.sun.jna.platform.win32.WinBase.FE_IMPORT_FUNC)

Example 23 with BYTE

use of com.sun.jna.platform.win32.WinDef.BYTE in project jna by java-native-access.

the class Advapi32UtilTest method testConvertSid.

public void testConvertSid() {
    // Everyone
    String sidString = "S-1-1-0";
    byte[] sidBytes = Advapi32Util.convertStringSidToSid(sidString);
    assertTrue(sidBytes.length > 0);
    String convertedSidString = Advapi32Util.convertSidToStringSid(new PSID(sidBytes));
    assertEquals(convertedSidString, sidString);
}
Also used : PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 24 with BYTE

use of com.sun.jna.platform.win32.WinDef.BYTE in project jna by java-native-access.

the class GuidTest method testGUIDFromBinary2.

/**
	 * Loads a GUID from a byte array via the constructor and verify that the
	 * guid returned has the expected values in each byte.
	 */
public void testGUIDFromBinary2() {
    byte[] sourceGuidBArr = new byte[] { (byte) 0xA5, (byte) 0xDC, (byte) 0xBF, (byte) 0x10, (byte) 0x65, (byte) 0x30, (byte) 0x11, (byte) 0xD2, (byte) 0x90, (byte) 0x1F, (byte) 0x00, (byte) 0xC0, (byte) 0x4F, (byte) 0xB9, (byte) 0x51, (byte) 0xED };
    // test loading via constructor
    GUID targetGuid = new GUID(sourceGuidBArr);
    byte[] targetGuidBArr = targetGuid.toByteArray();
    for (int i = 0; i < sourceGuidBArr.length; i++) {
        assertEquals(targetGuidBArr[i], sourceGuidBArr[i]);
    }
}
Also used : GUID(com.sun.jna.platform.win32.Guid.GUID)

Example 25 with BYTE

use of com.sun.jna.platform.win32.WinDef.BYTE in project jna by java-native-access.

the class Kernel32Test method testReadProcessMemory.

public void testReadProcessMemory() {
    Kernel32 kernel = Kernel32.INSTANCE;
    boolean successRead = kernel.ReadProcessMemory(null, Pointer.NULL, Pointer.NULL, 1, null);
    assertFalse(successRead);
    assertEquals(kernel.GetLastError(), WinError.ERROR_INVALID_HANDLE);
    ByteBuffer bufSrc = ByteBuffer.allocateDirect(4);
    bufSrc.put(new byte[] { 5, 10, 15, 20 });
    ByteBuffer bufDest = ByteBuffer.allocateDirect(4);
    bufDest.put(new byte[] { 0, 1, 2, 3 });
    Pointer ptrSrc = Native.getDirectBufferPointer(bufSrc);
    Pointer ptrDest = Native.getDirectBufferPointer(bufDest);
    HANDLE selfHandle = kernel.GetCurrentProcess();
    //Read only the first three
    kernel.ReadProcessMemory(selfHandle, ptrSrc, ptrDest, 3, null);
    assertEquals(bufDest.get(0), 5);
    assertEquals(bufDest.get(1), 10);
    assertEquals(bufDest.get(2), 15);
    assertEquals(bufDest.get(3), 3);
}
Also used : Pointer(com.sun.jna.Pointer) ByteBuffer(java.nio.ByteBuffer) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)12 Pointer (com.sun.jna.Pointer)8 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)8 Advapi32 (com.sun.jna.platform.win32.Advapi32)6 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)6 PointerByReference (com.sun.jna.ptr.PointerByReference)5 Memory (com.sun.jna.Memory)4 GUID (com.sun.jna.platform.win32.Guid.GUID)4 BSTR (com.sun.jna.platform.win32.WTypes.BSTR)4 File (java.io.File)4 Date (java.util.Date)4 Test (org.junit.Test)4 DATE (com.sun.jna.platform.win32.OaIdl.DATE)3 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)3 FE_EXPORT_FUNC (com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC)3 BYTE (com.sun.jna.platform.win32.WinDef.BYTE)3 CHAR (com.sun.jna.platform.win32.WinDef.CHAR)3 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)3 LONG (com.sun.jna.platform.win32.WinDef.LONG)3 SHORT (com.sun.jna.platform.win32.WinDef.SHORT)3