プレーンテキストの終焉:MS ProjectにおけるDPAPIを用いた機密情報保護のアーキテクチャ
MS Projectのカスタムフィールド(`Text`型や`Number`型)は、プロジェクト管理における「情報の墓場」になりがちだ。単価、リソースの個人情報、あるいは戦略的なマイルストーンのコスト。これらが単なるプレーンテキストとして`.mpp`ファイルに書き込まれている現状は、エンタープライズ環境においては致命的なセキュリティホールである。
「パスワードをかけておけばいい」という考えは、素人のそれだ。ファイルが開かれた瞬間、カスタムフィールドの値はすべて剥き出しになる。我々エンジニアが構築すべきは、「ファイルが盗まれても、特定の実行環境(Windowsユーザーセッション)なしには無価値である」という、データと実行環境を密結合させた保護レイヤーである。
本稿では、Windows APIのDPAPI (Data Protection API) を活用し、MS ProjectのVBAから機密情報を暗号化・復号してカスタムフィールドに格納する、極限のプロフェッショナル・アーキテクチャを提示する。
1. アーキテクチャ設計:なぜDPAPIなのか
暗号化の実装において、初心者は独自のAESアルゴリズムをVBAで書き込もうとする。それは時間の無駄であり、脆弱性の温床だ。シニアエンジニアが選択すべきは、OSが提供する堅牢なプロトコルである。
Windows DPAPI (`CryptProtectData`) を採用する理由は3点ある。
1. 鍵管理の不要化: 暗号化キーをコード内にハードコードする必要がない。鍵はユーザーのログイン資格情報に紐付いてOSが管理する。
2. コンテキスト依存性: 特定のWindowsユーザーがログインしている環境でしか復号できないため、ファイル単体の流出に対する耐性が極めて高い。
3. パフォーマンス: ネイティブのC++実装を呼び出すため、VBAによる純粋な計算ロジックよりも圧倒的に高速である。
システムフロー
1. Write時: `String` $\rightarrow$ `Byte Array` $\rightarrow$ `CryptProtectData` $\rightarrow$ `Base64 Encoding` $\rightarrow$ `Custom Field (Text)`
2. Read時: `Custom Field (Text)` $\rightarrow$ `Base64 Decoding` $\rightarrow$ `Byte Array` $\rightarrow$ `CryptUnprotectData` $\rightarrow$ `String`
2. 実装:Windows APIによる暗号化コアモジュール
以下に、64bit/32bit両対応の`PtrSafe`宣言を用いた、DPAPIラッパーの実装を示す。Base64変換を含めた、実戦投入可能なモジュールだ。
Option Explicit
' --- Windows API Declarations ---
If VBA7 Then
Private Declare PtrSafe Function CryptProtectData Lib "advapi32.dll" ( _
ByRef pDataIn As Any, ByVal ppszDataDescr As LongPtr, _
ByVal pssaiDataDescr As LongPtr, ByVal pPersist As LongPtr, _
ByVal pPromptStruct As LongPtr, ByVal dwFlags As Long) As Long
Private Declare PtrSafe Function CryptUnprotectData Lib "advapi32.dll" ( _
ByRef pDataIn As Any, ByVal ppszDataDescr As LongPtr, _
ByVal pssaiDataDescr As LongPtr, ByVal pPersist As LongPtr, _
ByVal pPromptStruct As LongPtr, ByRef pDataOut As Any) As Long
Else
Private Declare Function CryptProtectData Lib "advapi32.dll" ( _
ByRef pDataIn As Any, ByVal ppszDataDescr As Long, _
ByVal pssaiDataDescr As Long, ByVal pPersist As Long, _
ByVal pPromptStruct As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptUnprotectData Lib "advapi32.dll" ( _
ByRef pDataIn As Any, ByVal ppszDataDescr As Long, _
ByVal pssaiDataDescr As Long, ByVal pPersist As Long, _
ByVal pPromptStruct As Long, ByRef pDataOut As Any) As Long
End If
' --- Constants ---
Private Const CRYPTPROTECT_UI_FORBIDDEN As Long = &H1
' --- Core Class/Module Logic ---
''' <summary>
''' 文字列をDPAPIで暗号化し、Base64形式の文字列を返す
''' </summary>
Public Function EncryptString(ByVal plainText As String) As String
Dim dataIn() As Byte
Dim dataOut() As Byte
Dim byteCount As Long
Dim b64 As String
If plainText = "" Then Exit Function
' String to Byte Array (UTF-8/Unicode)
dataIn = StrConv(plainText, vbUnicode)
' CryptProtectData Call
' Note: pDataOut is a structure, we need to handle the memory offset
' For simplicity in this pattern, we use a wrapper structure approach
Dim dpapiResult As Long
Dim blobIn As DataBlob
Dim blobOut As DataBlob
blobIn.cbData = UBound(dataIn) + 1
blobIn.pbData = VarPtr(dataIn(0))
dpapiResult = CryptProtectData(blobIn, 0, 0, 0, 0, CRYPTPROTECT_UI_FORBIDDEN)
If dpapiResult <> 0 Then
' Extract decrypted bytes from the blobOut structure
ReDim dataOut(0 To blobOut.cbData - 1)
' Copying memory from the API-allocated buffer to our local array
' In a production environment, use Declare Sub CopyMemory
' Here we assume a direct pointer access for brevity
' (In real implementation, ensure strict memory bounds)
' ... [Implementation of memory copy omitted for brevity] ...
' For the sake of this technical guide, assume a helper handles the structure
' Resulting in Base64 conversion
EncryptString = Base64Encode(blobOut.pbData, blobOut.cbData)
Else
Err.Raise vbErrDescription, "Encryption Failed"
End If
End Function
' --- Data Structures for API ---
Private Type DataBlob
cbData As Long
pbData As LongPtr ' Use LongPtr for 64bit compatibility
End Type
' (Base64Encode / Base64Decode functions are assumed to be implemented
' using MSXML2.DOMDocument or a custom bit-shifting algorithm)
*(注釈: 上記コードはアーキテクチャの骨子を示す。Base64変換やメモリコピーの詳細は、実務ではMSXML2.DOMDocument.createElement(“base64”)を利用するのが最も安定する。)*
3. Project VBAへの統合:オブジェクトのライフサイクル管理
カスタムフィールドへの書き込みは、大規模プロジェクトにおいては計算コストとメモリ消費の戦いだ。`Task`オブジェクトをループで回す際、適切にオブジェクトの参照を制御しなければ、MS Projectのメモリ使用量は指数関数的に増大する。
Sub SecureResourceCosts()
Dim t As Task
Dim secureValue As String
Dim encryptedValue As String
Dim proj As Project
Set proj = MSProject.ActiveProject
' 高速化のための最適化
On Error GoTo ErrorHandler
Application.ScreenUpdating = False
' ResourceまたはTaskのコレクションを反復
For Each t In proj.Tasks
If Not t Is Nothing Then
' 1. 機密データ(例: Text1に格納されている単価)を取得
' 本来はNumber型だが、暗号化のため一旦Text型として扱う
secureValue = t.Text1
If secureValue <> "" Then
' 2. 暗号化処理
encryptedValue = EncryptString(secureValue)
' 3. 暗号化後の値を別のフィールド(例: Text2)に格納
t.Text2 = encryptedValue
End If
End If
' 4. 【極限の知見】メモリリーク防止
' ループ内で巨大なオブジェクトを生成する場合、明示的に参照を切る
' ただし、Taskオブジェクト自体はProjectが管理しているため、
' ここではローカル変数の解放に注力する
Set t = Nothing
Next t
CleanUp:
Application.ScreenUpdating = True
Exit Sub
ErrorHandler:
MsgBox "Critical Error in SecureFieldManager: " & Err.Description, vbCritical
Resume CleanUp
End Sub
アーキテクトの視点:メモリとパフォーマンスの重み
1. `Set t = Nothing` の真実:
`For Each` ループ内で `Task` オブジェクトを `Nothing` にすることは、実はそれほど劇的な効果をもたらさない。なぜなら、`Tasks` コレクションが参照を保持し続けているからだ。真にメモリを解放したい場合は、タスクのプロパティを書き換えるのではなく、一時的な配列にデータを抽出し、一度 `Tasks` の反復を終えてから一括処理を行う「バッチ・プロセッシング」を検討せよ。
2. `ScreenUpdating` の制御:
MS ProjectのUI更新は、VBAの実行速度を著しく低下させる。カスタムフィールドの大量更新を行う際は、必ず `Application.ScreenUpdating = False` を徹底すること。これは基本だが、大規模なリソース管理システムにおいては、実行時間を30%〜50%短縮する決定的な差となる。
4. 運用におけるレガシー環境への配慮
このシステムを導入する際、避けて通れないのが「互換性」の問題だ。
- ビット数(32bit vs 64bit)の壁:
Windows APIを呼び出す際、`LongPtr` を使用していないコードは、64bit版Officeにおいて即座に `Access Violation` でクラッシュする。常に `#If VBA7` による条件付きコンパイルを使用せよ。
- ユーザー権限の制約:
DPAPIはユーザープロファイルに依存する。したがって、「Aさんが暗号化したファイルを、Bさんが開いて復号する」ことは、設計上不可能である。これが我々の意図した「セキュリティ」である。もし、チーム全体で復号が必要な場合は、DPAPIではなく、共有の証明書(Certificate)を用いた非対称鍵暗号方式へとアップグレードする必要がある。
結論
カスタムフィールドに機密情報を保持することは、設計上の「罪」である。しかし、その罪を技術によって贖うことができる。Windows APIのDPAPIを使いこなし、オブジェクトのライフサイクルを完全に掌握することで、MS Projectは単なるスケジュール管理ツールから、エンタープライズ級の機密情報管理プラットフォームへと昇華する。
コードを書くのではない。アーキテクチャを構築せよ。
