int __stdcall dencrypt_3des(unsigned char* key,long keylen,unsigned char *data,short datalen,unsigned char *Des_result) { unsigned char szkey[9]; unsigned char szplain[9]; unsigned char szcipher[9]; if(keylen != 16) return INVALID_KEY_LEN; if(datalen != 8) return INVALID_DATA_LEN; memset(szkey,0,sizeof(szkey)); memset(szplain,0,sizeof(szplain)); memset(szcipher,0,sizeof(szcipher)); memcpy(szkey,key,8); memcpy(szcipher,data,8); deskey(szkey,1); Ddes(szcipher,szplain); memcpy(szkey,key+8,8); deskey(szkey,0); Ddes(szplain,szcipher); memcpy(szkey,key,8); deskey(szkey,1); Ddes(szcipher,szplain); memcpy(Des_result,szplain,8); return 0; }
char * vncDecryptPasswdFromFile(char *fname) { FILE *fp; int i, ch; unsigned char *passwd = (unsigned char *)malloc(9); if (strcmp(fname, "-") != 0) { if ((fp = fopen(fname,"r")) == NULL) return NULL; } else { fp = stdin; } for (i = 0; i < 8; i++) { ch = getc(fp); if (ch == EOF) break; passwd[i] = ch; } if (fp != stdin) fclose(fp); if (i != 8) /* Could not read eight bytes */ return NULL; deskey(s_fixedkey, DE1); des(passwd, passwd); passwd[8] = 0; return (char *)passwd; }
char * vncDecryptPasswdFromFile(char *fname) { FILE *fp; int i, ch; unsigned char *passwd = (unsigned char *)malloc(9); if ((fp = fopen(fname,"r")) == NULL) return NULL; for (i = 0; i < 8; i++) { ch = getc(fp); if (ch == EOF) { fclose(fp); return NULL; } passwd[i] = ch; } fclose(fp); deskey(fixedkey, DE1); des(passwd, passwd); passwd[8] = 0; return (char *)passwd; }
int vncEncryptAndStorePasswd(char *passwd, char *fname) { FILE *fp; int i; unsigned char encryptedPasswd[8]; if ((fp = fopen(fname,"w")) == NULL) return 1; chmod(fname, S_IRUSR|S_IWUSR); /* pad password with nulls */ for (i = 0; i < 8; i++) { if (i < strlen(passwd)) { encryptedPasswd[i] = passwd[i]; } else { encryptedPasswd[i] = 0; } } /* Do encryption in-place - this way we overwrite our copy of the plaintext password */ deskey(fixedkey, EN0); des(encryptedPasswd, encryptedPasswd); for (i = 0; i < 8; i++) { putc(encryptedPasswd[i], fp); } fclose(fp); return 0; }
int main() { char *key = "browsers"; unsigned char data[16]; int j; FILE *in, *out; deskey(key, EN0); in = fopen("challenge", "r"); if (!in) { printf("failed opening challenge\n"); exit(1); } fread(data, 1, 16, in); fclose(in); for (j=0; j<16; j += 8) { des(data+j, data+j); } out = fopen("out", "w+"); if (!out) { printf("failed opening out\n"); exit(1); } fwrite(data, 1, 16, out); fclose(out); return 0; }
int DES::decrypt ( char key[8], char* data, int blocks ) { if ((!data)||(blocks<1)) return 0; deskey ( (unsigned char *)key, DECRYPT ); des ( (unsigned char *)data, (unsigned char *)data, blocks); return 1; };
int CHttpDes::des_cbc_pkcs7_decrypt(uchar* from, int nLength, uchar * to, uchar key[], uchar iv[]) { if(nLength % 8) { return 0; //数据不正确 } //XOR uchar preEnc[8],buffer[8]; memcpy(preEnc,iv,8); deskey(key,DE1); int i = 0; for(; i<nLength; i+=8) { uchar* ps = from + i; uchar* pd = to + i; des(ps,buffer); //XOR for(int j = 0; j < 8; ++j) { buffer[j] ^= preEnc[j]; } if(nLength - i > 8) { //保存前一个输出 memcpy(preEnc, ps,8); memcpy(pd,buffer,sizeof(buffer)); } else { //去除数据尾 uchar chEnd = buffer[sizeof(buffer) - 1]; if(chEnd > 0 && chEnd < 9) { //有可能是填充字符,去除掉 for(int j = sizeof(buffer) - 1; j >= (int)(sizeof(buffer) - chEnd); --j) { if(buffer[j] != chEnd) return 0; } int nSize =nLength - chEnd; memcpy(pd, buffer, sizeof(buffer) - chEnd); return nLength - chEnd; } else { //数据格式不正确 return 0; } } } return 0; }
void rfb_crypt(CARD8 *dst_buf, CARD8 *src_buf, unsigned char *password) { unsigned char key[8]; memset(key, 0, 8); strncpy((char *)key, (char *)password, 8); deskey(key, EN0); des(src_buf, dst_buf); des(src_buf + 8, dst_buf + 8); }
int __stdcall dencrypt_des(unsigned char* key,long keylen,unsigned char *data,short datalen,unsigned char *Des_result) { if(keylen != 8) return INVALID_KEY_LEN; if(datalen != 8) return INVALID_DATA_LEN; deskey(key,1); Ddes(data,Des_result); return 0; }
/*------------------------------------------------------------------------- Write the dynatab to the given file in a format encrypted with key, suitable for restoration with dynatab_thaw_encrypted. -------------------------------------------------------------------------*/ void dynatab_freeze_encrypted(dynatab_t *tab, FILE *fp, const unsigned char key[8]) { dynatab_encrypted_freeze_t d; size_t encrypted_unit; int nwritten; int n; size_t chunk; char buf[crypttab_RW_BUFFER_SIZE]; char *pbuf; char unitbuf[crypttab_RW_BUFFER_SIZE]; d.magic = dynatab_encrypted_MAGIC; d.n_used = tab->n_used; d.unit = tab->unit; /* round up to nearest multiple of 8 */ encrypted_unit = d.unit + 7 - (d.unit + 7) % 8; assert(encrypted_unit <= crypttab_RW_BUFFER_SIZE); assert(d.n_used <= 5000); DPRINT(("dynatab_freeze_encrypted: Saving %d elements of size %d (%d encrypted).\n", d.n_used, tab->unit, encrypted_unit)); nwritten = fwrite(&d, sizeof(d), 1, fp); if (nwritten != 1) { DPRINT(("dynatab_freeze_encrypted: Error writing info.\n")); return; } pbuf = buf; memset(unitbuf, 0, encrypted_unit); deskey(key, EN0); for (n = 0; n < tab->n_used; n++) { if (pbuf - buf + encrypted_unit > crypttab_RW_BUFFER_SIZE) { /* if the write buffer is full, write it to disk, then start * adding at the beginning of the buffer again */ nwritten = fwrite(buf, (pbuf - buf), 1, fp); if (nwritten != 1) { DPRINT(("dynatab_freeze_encrypted: Error writing unit %d (wrote %d x %d).\n", n, (pbuf - buf), nwritten)); return; /* need an error code? */ } pbuf = buf; } /* encrypt the entry and add it to the write buffer in 8 byte chunks */ memcpy(unitbuf, (char *)tab->buf + n * tab->unit, tab->unit); for (chunk = 0; chunk < encrypted_unit; chunk += 8, pbuf += 8) des(unitbuf + chunk, pbuf); } /* Write the last partial buffer to disk */ nwritten = fwrite(buf, (pbuf - buf), 1, fp); if (nwritten != 1) { DPRINT(("dynatab_freeze_encrypted: Error writing unit %d.\n", n)); return; /* need an error code? */ } }
/*! Initialize the LTC_DES block cipher @param key The symmetric key you wish to pass @param keylen The key length in bytes @param num_rounds The number of rounds desired (0 for default) @param skey The key in as scheduled by this function. @return CRYPT_OK if successful */ static int ltc_des_setup(const unsigned char *key, unsigned long keylen, int num_rounds, ccecb_ctx *skey) { ltc_des_keysched *des; des = (ltc_des_keysched *)skey; if (num_rounds != 0 && num_rounds != 16) { return -1; /* CRYPT_INVALID_ROUNDS; */ } if (keylen != 8) { return -1; /* CRYPT_INVALID_KEYSIZE; */ } deskey(key, EN0, des->ek); deskey(key, DE1, des->dk); return 0; /* CRYPT_OK; */ }
/* * Decrypt a password. Returns a pointer to a newly allocated * string containing the password or a null pointer if the password could * not be retrieved for some reason. */ char * vncDecryptPasswd(unsigned char *inouttext) { unsigned char *passwd = (unsigned char *)malloc(9); deskey(fixedkey, DE1); des(inouttext, passwd); passwd[8] = 0; return (char *)passwd; }
/*! Initialize the 3LTC_DES-EDE block cipher @param key The symmetric key you wish to pass @param keylen The key length in bytes @param num_rounds The number of rounds desired (0 for default) @param skey The key in as scheduled by this function. @return CRYPT_OK if successful */ static int ltc_des3_setup(const unsigned char *key, unsigned long keylen, int num_rounds, ccecb_ctx * skey) { ltc_des3_keysched *des3; des3 = (ltc_des3_keysched *)skey; if(num_rounds != 0 && num_rounds != 16) { return -1; /* CRYPT_INVALID_ROUNDS; */ } if (keylen != 24) { return -1; /* CRYPT_INVALID_KEYSIZE; */ } deskey(key, EN0, des3->ek[0]); deskey(key+8, DE1, des3->ek[1]); deskey(key+16, EN0, des3->ek[2]); deskey(key, DE1, des3->dk[2]); deskey(key+8, EN0, des3->dk[1]); deskey(key+16, DE1, des3->dk[0]); return 0; /* CRYPT_OK; */ }
int cpu_calc_descrypt1(u_char *factor, u_char *key, u_char key_id, size_t key_len) { /* DBG("using cpu descrypt.\n"); */ u_char keybuf[16] = {0}; u_char factorbuf[16] = {0}; switch(key_id) { case 0x0D: /* DBG("Gen key, id: 0x0D\n"); */ memcpy(keybuf, get_netpara()->CardKey, 8); memcpy(factorbuf, factor, 8); deskey(keybuf, EN0); OneDes(factorbuf); deskey(factorbuf, EN0); OneDes(key); return 0; case 0x0E: /* DBG("Gen key, id: 0x0E\n"); */ memcpy(keybuf, get_netpara()->CardKey, 12); memcpy(factorbuf, factor, 16); *((u_int *)(&factorbuf[8])) = ~(*((u_int *)(&factorbuf[0]))); *((u_int *)(&factorbuf[12])) = ~(*((u_int *)(&factorbuf[4]))); stntripdes(factorbuf,keybuf); /* 分散因子前8字节,密钥 */ stntripdes(&factorbuf[8],keybuf); /* 分散因子后8字节,密钥 */ stntripdes(key,factorbuf); /* 随机数,XdataKeyBuf中是过程密钥 */ return 0; default: return -1; } }
int DES::yencrypt ( char key[8], char* data, int size ) { if ((!data)||(size<1)) return 0; // The last char of data is bitwise complemented and filled the rest // buffer.If size is 16, it will extend to 24,and 17 still 24. char lastChar = *(data+size-1); int blocks = size/8+1; memset (data+size, ~lastChar, blocks*8-size); deskey ( (unsigned char *)key, ENCRYPT ); return encrypt ( data, data, blocks); };
/* * marscha@2006 * Encrypt bytes[length] in memory using key. * Key has to be 8 bytes, length a multiple of 8 bytes. */ void vncEncryptBytes2(unsigned char *where, const int length, unsigned char *key) { int i, j; deskey(key, EN0); for (i = 0; i< 8; i++) where[i] ^= key[i]; des(where, where); for (i = 8; i < length; i += 8) { for (j = 0; j < 8; j++) where[i + j] ^= where[i + j - 8]; des(where + i, where + i); } }
int CHttpDes::des_cbc_pkcs7_encrypt(uchar* from, int nLength, uchar * to, uchar key[],uchar iv[]) { //uchar buffer[8]; int nSize = nLength % 8 ?(nLength + 7) / 8 * 8 : nLength + 8; if(to == NULL) { //计算长度 return nSize; } else { deskey(key,EN0); uchar preEnc[8]; memcpy(preEnc,iv,8); //加密块 int i=0; for(; i < nSize; i+=8) { uchar* ps = from + i; uchar* pd = to + i; if(nSize - i > 8) { //XOR for(int j = 0; j < 8; ++j) { preEnc[j] ^= *(ps + j); } } else { //XOR for(int j = 0; j < nLength - i; ++j) { preEnc[j] ^= *(ps + j); } for(int j = nLength - i; j < 8; ++j) { preEnc[j] ^= nSize - nLength; } } des(preEnc,pd); //保存前一个输出 memcpy(preEnc, pd,8); } return i; } }
/* * marscha@2006 * Decrypt bytes[length] in memory using key. * Key has to be 8 bytes, length a multiple of 8 bytes. */ void vncDecryptBytes(unsigned char *where, const int length, const unsigned char *key) { int i, j; deskey((unsigned char*) key, DE1); for (i = length - 8; i > 0; i -= 8) { des(where + i, where + i); for (j = 0; j < 8; j++) where[i + j] ^= where[i + j - 8]; } /* i = 0 */ des (where, where); for (i = 0; i < 8; i++) where[i] ^= key[i]; }
int vncDecryptPasswdFromFile2(char *fname, char *passwdFullControl, char *passwdViewOnly) { FILE *fp; int i, ch; char passwd[16]; if (strcmp(fname, "-") != 0) { if ((fp = fopen(fname,"r")) == NULL) return 0; /* Could not open the file */ } else { fp = stdin; } for (i = 0; i < 16; i++) { ch = getc(fp); if (ch == EOF) break; passwd[i] = ch; } if (fp != stdin) fclose(fp); if (i < 8) return 0; /* Could not read eight bytes */ deskey(s_fixedkey, DE1); /* Decoding first (full-control) password */ if (passwdFullControl != NULL) { des(passwd, passwd); memcpy(passwdFullControl, passwd, 8); passwdFullControl[8] = '\0'; } /* Decoding second (view-only) password if available */ if (i == 16 && passwdViewOnly != NULL) { des(&passwd[8], &passwd[8]); memcpy(passwdViewOnly, &passwd[8], 8); passwdViewOnly[8] = '\0'; } /* Destroying our copy of clear-text passwords */ memset(passwd, 0, 16); return (i < 16) ? 1 : 2; }
void des3key(uchar *hexkey, short mode, void **ksa) { ulong *ks; uchar *first, *third; short revmod; *ksa = ks = (ulong *)pgp_malloc(96 * sizeof(ulong)); if(mode == EN0) { revmod = DE1; first = hexkey; third = &hexkey[16]; } else { revmod = EN0; first = &hexkey[16]; third = hexkey; } deskey(first, mode, ks); deskey(&hexkey[8], revmod, &ks[32]); deskey(third, mode, &ks[64]); }
int DES::decrypt (const char key[8], char* data, int blocks, int mode) { if ((!data)||(blocks<1)) return 0; deskey((unsigned char*)key, DECRYPT); if(mode == 1) { cbc_dec((unsigned char*)data, (unsigned char*)data, blocks); } else { des((unsigned char*)data, (unsigned char*)data, blocks); } return 1; }
char *vncEncryptPasswd(const char *passwd) { unsigned char encryptedPasswd[8]; char *returnPass; // Handles copying 0's into extra space strncpy((char *)encryptedPasswd, passwd, 8); /* Do encryption in-place - this way we overwrite our copy of the plaintext password */ deskey(fixedkey, EN0); des(encryptedPasswd, encryptedPasswd); returnPass = malloc(8); strncpy(returnPass, (const char *) encryptedPasswd, 8); return (char *)returnPass; }
void vncEncryptPasswd(unsigned char *encryptedPasswd, char *passwd) { unsigned int i; /* pad password with nulls */ for (i = 0; i < MAXPWLEN; i++) { if (i < strlen(passwd)) encryptedPasswd[i] = passwd[i]; else encryptedPasswd[i] = 0; } /* Do encryption in-place - this way we overwrite our copy of the plain-text password */ deskey(fixedkey, EN0); des(encryptedPasswd, encryptedPasswd); }
void vncEncryptBytes(unsigned char *bytes, char *passwd) { unsigned char key[8]; int i; /* key is simply password padded with nulls */ for (i = 0; i < 8; i++) { if (i < strlen(passwd)) { key[i] = passwd[i]; } else { key[i] = 0; } } deskey(key, EN0); for (i = 0; i < CHALLENGESIZE; i += 8) { des(bytes + i, bytes + i); } }
char *vncDecryptPasswd(const unsigned char *encryptedPasswd) { unsigned int i; unsigned char *passwd = (unsigned char *)malloc(MAXPWLEN + 1); memcpy(passwd, encryptedPasswd, MAXPWLEN); for (i = 0; i < MAXPWLEN; i++) passwd[i] = encryptedPasswd[i]; deskey(fixedkey, DE1); des(passwd, passwd); passwd[MAXPWLEN] = 0; return (char *)passwd; }
void CDESEncrypt::setKey(const char *pszKey, const unsigned short usType, const unsigned short usMode) { m_usMode = usMode; m_usType = usType; switch (m_usType) { case DESTYPE_DES: deskey((DESContext *)m_pContext, (unsigned char*)pszKey, usMode); break; case DESTYPE_D2DES: des2key((DESContext *)m_pContext, (unsigned char*)pszKey, usMode); break; case DESTYPE_D3DES: des3key((DESContext *)m_pContext, (unsigned char*)pszKey, usMode); break; } }
int CHttpDes::des_ecb_pkcs7_decrypt(uchar* from, int nLength, uchar * to, uchar key[]) { if(nLength % 8) return 0; //数据不正确 deskey(key,DE1); int i = 0; for(; i < nLength; i+=8) { if(nLength - i > 8) { des(from + i,to + i); } else { uchar endBuf[8]; des(from + i,endBuf); //去除数据尾 uchar chEnd = endBuf[7]; if(chEnd > 0 && chEnd < 9) { //有可能是填充字符,去除掉 for(int j = 7; j >= 8 - chEnd; --j) { if(endBuf[j] != chEnd) return 0; } memcpy(to + i, endBuf, 8 - chEnd); return i + 8 - chEnd; } else { return 0; } } } return 0; }
int DES::ydecrypt ( char key[8], char* data, int blocks, int* size ) { if ( (!data) || (blocks<1) ) return 0; deskey ( (unsigned char *)key, DECRYPT ); if ( !decrypt ( data, data, blocks) ) return 0; if ( size != 0 ) { int pos = blocks*8-1; char endChar = data[pos]; while ((pos>0)&&(data[pos]==endChar)) pos--; if ( data[pos] != ~endChar ) return 0; *size = pos+1; } return 1; };
int vncEncryptAndStorePasswd2(char *passwd, char *passwdViewOnly, char *fname) { FILE *fp; int i, bytesToWrite, bytesWrote; unsigned char encryptedPasswd[16] = { 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 }; if (strcmp(fname, "-") != 0) { fp = fopen(fname, "w"); if (fp == NULL) { return 0; } chmod(fname, S_IRUSR|S_IWUSR); } else { fp = stdout; } strncpy(encryptedPasswd, passwd, 8); if (passwdViewOnly != NULL) strncpy(encryptedPasswd + 8, passwdViewOnly, 8); /* Do encryption in-place - this way we overwrite our copies of plaintext passwords. */ deskey(s_fixedkey, EN0); des(encryptedPasswd, encryptedPasswd); if (passwdViewOnly != NULL) des(encryptedPasswd + 8, encryptedPasswd + 8); bytesToWrite = (passwdViewOnly == NULL) ? 8 : 16; bytesWrote = fwrite(encryptedPasswd, 1, bytesToWrite, fp); if (fp != stdout) { fclose(fp); } return (bytesWrote == bytesToWrite); }
/* * Encrypt some bytes in memory using a password. */ void vncEncryptBytes(unsigned char *where, const char *passwd) { unsigned char key[8]; int i; // key is simply password padded with nulls for (i = 0; i < 8; i++) { if (i < (int)strlen(passwd)) { key[i] = passwd[i]; } else { key[i] = 0; } } deskey(key, EN0); for (i = 0; i < CHALLENGESIZE; i += 8) { des(where+i, where+i); } }