public static byte[] Encrypt(Uri uri, string templateString, string data) { MSIPC.ConnectionInfo connectionInfo = new MSIPC.ConnectionInfo(uri, uri); object cred = GetOAuthContext(null); // object cred = GetCertData(); Collection <MSIPC.TemplateInfo> templates = MSIPC.SafeNativeMethods.IpcGetTemplateList( connectionInfo, forceDownload: false, suppressUI: true, offline: false, hasUserConsent: true, parentWindow: null, cultureInfo: null, credentialType: cred); MSIPC.TemplateInfo templateToUse = templates[0]; foreach (MSIPC.TemplateInfo info in templates) { if (info.TemplateId == templateString) { templateToUse = info; break; } } templateString = templateToUse.TemplateId; if (!string.IsNullOrEmpty(templateString)) { MSIPC.SafeInformationProtectionKeyHandle keyHandle = null; byte[] license = MSIPC.SafeNativeMethods.IpcSerializeLicense( templateString, MSIPC.SerializeLicenseFlags.KeyNoPersist, suppressUI: true, offline: false, hasUserConsent: true, parentWindow: null, keyHandle: out keyHandle, credentialType: cred); using (MemoryStream outputStream = new MemoryStream()) { int blockSize = MSIPC.SafeNativeMethods.IpcGetKeyBlockSize(keyHandle); byte[] bytes = Encoding.UTF8.GetBytes(data); MSIPC.SafeNativeMethods.IpcEncrypt(keyHandle, 0, true, ref bytes); byte[] lengthBytes = BitConverter.GetBytes(license.Length); int length = 0; while (length + lengthBytes.Length < 4) { outputStream.WriteByte(0); length++; } outputStream.Write(lengthBytes, 0, lengthBytes.Length); outputStream.Write(license, 0, license.Length); outputStream.Write(bytes, 0, bytes.Length); return(outputStream.ToArray()); } } return(null); }
public TemplateIssuer(ConnectionInfo connectionInfo, string displayName, bool allowFromScratch) { m_connectionInfo = connectionInfo; m_displayName = displayName; m_allowFromScratch = allowFromScratch; }
// IpcGetTemplateIssuerList() - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535266(v=vs.85).aspx public static Collection<TemplateIssuer> IpcGetTemplateIssuerList( ConnectionInfo connectionInfo, bool defaultServerOnly, bool suppressUI, bool offline, bool hasUserConsent, System.Windows.Forms.Form parentForm, SymmetricKeyCredential symmKey = null) { Collection<TemplateIssuer> templateIssuerList = null; int hr = 0; uint flags = 0; if (defaultServerOnly) { flags |= Convert.ToUInt32(GetTemplateIssuerListFlags.DefaultServerOnly); } IpcConnectionInfo ipcConnectionInfo = ConnectionInfoToIpcConnectionInfo(connectionInfo); SafeIpcPromptContext ipcPromptContext = CreateIpcPromptContext(suppressUI, offline, hasUserConsent, parentForm, symmKey); IntPtr ipcTemplateIssuerListPtr = IntPtr.Zero; try { hr = UnsafeNativeMethods.IpcGetTemplateIssuerList( ipcConnectionInfo, flags, (IpcPromptContext)ipcPromptContext, IntPtr.Zero, out ipcTemplateIssuerListPtr); ThrowOnErrorCode(hr); templateIssuerList = new Collection<TemplateIssuer>(); MarshalIpcTemplateIssuerListToManaged(ipcTemplateIssuerListPtr, templateIssuerList); } finally { UnsafeNativeMethods.IpcFreeMemory(ipcTemplateIssuerListPtr); ReleaseIpcPromptContext(ipcPromptContext); } return templateIssuerList; }
private static IpcConnectionInfo ConnectionInfoToIpcConnectionInfo(ConnectionInfo connectionInfo) { IpcConnectionInfo ipcConnectionInfo = null; if (connectionInfo != null) { ipcConnectionInfo = new IpcConnectionInfo(); if (connectionInfo.IntranetUrl != null) ipcConnectionInfo.IntranetUrl = connectionInfo.IntranetUrl.OriginalString; if (connectionInfo.ExtranetUrl != null) ipcConnectionInfo.ExtranetUrl = connectionInfo.ExtranetUrl.OriginalString; } return ipcConnectionInfo; }
private static ConnectionInfo IpcConnectionInfoToConnectionInfo(IpcConnectionInfo ipcConnectionInfo) { ConnectionInfo connectionInfo = null; if (ipcConnectionInfo == null) { connectionInfo = null; } else if (String.IsNullOrEmpty(ipcConnectionInfo.ExtranetUrl) && String.IsNullOrEmpty(ipcConnectionInfo.IntranetUrl)) { connectionInfo = null; } else { Uri extranetUrl = null; if (!String.IsNullOrEmpty(ipcConnectionInfo.ExtranetUrl)) extranetUrl = new Uri(ipcConnectionInfo.ExtranetUrl); Uri intranetUrl = null; if (!String.IsNullOrEmpty(ipcConnectionInfo.IntranetUrl)) intranetUrl = new Uri(ipcConnectionInfo.IntranetUrl); connectionInfo = new ConnectionInfo(extranetUrl, intranetUrl); } return connectionInfo; }
// IpcGetTemplateList() - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535267(v=vs.85).aspx public static Collection<TemplateInfo> IpcGetTemplateList( ConnectionInfo connectionInfo, bool forceDownload, bool suppressUI, bool offline, bool hasUserConsent, System.Windows.Forms.Form parentForm, CultureInfo cultureInfo, SymmetricKeyCredential symmKey = null) { Collection<TemplateInfo> templateList = null; int hr = 0; uint flags = 0; if (forceDownload) { flags |= Convert.ToUInt32(GetTemplateListFlags.ForceDownload); } uint lcid = 0; if (null != cultureInfo) { lcid = (uint)(cultureInfo.LCID); } IpcConnectionInfo ipcConnectionInfo = ConnectionInfoToIpcConnectionInfo(connectionInfo); SafeIpcPromptContext ipcPromptContext = CreateIpcPromptContext(suppressUI, offline, hasUserConsent, parentForm, symmKey); IntPtr ipcTilPtr = IntPtr.Zero; try { hr = UnsafeNativeMethods.IpcGetTemplateList( ipcConnectionInfo, flags, lcid, (IpcPromptContext)ipcPromptContext, IntPtr.Zero, out ipcTilPtr); ThrowOnErrorCode(hr); templateList = new Collection<TemplateInfo>(); MarshalIpcTilToManaged(ipcTilPtr, templateList); } finally { UnsafeNativeMethods.IpcFreeMemory(ipcTilPtr); ReleaseIpcPromptContext(ipcPromptContext); } return templateList; }