⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 secure.cpp

📁 采用c++实现的文件加密程序
💻 CPP
字号:
// Secure.cpp : Implementation of CSecure
#include "stdafx.h"
#include "SecureStorage.h"
#include "Secure.h"

/////////////////////////////////////////////////////////////////////////////
// CSecure

STDMETHODIMP CSecure::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_ISecure
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CSecure::Encrypt(BSTR key, BSTR value, BSTR * cipher)
{
	rc4_key rc4key;

	// generating the key
	unsigned char* pKey = (unsigned char*)malloc(bstr_t(key).length()+1);
	memset(pKey, 0, bstr_t(key).length()+1);
	strncpy((char*)pKey, bstr_t(key), bstr_t(key).length());

	prepare_key(pKey, strlen((char*)pKey), &rc4key);
	
	// performing encryption
	unsigned char* pValue = (unsigned char*)malloc(bstr_t(value).length()+1);
	memset(pValue, 0, bstr_t(value).length()+1);
	strncpy((char*)pValue, bstr_t(value), bstr_t(value).length());	
	
	rc4(pValue, strlen((char*)pValue), &rc4key);

	*cipher = bstr_t((char*)pValue).copy();

	free(pKey);
	free(pValue);

	return S_OK;
}

STDMETHODIMP CSecure::Decrypt(BSTR key, BSTR cipher, BSTR * clearText)
{
	return Encrypt(key, cipher, clearText);
}

STDMETHODIMP CSecure::get_RegistryKey(BSTR *pVal)
{
	*pVal = m_registryPath.copy();

	return S_OK;
}

STDMETHODIMP CSecure::put_RegistryKey(BSTR newVal)
{
	if (0 < bstr_t(newVal).length()) {
		m_Registry.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\SecureStorage\\" + bstr_t(newVal));
	} else {
		m_Registry.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\SecureStorage");
	}
	
	m_registryPath = newVal;

	return S_OK;
}

STDMETHODIMP CSecure::EncryptToRegistry(BSTR key, BSTR name, BSTR value)
{
	try {
		BSTR cipher;
		Encrypt(key, value, &cipher);

		m_Registry.SetValue(bstr_t(cipher), bstr_t(name));
	} catch (...) {

	}
	return S_OK;
}

STDMETHODIMP CSecure::DecryptFromRegistry(BSTR key, BSTR name, BSTR *value)
{
	try {
		DWORD count = 1024;
		char fetched[1024];
		memset(fetched, 0, 1024);

		m_Registry.QueryValue(fetched, bstr_t(name), &count);

		Decrypt(key, bstr_t(fetched), value);
	} catch (...) {

	}
	return S_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -