示例#1
0
int main(int argc, char *argv[])
{
	if (argc < 2) {
		printf("usage : %s  password\n", argv[0]);
		return 1;
	}

	char szCrypt[1024] = "";

	StrCrypt(argv[1], szCrypt);

	printf("%s\n", szCrypt);

	return 0;
}
void CryptSelection(CryptAction action)
{
    int currentEdit = -1;
    HWND hCurrentEditView = NULL;
    ULONG textLength = 0L, textLengthOut = 0L;
    TCHAR *textBuf = NULL, *textBufOut = NULL;

    // Prompt for crypt key
    INT_PTR ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGKEY), nppData._nppHandle, reinterpret_cast<DLGPROC>(DlgProcCryptKey));

    if(ret == 0)
    {
        return;
    }

    // Get edit view handle
    SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&currentEdit);
    if(currentEdit == 0) hCurrentEditView = nppData._scintillaMainHandle;
    else hCurrentEditView = nppData._scintillaSecondHandle;

    // Get selected text length
    long selectStart = (long)SendMessage(hCurrentEditView, SCI_GETSELECTIONSTART, 0, 0);
    long selectEnd = (long)SendMessage(hCurrentEditView, SCI_GETSELECTIONEND, 0, 0);

    if(selectEnd == 0 || (selectEnd - selectStart) == 0)
    {
        MessageBox(nppData._nppHandle, TEXT("No text was selected!"), TEXT("Error"), MB_OK);
        return;
    }

    textLength = (selectEnd - selectStart); textLength++;
    textLengthOut = textLength;
    if(action == Encrypt) textLengthOut *= 2;

    // Create input buffer  (round up to nearest 8 multiplier)
    while(textLength % 8 != 0)textLength++;
    textBuf = (TCHAR*)VirtualAlloc(NULL, textLength, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

    // Create output buffer (round up to nearest 8 multiplier)
    while(textLengthOut % 8 != 0)textLengthOut++;
    textBufOut = (TCHAR*)VirtualAlloc(NULL, textLengthOut, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

    // Get text range
    struct TextRange tr = {{selectStart, selectEnd}, reinterpret_cast<char*>(textBuf)};
    SendMessage(hCurrentEditView, SCI_GETTEXTRANGE, 0, (LPARAM)&tr);

    // Crypt the text
    if(action == Encrypt)
        StrCrypt((unsigned char*)textBuf, textLength, (unsigned char*)textBufOut, textLengthOut, (unsigned char*)cryptkey, Encrypt);
    else
        StrCrypt((unsigned char*)textBuf, textLength, (unsigned char*)textBufOut, textLengthOut, (unsigned char*)cryptkey, Decrypt);
    
    // Output to view
    SendMessage(hCurrentEditView, SCI_REPLACESEL, 0, (LPARAM)textBufOut);

    // Cleanup
    if (textBuf)
    {
        VirtualFree(textBuf, 0, MEM_RELEASE);
    }
    if (textBufOut)
    {
        VirtualFree(textBufOut, 0, MEM_RELEASE);
    }
}
//----------------------------------------------//
//-- STEP 4. DEFINE YOUR ASSOCIATED FUNCTIONS --//
//----------------------------------------------//
void CryptDoc(CryptAction action)
{
    int currentEdit = -1;
    HWND hCurrentEditView = NULL;
    ULONG textLength = 0L, textLengthOut = 0L;
    TCHAR *textBuf = NULL, *textBufOut = NULL;

    // Prompt for crypt key
    INT_PTR ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGKEY), nppData._nppHandle, reinterpret_cast<DLGPROC>(DlgProcCryptKey));

    if(ret == 0)
    {
        return;
    }

    // Get edit view handle
    SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&currentEdit);
    if(currentEdit == 0) hCurrentEditView = nppData._scintillaMainHandle;
    else hCurrentEditView = nppData._scintillaSecondHandle;
    
    // Get document text length
    textLength = (ULONG)SendMessage(hCurrentEditView, SCI_GETTEXTLENGTH, 0, 0);

    if(textLength == 0)
    {
        MessageBox(nppData._nppHandle, TEXT("The document is empty!"), TEXT("Error"), MB_OK);
        return;
    }

    textLength++;
    textLengthOut = textLength;

    if(action == Encrypt) textLengthOut *= 2;

    // Create input buffer  (round up to nearest 8 multiplier)
    while(textLength % 8 != 0)textLength++;
    textBuf = (TCHAR*)VirtualAlloc(NULL, textLength, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

    // Create output buffer (round up to nearest 8 multiplier)
    while(textLengthOut % 8 != 0)textLengthOut++;
    textBufOut = (TCHAR*)VirtualAlloc(NULL, textLengthOut, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

    // Get text
    SendMessage(hCurrentEditView, SCI_GETTEXT, textLength, (LPARAM)textBuf);

    // Crypt the text
    if(action == Encrypt)
        StrCrypt((unsigned char*)textBuf, textLength, (unsigned char*)textBufOut, textLengthOut, (unsigned char*)cryptkey, Encrypt);
    else
        StrCrypt((unsigned char*)textBuf, textLength, (unsigned char*)textBufOut, textLengthOut, (unsigned char*)cryptkey, Decrypt);

    // Output to view
    SendMessage(hCurrentEditView, SCI_SETTEXT, 0, (LPARAM)textBufOut);

    // Cleanup
    if(textBuf)
    {
        VirtualFree(textBuf, 0, MEM_RELEASE);
    }
    if (textBufOut)
    {
        VirtualFree(textBufOut, 0, MEM_RELEASE);
    }
}