void des_decrypt3(DES_LONG *data, des_key_schedule ks1, des_key_schedule ks2, des_key_schedule ks3) { register DES_LONG l,r; l=data[0]; r=data[1]; IP(l,r); data[0]=l; data[1]=r; /*des_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT); */ /*des_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT); */ /*des_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT); */ /* XID on 23-MAR-1999 */ /* Modified to use Triple DES. Key must be 24 bytes. */ des_encrypt((DES_LONG *)data,ks3,DES_DECRYPT); des_encrypt((DES_LONG *)data,ks2,DES_ENCRYPT); des_encrypt((DES_LONG *)data,ks1,DES_DECRYPT); l=data[0]; r=data[1]; FP(r,l); data[0]=l; data[1]=r; }/*end des_decrypt3 */
static void nt_password_hash_encrypted_with_block(const u8 *password_hash, const u8 *block, u8 *cypher) { des_encrypt(password_hash, block, cypher); des_encrypt(password_hash + 8, block + 7, cypher + 8); }
void des_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length, des_key_schedule schedule, des_cblock *ivec, int *num, int enc) { register DES_LONG v0,v1; register long l=length; register int n= *num; DES_LONG ti[2]; unsigned char *iv,c,cc; iv = &(*ivec)[0]; if (enc) { while (l--) { if (n == 0) { c2l(iv,v0); ti[0]=v0; c2l(iv,v1); ti[1]=v1; des_encrypt(ti,schedule,DES_ENCRYPT); iv = &(*ivec)[0]; v0=ti[0]; l2c(v0,iv); v0=ti[1]; l2c(v0,iv); iv = &(*ivec)[0]; } c= *(in++)^iv[n]; *(out++)=c; iv[n]=c; n=(n+1)&0x07; } } else { while (l--) { if (n == 0) { c2l(iv,v0); ti[0]=v0; c2l(iv,v1); ti[1]=v1; des_encrypt(ti,schedule,DES_ENCRYPT); iv = &(*ivec)[0]; v0=ti[0]; l2c(v0,iv); v0=ti[1]; l2c(v0,iv); iv = &(*ivec)[0]; } cc= *(in++); c=iv[n]; iv[n]=cc; *(out++)=c^cc; n=(n+1)&0x07; } } v0=v1=ti[0]=ti[1]=c=cc=0; *num=n; }
/** * challenge_response - ChallengeResponse() - RFC 2759, Sect. 8.5 * @challenge: 8-octet Challenge (IN) * @password_hash: 16-octet PasswordHash (IN) * @response: 24-octet Response (OUT) */ void challenge_response(const u8 *challenge, const u8 *password_hash, u8 *response) { u8 zpwd[7]; des_encrypt(challenge, password_hash, response); des_encrypt(challenge, password_hash + 7, response + 8); zpwd[0] = password_hash[14]; zpwd[1] = password_hash[15]; os_memset(zpwd + 2, 0, 5); des_encrypt(challenge, zpwd, response + 16); }
static void do_des_encrypt(struct crypto_instance *s, UINT32 length, const UINT8 *src, UINT8 *dst) { CAST(des_instance, self, s); des_encrypt(&self->ctx, length, dst, src); }
/** * LM_Response generates the LM response given a 16-byte password hash and the * challenge from the Type-2 message. * * @param hash * 16-byte password hash * @param challenge * 8-byte challenge from Type-2 message * @param response * 24-byte buffer to contain the LM response upon return */ static void LM_Response(const uint8_t *hash, const uint8_t *challenge, uint8_t *response) { uint8_t keybytes[21], k1[8], k2[8], k3[8]; memcpy(keybytes, hash, 16); ZapBuf(keybytes + 16, 5); des_makekey(keybytes , k1); des_makekey(keybytes + 7, k2); des_makekey(keybytes + 14, k3); des_encrypt(k1, challenge, response); des_encrypt(k2, challenge, response + 8); des_encrypt(k3, challenge, response + 16); }
int main(int argc, char** argv) { int i; for (i = 0 ; i < 80 ; i++ ) des_encrypt( (uint64_t*)buffer, (uint64_t*)(buffer+sizeof(buffer)) ); return 0; }
/* The input and output are loaded in multiples of 8 bits. * What this means is that if you hame numbits=12 and length=2 * the first 12 bits will be retrieved from the first byte and half * the second. The second 12 bits will come from the 3rd and half the 4th * byte. */ void des_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits, long length, des_key_schedule schedule, des_cblock *ivec, int enc) { register DES_LONG d0,d1,v0,v1,n=(numbits+7)/8; register DES_LONG mask0,mask1; register unsigned long l=length; register int num=numbits; DES_LONG ti[2]; unsigned char *iv; if (num > 64) return; if (num > 32) { mask0=0xffffffffL; if (num == 64) mask1=mask0; else mask1=(1L<<(num-32))-1; } else { if (num == 32) mask0=0xffffffffL; else mask0=(1L<<num)-1; mask1=0x00000000L; } iv = &(*ivec)[0]; c2l(iv,v0); c2l(iv,v1); if (enc) { while (l >= n) { l-=n; ti[0]=v0; ti[1]=v1; des_encrypt((DES_LONG *)ti,schedule,DES_ENCRYPT); c2ln(in,d0,d1,n); in+=n; d0=(d0^ti[0])&mask0; d1=(d1^ti[1])&mask1; l2cn(d0,d1,out,n); out+=n; /* 30-08-94 - eay - changed because l>>32 and * l<<32 are bad under gcc :-( */ if (num == 32) { v0=v1; v1=d0; } else if (num == 64) { v0=d0; v1=d1; } else if (num > 32) /* && num != 64 */ { v0=((v1>>(num-32))|(d0<<(64-num)))&0xffffffffL; v1=((d0>>(num-32))|(d1<<(64-num)))&0xffffffffL; } else /* num < 32 */ { v0=((v0>>num)|(v1<<(32-num)))&0xffffffffL; v1=((v1>>num)|(d0<<(32-num)))&0xffffffffL; } }
void verify_des(trace_s *trace) { unsigned char output[8]; int result; des_encrypt(trace->message, output, trace->key); result = memcmp(output, trace->ciphertext, 8); assert(result == 0); }
void testDES() { char t[100]="11111111"; int i; set_key(t); scanf("%s",t); des_encrypt(t,t); printf("密文长度为%d %s\n",strlen(t),t); des_decrypt(t,t); printf("明文长度为%d %s\n",strlen(t),t); }
int myEncrypt(char *str, char *key) { assert(str != NULL); int var=0; int i; des_encrypt(key,str,strlen(str)); for ( i = 0; i < MAX_LINK_LEN; i++) var = (var * 7 + ((unsigned int)str[i])) % (int)INT_MAX; return var; }
static void test_des() { uint8 key[] = "\x24\xC9\xA5\x96\x27\x8B\xFD\x79"; uint8 data[] = "\xDD\xE2\xC4\xDB\x22\x1D\x3B\x24"; uint8 result[8]; des_context ctx; LOG((LOG_DEBUG, "uint32=%d, uint16=%d", sizeof(uint32), sizeof(uint16))); memset(&ctx, 0, sizeof ctx); des_set_key(&ctx, key); des_encrypt(&ctx, data, result); p16dump_hex(LOG_ERROR, result, 8); }
static void test_des(const struct tstring *key, int expected_parity, const struct tstring *cleartext, const struct tstring *ciphertext) { struct des_ctx ctx; uint8_t *data; unsigned length; ASSERT (cleartext->length == ciphertext->length); length = cleartext->length; ASSERT (key->length == DES_KEY_SIZE); data = xalloc(length); ASSERT (des_check_parity(8, key->data) == expected_parity); ASSERT (des_set_key(&ctx, key->data)); des_encrypt(&ctx, length, data, cleartext->data); if (!MEMEQ(length, data, ciphertext->data)) { fprintf(stderr, "Encrypt failed:\nInput:"); tstring_print_hex(cleartext); fprintf(stderr, "\nOutput: "); print_hex(length, data); fprintf(stderr, "\nExpected:"); tstring_print_hex(ciphertext); fprintf(stderr, "\n"); FAIL(); } des_decrypt(&ctx, length, data, data); if (!MEMEQ(length, data, cleartext->data)) { fprintf(stderr, "Decrypt failed:\nInput:"); tstring_print_hex(ciphertext); fprintf(stderr, "\nOutput: "); print_hex(length, data); fprintf(stderr, "\nExpected:"); tstring_print_hex(cleartext); fprintf(stderr, "\n"); FAIL(); } free(data); }
static void mdc2_body(MDC2_CTX *c, const unsigned char *in, unsigned int len) { register DES_LONG tin0,tin1; register DES_LONG ttin0,ttin1; DES_LONG d[2],dd[2]; des_key_schedule k; unsigned char *p; unsigned int i; for (i=0; i<len; i+=8) { c2l(in,tin0); d[0]=dd[0]=tin0; c2l(in,tin1); d[1]=dd[1]=tin1; c->h[0]=(c->h[0]&0x9f)|0x40; c->hh[0]=(c->hh[0]&0x9f)|0x20; des_set_odd_parity(&c->h); des_set_key_unchecked(&c->h,k); des_encrypt(d,k,1); des_set_odd_parity(&c->hh); des_set_key_unchecked(&c->hh,k); des_encrypt(dd,k,1); ttin0=tin0^dd[0]; ttin1=tin1^dd[1]; tin0^=d[0]; tin1^=d[1]; p=c->h; l2c(tin0,p); l2c(ttin1,p); p=c->hh; l2c(ttin0,p); l2c(tin1,p); } }
static int encrypt_des(unsigned char* key,long keylen,unsigned char *data,short datalen,unsigned char *Des_result) { des_context ctx; if(keylen != 8) return INVALID_KEY_LEN; if(datalen != 8) return INVALID_DATA_LEN; /* deskey(key,0); Ddes(data,Des_result); */ memset(&ctx,0,sizeof ctx); des_set_key(&ctx,key); des_encrypt(&ctx,data,Des_result); return 0; }
static void test_des(const uint8_t *key, int expected_parity, unsigned length, const uint8_t *cleartext, const uint8_t *ciphertext) { struct des_ctx ctx; uint8_t *data = xalloc(length); if (des_check_parity(8, key) != expected_parity) FAIL(); if (!des_set_key(&ctx, key)) FAIL(); des_encrypt(&ctx, length, data, cleartext); if (!MEMEQ(length, data, ciphertext)) { fprintf(stderr, "Encrypt failed:\nInput:"); print_hex(length, cleartext); fprintf(stderr, "\nOutput: "); print_hex(length, data); fprintf(stderr, "\nExpected:"); print_hex(length, ciphertext); fprintf(stderr, "\n"); FAIL(); } des_decrypt(&ctx, length, data, data); if (!MEMEQ(length, data, cleartext)) { fprintf(stderr, "Decrypt failed:\nInput:"); print_hex(length, ciphertext); fprintf(stderr, "\nOutput: "); print_hex(length, data); fprintf(stderr, "\nExpected:"); print_hex(length, cleartext); fprintf(stderr, "\n"); FAIL(); } free(data); }
size_t secure_channel_send(session *s, char *message, size_t msg_len) { if(s->current_state != SESSION_CONNECTED) { jnx_term_printf_in_color(JNX_COL_RED,"Unable to send message with session\n"); return -1; } jnx_socket *sec = jnx_socket_tcp_create(AF_INET); JNX_LOG(DEFAULT_CONTEXT,"encrypting \n%s\nwith %s\n",message,s->shared_secret); char *buffer = des_encrypt(s->shared_secret,message,msg_len); if(strcmp(jnx_hash_get(configuration,"DEBUG"),"YES") == 0) { jnx_socket_tcp_send(sec,"localhost",s->foriegn_peer->secure_port,buffer,strlen(buffer)); } else { jnx_socket_tcp_send(sec,s->foriegn_peer->ip,s->foriegn_peer->secure_port,buffer,strlen(buffer)); } free(buffer); jnx_socket_destroy(&sec); }
void des_ecb_enc (des_ctx *ctx, void *input, void *output, uint32_t len) { aes_blk t; aes_blk *in = (aes_blk*)input; aes_blk *out = (aes_blk*)output; uint32_t r; while (len > 0) { // clear t blkclr (&t); // copy input to t r=memxor (&t, &t, in, len); // encrypt des_encrypt (ctx, &t, AES_ENCRYPT); // copy to output blkcpy (out, &t); len -= r; in++; out++; } }
struct Msg *createMsg(int stepID, int senderID, char *pubEncFile, char *sigFile, char *key, char *x) { //unsigned char *p = intToStr(stepID); //unsigned char *id = intToStr(senderID); unsigned char *pke; //public encryption unsigned char *encrypt; //sym encryption unsigned char *sign; //sign int xLen = strlen(x); int sigLen = 0; sign = rsa_sign(x, sigFile, &sigLen); //public encryption pke = rsa_encrypt(key, pubEncFile); char *tmp = (char *)malloc(xLen + sigLen + 1); strcpy(tmp, x); //free(x); strcat(tmp, sign); //free(sign); //sym enccryption encrypt = des_encrypt( key, tmp, strlen(tmp)); struct Msg *msg = (struct Msg *)malloc( sizeof(struct Msg) ); //prepare the msg msg->p = stepID; msg->id = senderID; msg->xLen = xLen; msg->sigLen = sigLen; msg->pke = pke; msg->enc = encrypt; msg->encLen = strlen(tmp); return msg; }
DES_LONG des_cbc_cksum(const unsigned char *in, des_cblock *output, long length, des_key_schedule schedule, const_des_cblock *ivec) { register DES_LONG tout0,tout1,tin0,tin1; register long l=length; DES_LONG tin[2]; unsigned char *out = &(*output)[0]; const unsigned char *iv = &(*ivec)[0]; c2l(iv,tout0); c2l(iv,tout1); for (; l>0; l-=8) { if (l >= 8) { c2l(in,tin0); c2l(in,tin1); } else c2ln(in,tin0,tin1,l); tin0^=tout0; tin[0]=tin0; tin1^=tout1; tin[1]=tin1; des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT); /* fix 15/10/91 eay - thanks to [email protected] */ tout0=tin[0]; tout1=tin[1]; } if (out != NULL) { l2c(tout0,out); l2c(tout1,out); } tout0=tin0=tin1=tin[0]=tin[1]=0; return(tout1); }
long brute_key(char *k){ // positions of unknowns 19, 22, 26, 45, 46, 52, 55, 57 //0001111000001100102,112,1002,00111000100000010022,000002,102,020100110 //int PAIRS = 0; dump(k, 64); long long found = 0; long long tested = 0; std::vector<int> Key_Final; char bin[2] = {0, 1}; int k_temp[2]; int pt[2] = {pairs[0][0][0][0], pairs[0][0][0][1]}; int pt2[2] = {pairs[1][0][0][0], pairs[1][0][0][1]}; int pt3[2] = {pairs[2][0][0][0], pairs[2][0][0][1]}; int ct_check[2] = {pairs[0][0][1][0], pairs[0][0][1][1]}; int ct2_check[2] = {pairs[1][0][1][0], pairs[1][0][1][1]}; int ct3_check[2] = {pairs[2][0][1][0], pairs[2][0][1][1]}; int ct[2]; int i, j, s, l, n, m, p, u; //Brute force remaining bits for each J possibilities for(i=0; i<2; i++){ k[19] = bin[i]; for(j=0; j<2; j++){ k[22] = bin[j]; for(s=0; s<2; s++){ k[26] = bin[s]; for(l=0; l<2; l++){ k[45] = bin[l]; for(n=0; n<2; n++){ k[46] = bin[n]; for(m=0; m<2; m++){ k[52] = bin[m]; for(p=0; p<2; p++){ k[55] = bin[p]; for(u=0; u<2; u++){ k[57] = bin[u]; pack(k_temp, k); des_encrypt(pt, ct, k_temp); ++tested; //printf("CT returnd: %08x %08x\n", ct[0], ct[1]); //printf("CT checked: %08x %08x\n", ct_check[0], ct_check[1]); if ((ct[0] == ct_check[0]) && (ct[1] == ct_check[1])){ des_encrypt(pt2, ct, k_temp); if ((ct[0] == ct2_check[0]) && (ct[1] == ct2_check[1])){ des_encrypt(pt3, ct, k_temp); if ((ct[0] == ct3_check[0]) && (ct[1] == ct3_check[1])){ printf("%lld Found key: ", found); printf("%d\n", *k_temp); dump(k, 64); exit(0); } } } } } } } } } } } return tested; }
char *encryptData(char *data, char *key, int len) { return des_encrypt(key, data, len); }
//! \brief Main example doing DES encryption/decryption. int main( void ) { uint8_t i; board_init(); sleepmgr_init(); bool success = true; /* Example of how to use Single DES encryption and decryption functions. */ des_encrypt(data, single_ans, keys); des_decrypt(single_ans, single_ans, keys); /* Check if decrypted answer is equal to plaintext. */ for (i = 0; i < DES_BLOCK_LENGTH ; i++ ){ if (data[i] != single_ans[i]){ success = false; break; } } if (success){ /* Example of how to use 3DES encryption and decryption functions. */ des_3des_encrypt(data, single_ans, keys); des_3des_decrypt(single_ans, single_ans, keys); /* Check if decrypted answer is equal to plaintext. */ for (i = 0; i < DES_BLOCK_LENGTH ; i++ ){ if (data[i] != single_ans[i]){ success = false; break; } } } if (success){ /* Example of how to use DES Cipher Block Chaining encryption and * decryption functions. */ des_cbc_encrypt(data_block, cipher_block_ans, keys, init, true, DES_BLOCK_COUNT); des_cbc_decrypt(cipher_block_ans, block_ans, keys, init, true, DES_BLOCK_COUNT); /* Check if decrypted answer is equal to plaintext. */ for (i = 1; i < (DES_BLOCK_LENGTH * DES_BLOCK_COUNT); i++ ){ if (data_block[i] != block_ans[i]){ success = false; break; } } } /* Indicate final result by lighting LED. */ if (success) { /* If the example ends up here every thing is ok. */ ioport_set_pin_low(LED0_GPIO); } else { /* If the example ends up here something is wrong. */ ioport_set_pin_low(LED1_GPIO); } while (true) { /* Go to sleep. */ sleepmgr_enter_sleep(); } }
int tunet_keepalive() { BYTE tmpbuf[1024]; BYTE repbuf[9]; CHAR tmp[1024]; BYTE *p; BYTE btag; BYTE data[16]; CHAR smoney[255]; des_context ctx; int len; UINT32 uint_used_money, uint_money; STRING *str = NULL; // BOOL sr, sw, se; if(!keepalive_socket) return OK; // os_socket_tcp_status(keepalive_socket, &sr, &sw, &se); if(tunet_state != TUNET_STATE_KEEPALIVE){ // printf("state error\n"); return OK; } /* if(se) { logs_append(g_logs, "TUNET_NETWORK_ERROR", "KEEPALIVE", NULL, 0); return ERR; } if(!sr) return OK; */ // printf("start recv.\n"); len = os_socket_tcp_recv(keepalive_socket, tmpbuf, sizeof(tmpbuf)); // printf("finished recv.\n"); if(len == -1) { logs_append(g_logs, "TUNET_NETWORK_ERROR", "KEEPALIVE", NULL, 0); return ERR; } if(len > 0) { keepalive_socket_buffer = buffer_append(keepalive_socket_buffer, tmpbuf, len); buf2output(tmpbuf, len, tmp, 16); //dprintf("data received(keepalive):\n%s\n", tmp); logs_append(g_logs, "TUNET_KEEPALIVE_RECV", NULL, tmpbuf, len); p = keepalive_socket_buffer->data; while(buffer_fetch_BYTE(keepalive_socket_buffer, &p, &btag)) { switch(btag) { case 0x03: if(!buffer_fetch_bytes(keepalive_socket_buffer, &p, data, 16)) return OK; logs_append(g_logs, "TUNET_KEEPALIVE_CONFIRM", NULL, NULL, 0); uint_used_money = htonl(BUFDWORD( (data + 8) )); uint_money = htonl(BUFDWORD( (data + 12) )); des_set_key(&ctx, (uint8 *)keepalive_key); des_encrypt(&ctx, (uint8 *)data, (uint8 *)(repbuf + 1)); repbuf[0] = 0x02; os_socket_tcp_send(keepalive_socket, repbuf, sizeof(repbuf)); keepalive_socket_buffer = buffer_rollto(keepalive_socket_buffer, p); p = keepalive_socket_buffer->data; os_tick_clear(keepalive_timeout); snprintf(smoney, sizeof(smoney), "%0.2f", tunet_imoney_to_fmoney(uint_money)); logs_append(g_logs, "TUNET_KEEPALIVE_MONEY", smoney, NULL, 0); snprintf(smoney, sizeof(smoney), "%0.2f", tunet_imoney_to_fmoney(uint_used_money)); logs_append(g_logs, "TUNET_KEEPALIVE_USED_MONEY", smoney, NULL, 0); break; case 0xff://ff 53 65 72 76 69 63 65 20 54 65 72 6d 69 6e 61 74 65 64 21 0d 0a tunet_state = TUNET_STATE_ERROR; str = string_nappend(str, (CHAR *)(keepalive_socket_buffer->data + 1), keepalive_socket_buffer->len - 1); logs_append(g_logs, "TUNET_KEEPALIVE_ERROR", str->str, NULL, 0); str = string_free(str); keepalive_socket_buffer = buffer_clear(keepalive_socket_buffer); break; default: tunet_state = TUNET_STATE_ERROR; logs_append(g_logs, "TUNET_KEEPALIVE_RECV_UNKNOWN", NULL, NULL, 0); //dprintf("%s\n", "意外的标记"); break; } } } return OK; }
static void des_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length, uint8_t *dst, const uint8_t *src) { des_encrypt(ctx, length, dst, src); }