如何用openssl解密wpa 2 psk,用c编程

时间:2011-08-19 06:31:03

标签: c encryption openssl aes wpa

我需要在c中解密wpa 2 psk编程。假设我已经拥有TK(需要仅解密单播)我正在尝试使用以下代码解密数据包(实际上没有运气):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/pem.h>
#include <openssl/bn.h>
#include <openssl/aes.h>


    struct ctr_state { 
unsigned char ivec[128];   
unsigned int num; 
unsigned char ecount[128]; 
}; 

 void init_ctr(struct ctr_state *state, const unsigned char iv[16]){     //
    state->num = 0; 
    memset(state->ecount, 0, 128);
    memset(state->ivec, 0, 128); 
    memcpy(state->ivec, iv, 16);
} 

char * extochar(char * in, int inLen){
    int i,k;
    int resInt[inLen/2];
    char * resChar=malloc(inLen/2);

    k=0;
    for(i=0; i<inLen/2; i=i++){
            resInt[k]=chartoint(in[i*2])<<4;
            resInt[k]+=chartoint(in[(i*2)+1]);
            k++;
    }

    for(k=0; k<inLen/2;k++){
            resChar[k]=(char)resInt[k];
    }
    return resChar;
}

int chartoint(char car){
    int intero = 0;
    intero = car - '0';
    if(intero < 10 && intero > -1)
            return intero;
    else
            return car - 'a' + 10; 
}

void main(){        

    unsigned char * po = extochar("00",2);
    unsigned char * a2 = extochar("0012f0be7301",12);
    unsigned char * pn = extochar("000000000052",12);
    unsigned char * nonce= malloc(13);
    char * hextk= "15b1657878b1d12c93b4e073e42b629a";
    unsigned char * tk= extochar(hexstr, strlen(hextk));
    init_ctr(&status, nonce);

    nonce[0]=po[0];        
    nonce[1]=a2[0];
    nonce[2]=a2[1];
    nonce[3]=a2[2];
    nonce[4]=a2[3];
    nonce[5]=a2[4];
    nonce[6]=a2[5];
    nonce[7]=pn[0];
    nonce[8]=pn[1];
    nonce[9]=pn[2];
    nonce[10]=pn[3];
    nonce[11]=pn[4];
    nonce[12]=pn[5];

    AES_KEY aes_key;
    if(AES_set_encrypt_key(tk, 128, &aes_key))
            exit(-1);
    //encrypted payload
    char * ext_crypt = "146a056e3b20ece434594373a0e4fcbc83114c9a1bc158ecc4ca6bb449d6ec8468c8e08af3f4f33ce961f7b42c7651e22042e0bf39bd864a1b5f1035af5a54986183ee79446e3fb80a6f9bbb7a0177f557ce192c5515bd3a671b73464b9cf0fb817fd614987b65c0e20d753dedab8bf1934294e112cb865effb14724a2c66fcc7956f8fcfb0f7f2e539fbbf4e30c08fc18d10eb143510eae8b88e911c1cee773b73cdaf6d45151ad01fb2e2f5aa014510a";
    int msg_len= strlen(ext_crypt)/2 - 12;
    unsigned char * crypt =  extochar(ext_crypt, strlen(ext_crypt));
    AES_ctr128_encrypt(crypt, cleartxt, msg_len, &aes_key, status.ivec, status.ecount, &status.num);

}

输出应该是这样的: -SEARCH * HTTP / 1.1 主持人:239.255.255.250:1900 男:“ssdp:发现” MX:2 ST:urn:schemas-upnp-org:service:WANPPPConnection:1

我在这里粘贴了加密数据包的来源:http://pastebin.com/RvkfSt54 代码或数据中是否有任何问题,我该如何解决? 非常感谢!

1 个答案:

答案 0 :(得分:0)

除了代码被取消注释并且格式有些差...你的问题是否存在“代码中的任何问题”可以由编译器回答,如果你启用了警告(你应该总是这样做):

test.c: In function ‘extochar’:
test.c:28:26: warning: operation on ‘i’ may be undefined
test.c:29:13: warning: implicit declaration of function ‘chartoint’
test.c: At top level:
test.c:40:5: error: conflicting types for ‘chartoint’
test.c:40:1: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
test.c:29:23: note: previous implicit declaration of ‘chartoint’ was here
test.c:49:6: warning: return type of ‘main’ is not ‘int’
test.c: In function ‘main’:
test.c:51:26: warning: pointer targets in initialization differ in signedness
test.c:52:26: warning: pointer targets in initialization differ in signedness
test.c:53:26: warning: pointer targets in initialization differ in signedness
test.c:56:34: error: ‘hexstr’ undeclared (first use in this function)
test.c:56:34: note: each undeclared identifier is reported only once for each function it appears in
test.c:57:15: error: ‘status’ undeclared (first use in this function)
test.c:79:30: warning: pointer targets in initialization differ in signedness
test.c:80:31: error: ‘cleartxt’ undeclared (first use in this function)