선언:

SystemFunction041 (C#)
[DllImport("advapi32", EntryPoint="SystemFunction041")]
static extern int RtlDecryptMemory(IntPtr Memory, int MemoryLength, int OptionFlags);

SystemFunction041 (VB.NET)
 <DllImport("advapi32", EntryPoint:="SystemFunction041")> _
Shared Function RtlDecryptMemory( _
    ByVal Memory As IntPtr, _
    ByVal MemoryLength As Integer, _
    ByVal OptionFlags As Integer) As Integer _
End Function



사용 예제:

SystemFunction041 예제 코드 (C#)
using System;
using System.Runtime.InteropServices;

namespace ApiReference {
    class ApiExample {
        [DllImport("advapi32", EntryPoint="SystemFunction040")]
        static extern int RtlEncryptMemory(IntPtr Memory, int MemoryLength, int OptionFlags);
        [DllImport("advapi32", EntryPoint="SystemFunction041")]
        static extern int RtlDecryptMemory(IntPtr Memory, int MemoryLength, int OptionFlags);
        const int RTL_ENCRYPT_MEMORY_SIZE = 8;
        
        public static void Main(string[] args) {
            IntPtr pLong = Marshal.AllocHGlobal(8);
            
            // pLong 의 주소값에 0x12345678ABCD 값을 쓴다.
            Marshal.WriteInt64(pLong, 0x1234567890ABCDEF);
            
            // 주소에 저장된 값을 읽어오고 표시한다.
            Console.WriteLine("SystemFunction040(RtlEncryptMemory) 메서드 호출 전");
            Console.WriteLine("\tpLong: 0x{0:X16}", Marshal.ReadInt64(pLong));
            
            // 이제 값을 암호화한다.
            RtlEncryptMemory(pLong, 8, 0);
            
            Console.WriteLine("SystemFunction040(RtlEncryptMemory) 메서드 호출 후");
            Console.WriteLine("\tpLong: 0x{0:X16}", Marshal.ReadInt64(pLong));
            
            // 다시 값을 복호화한다.
            RtlDecryptMemory(pLong, 8, 0);
            
            // 주소에 저장된 값을 읽어오고 표시한다.
            Console.WriteLine("SystemFunction041(RtlDecryptMemory) 메서드 호출 후");
            Console.WriteLine("\tpLong: 0x{0:X16}", Marshal.ReadInt64(pLong));
            
            Marshal.FreeHGlobal(pLong);
            
            Console.ReadKey(true);
        }
    }
}
예제 코드 (.NET Fiddle)


SystemFunction040 예제 코드 (VB.NET)
Imports System
Imports System.Runtime.InteropServices

Class ApiExample
    
    <DllImport("advapi32", EntryPoint:="SystemFunction040")>
    Shared Function RtlEncryptMemory(Memory As IntPtr, MemoryLength As Integer, OptionFlags As Integer) As Integer
    End Function
    
    <DllImport("advapi32", EntryPoint:="SystemFunction041")>
    Shared Function RtlDecryptMemory(Memory As IntPtr, MemoryLength As Integer, OptionFlags As Integer) As Integer
    End Function
        
    Const RTL_ENCRYPT_MEMORY_SIZE As Integer = 8
        
    Public Shared Sub Main(args As String())
        Dim pLong As IntPtr = Marshal.AllocHGlobal(8)
        
        ' pLong 의 주소값에 0x12345678ABCD 값을 쓴다.
        Marshal.WriteInt64(pLong, &H1234567890ABCDEF)
        
        ' 주소에 저장된 값을 읽어오고 표시한다.
        Console.WriteLine("SystemFunction040(RtlEncryptMemory) 메서드 호출 전")
        Console.WriteLine("\tpLong: 0x{0:X16}", Marshal.ReadInt64(pLong))
        
        ' 이제 값을 암호화한다.
        RtlEncryptMemory(pLong, 8, 0)
        
        Console.WriteLine("SystemFunction040(RtlEncryptMemory) 메서드 호출 후")
        Console.WriteLine("\tpLong: 0x{0:X16}", Marshal.ReadInt64(pLong))
        
        ' 다시 값을 복호화한다.
        RtlDecryptMemory(pLong, 8, 0)
        
        ' 주소에 저장된 값을 읽어오고 표시한다.
        Console.WriteLine("SystemFunction041(RtlDecryptMemory) 메서드 호출 후")
        Console.WriteLine("\tpLong: 0x{0:X16}", Marshal.ReadInt64(pLong))
        
        Marshal.FreeHGlobal(pLong)
        
        Console.ReadKey(True)
    End Sub
End Class
예제 코드 (.NET Fiddle)




예제 실행 결과:




매개변수 설명(기울임꼴로 표시된 매개변수는 생략 가능합니다):

Memory - 복호화할 메모리의 주소를 입력합니다.

MemoryLength - 복호화할 메모리 영역의 크기를 입력합니다. (비고 참조)

OptionFlags - 복호화 옵션을 입력합니다. (비고 참조)




API 설명:

메모리를 복호화합니다.




참고:

SystemFunction041 (RtlDecryptMemory) (MSDN)




요구 사항:

Windows XP 이상

Windows Server 2003 이상




비고:

이 API의 원래 이름은 SystemFunction041 입니다. 하지만 MSDN 에서는 RtlDecryptMemory 로 표기하고 있으므로, P/Invoke 메서드 정의 부분에는 메서드 이름을 RtlDecryptMemory 로 명명해놓고 진입점을 SystemFunction041 으로 설정하였습니다.


MemoryLength 매개변수의 값은 반드시 RTL_ENCRYPT_MEMORY_SIZE 상수 값의 배수가 되어야 합니다.

RTL_ENCRYPT_MEMORY_SIZE 상수는 ntsecapi.h 헤더 파일에 다음과 같이 정의되어 있습니다.

RTL_ENCRYPT_MEMORY_SIZE 상수 선언
#define RTL_ENCRYPT_MEMORY_SIZE 8


OptionFlags 매개변수에 사용할 수 있는 값은 반드시 다음 중 하나여야 합니다.

OptionFlags 매개변수에 사용될 수 있는 옵션 설명 및 값
 RTL_ENCRYPT_OPTION_SAME_PROCESS     (0, 0x00000000)
// -> 호출자 프로세스만 암호화 및 복호화가 가능

RTL_ENCRYPT_OPTION_CROSS_PROCESS    (1, 0x00000001)
// -> 다른 프로세스에서도 암호화 및 복호화가 가능

RTL_ENCRYPT_OPTION_SAME_LOGON       (2, 0x00000002)
// -> 같은 로그온 자격 증명 내에서 암호화 및 복호화가 가능




.NET Fiddle 의 .NET 보안 수준으로 인해 P/Invoke 를 직접 테스트하실 수 없습니다. .NET Fiddle 사이트는 코드 참고용으로만 이용해 주시기 바랍니다.
Powered by SlaneR & TeamDEV Korea


'API Reference' 카테고리의 다른 글

60. RegisterWindowMessage  (0) 2015.10.03
59. IsWindow  (0) 2015.09.29
57. SystemFunction040 (RtlEncryptMemory)  (0) 2015.09.26
56. CreateFile  (2) 2015.09.14
55. AccessMask  (0) 2015.09.13

+ Recent posts