Skip to content

Instantly share code, notes, and snippets.

@alecgorge
Created January 17, 2012 00:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alecgorge/1623895 to your computer and use it in GitHub Desktop.
Save alecgorge/1623895 to your computer and use it in GitHub Desktop.
notepad++ nppftp decryption routine
// Encryption.cpp
#include <openssl/des.h>
char * Encryption::_DefaultKey = NULL;
bool Encryption::_IsDefaultKey = true;
const char * defaultString = "NppFTP00"; //must be 8 in length
const size_t Encryption::KeySize = 8;
int Encryption::Init() {
_DefaultKey = new char[KeySize];
strncpy(_DefaultKey, defaultString, KeySize);
return 0;
}
char* Encryption::Decrypt(const char * key, int keysize, const char * data, bool addZero) {
int size = strlen(data);
char * encrdata = SU::HexToData(data, size, false);
if (!encrdata)
return NULL;
size = size/2;
char * decdata = DES_encrypt(key, keysize, encrdata, size, addZero, DES_DECRYPT);
SU::FreeChar(encrdata);
return decdata;
}
char* Encryption::DES_encrypt(const char * key, int keysize, const char * data, int size, bool addZero, int type) {
char keybuf[KeySize];
if (key == NULL) {
memcpy(keybuf, _DefaultKey, KeySize);
keysize = KeySize;
} else {
if (keysize == -1)
keysize = strlen(key); //zero terminator NOT included
if (keysize > KeySize)
keysize = KeySize;
memcpy(keybuf, key, keysize);
for(size_t i = keysize; i < KeySize; i++)
keybuf[i] = 0;
}
if (size == -1)
size = strlen(data);
char * decrypted = new char[size+(addZero?1:0)];
if (!decrypted)
return NULL;
if (addZero)
decrypted[size] = 0;
// Prepare the key for use with DES_cfb64_encrypt
DES_cblock key2;
DES_key_schedule schedule;
memcpy(key2, keybuf, KeySize);
DES_set_odd_parity(&key2);
DES_set_key_checked(&key2, &schedule);
// Decryption occurs here
int n = 0;
DES_cfb64_encrypt((unsigned char*) data, (unsigned char *)decrypted, size, &schedule, &key2, &n, type);
return decrypted;
}
// StringUtils.cpp
char* SU::HexToData(const char * hex, int len, bool addZero) {
if (len == -1)
len = strlen(hex);
if (len%2 != 0)
return NULL;
len = len/2;
unsigned char * data = new unsigned char[len + (addZero?1:0)];
for(int i = 0; i < len; i++) {
data[i] = 0;
if (hex[i*2] <= '9')
data[i] += (hex[i*2] - '0') * 16;
else
data[i] += ((hex[i*2] - 'A') + 10) * 16;
if (hex[i*2+1] <= '9')
data[i] += (hex[i*2+1] - '0');
else
data[i] += (hex[i*2+1] - 'A') + 10;
}
if (addZero) {
data[len] = 0;
}
return (char*)data;
}
int SU::FreeChar(char * string) {
if (!string)
return -1;
delete [] string;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment