Skip to content

Instantly share code, notes, and snippets.

@mokapyo
Created March 17, 2017 18:51
Show Gist options
  • Save mokapyo/25fa35612ced86560ae87eb98f44d66c to your computer and use it in GitHub Desktop.
Save mokapyo/25fa35612ced86560ae87eb98f44d66c to your computer and use it in GitHub Desktop.
// TestRMSDlg.cpp : 実装ファイル
//
#include "stdafx.h"
#include "TestRMS.h"
#include "TestRMSDlg.h"
#include "SelectDlg.h"
#include "afxdialogex.h"
#include <msipc.h>
BOOL CTestRMSDlg::EncryptFileRMS(){
// 初期化
HRESULT hr = 0;
CString msg;
// MSIPC.dll読み込み
hr = IpcInitialize();
if (FAILED(hr)) {
msg.Format(_T("Failed to initialize MSIPC.Are you sure the runtime is installed ? \n(result = %08X)\n"), hr);
MessageBox(msg, NULL, MB_OK);
return false;
}
// サーバーモードアプリケーションとしてセット
int mode = IPC_API_MODE_SERVER;
hr = IpcSetGlobalProperty(IPC_EI_API_MODE, &mode);
if (hr != S_OK) {
msg.Format(_T("IpcSetGlobalProperty failed. (result = %08X)"), hr);
MessageBox(msg, NULL, MB_OK);
return false;
}
// 暗号化
DWORD dwResult = ERROR_SUCCESS;
CString FilePath;
PCIPC_PROMPT_CTX pContext = NULL;
PVOID pvLicenseInfo = NULL;
PCIPC_TIL pcTil = NULL;
IPC_KEY_HANDLE hContentKey = NULL;
PIPC_BUFFER pSerializedLicense = NULL;
CString msg = _T("AzureEncrypt Success!!!");
UpdateData();
FilePath = m_FilePath;
do {
// 登録情報
IPC_CREDENTIAL_SYMMETRIC_KEY symKey = { 0 };
symKey.wszBase64Key = _T("X5Vj+N9bk38bwwWxGpNeHi6r32oiKxduEyrTAqGidQI=");
symKey.wszAppPrincipalId = _T("a7371501-27c2-4863-9c3f-6485c7d20503 ");
symKey.wszBposTenantId = _T("6e0da1ab-d715-48c9-91cc-1ae9952039fe");
// Create a credential structure.
IPC_CREDENTIAL cred = { 0 };
IPC_CONNECTION_INFO connectionInfo = { 0 };
connectionInfo.wszIntranetUrl = _T("https://f5c1abc4-600c-42cc-a24d-6e8006b3a323.rms.ap.aadrm.com/_wmcs/licensing");
connectionInfo.wszExtranetUrl = _T("https://f5c1abc4-600c-42cc-a24d-6e8006b3a323.rms.ap.aadrm.com/_wmcs/licensing");
// Set each member.
cred.dwType = IPC_CREDENTIAL_TYPE_SYMMETRIC_KEY;
cred.pcCertContext = (PCCERT_CONTEXT)&symKey;
// Create your prompt control.
IPC_PROMPT_CTX promptCtx = { 0 };
// Set each member.
promptCtx.cbSize = sizeof(IPC_PROMPT_CTX);
promptCtx.hwndParent = NULL;
promptCtx.dwFlags = IPC_PROMPT_FLAG_SILENT;
promptCtx.hCancelEvent = NULL;
promptCtx.pcCredential = &cred;
// テンプレ―トの取得
PCIPC_TIL pcTil = NULL;
dwResult = IpcGetTemplateList(&connectionInfo, IPC_GTIL_FLAG_DEFAULT_SERVER_ONLY, 0, &promptCtx, NULL, &pcTil);
if (dwResult != ERROR_SUCCESS) {
msg.Format(_T("IpcGetTemplateList failed (result = %08X)\n"), dwResult);
break;
}
// テンプレート選択画面表示
CSelectDlg SelectDlg(&pcTil,NULL);
SelectDlg.DoModal();
// 選択されたテンプレート情報を使用してライセンスのシリアル化
dwResult = IpcSerializeLicense((LPCVOID)pcTil->aTi[SelectDlg.m_SelectTemplateNo].wszID, IPC_SL_TEMPLATE_ID, 0, NULL, &hContentKey, &pSerializedLicense);
if (dwResult != ERROR_SUCCESS) {
msg.Format(_T("IpcSerializeLicense failed (result = %08X)\n"), dwResult);
break;
}
// ファイル暗号化
LPCWSTR outFilePath[1024];
dwResult = IpcfEncryptFile(m_FilePath,
(LPCVOID)pcTil->aTi[SelectDlg.m_SelectTemplateNo].wszID,
IPCF_EF_TEMPLATE_ID,
IPC_EF_FLAG_KEY_NO_PERSIST,
&promptCtx,
NULL,
outFilePath);
/**************** 復号の場合は同じ手順でこれに変えるだけ *******************
LPCWSTR outFilePath[1024];
UpdateData(FALSE);
dwResult = IpcfDecryptFile(m_FilePath,
IPCF_DF_FLAG_DEFAULT,
&promptCtx,
NULL,
outFilePath);
****************************************************************************/
if (dwResult != ERROR_SUCCESS) {
msg.Format(_T("IpcfEncryptFile failed. (result = %08X)"), dwResult);
}
} while (FALSE);
MessageBox(msg, _T("Result"), MB_OK);
return dwResult;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment