Exemple #1
0
int main(int argc, char *argv[]) {
	
	unsigned char key[4] = {0x23, 0x24, 0x12, 0x34};
	unsigned char in[6] = {0x12, 0x14, 0x15, 0x17, 0x18, 0xf2};	
	unsigned char out1[6] = {0};
	unsigned char out2[6] = {0};
	int i = 0;

	for (i = 0; i < 6; i ++) {
		
		printf("%0x ", in[i]);
	}
	printf("\n");
	
	rc4_init(key, 4);
	rc4_generate(in, 6, out1);

	for (i = 0; i < 6; i ++) {
		
		printf("%0x ", out1[i]);
	}
	printf("\n");

	rc4_init(key, 4);
	rc4_generate(out1, 6, out2);

	for (i = 0; i < 6; i ++) {
		
		printf("%0x ", out2[i]);
	}
	printf("\n");

	return 0;
}
void WardenMgr::SetInitialKeys(const uint8 *bSessionKey1, const uint8 *bSessionKey2, uint8* ClientKey, uint8 *ServerKey)
{
     uint8 bRandomData[20] = {0};
     uint8 bRandomSource1[20] = {0};
     uint8 bRandomSource2[20] = {0};

     SHA1(bSessionKey1, 20, bRandomSource1);
     SHA1(bSessionKey2, 20, bRandomSource2);

     SHA_CTX mCtx;
     SHA1_Init(&mCtx);

     // compute hash
     SHA1_Update(&mCtx, bRandomSource1, 20);
     SHA1_Update(&mCtx, bRandomData, 20);
     SHA1_Update(&mCtx, bRandomSource2, 20);

     // finalize
     SHA1_Final(bRandomData, &mCtx);

     uint8 position = 0;
     uint8 key[16] = {0};
     for (uint8 i=0; i<16; ++i)
     {
         if (position >= 20)
         {
             SHA1_Init(&mCtx);
             SHA1_Update(&mCtx, bRandomSource1, 20);
             SHA1_Update(&mCtx, bRandomData, 20);
             SHA1_Update(&mCtx, bRandomSource2, 20);
             SHA1_Final(bRandomData, &mCtx);
             position = 0;
         }
         key[i] = bRandomData[position];
         ++position;
     }
     rc4_init(ClientKey, key, 16);

     for (uint8 i=0; i<16; ++i)
         key[i]=0;

     for (uint8 i=0; i<16; ++i)
     {
         if (position >= 20)
         {
             SHA1_Init(&mCtx);
             SHA1_Update(&mCtx, bRandomSource1, 20);
             SHA1_Update(&mCtx, bRandomData, 20);
             SHA1_Update(&mCtx, bRandomSource2, 20);
             SHA1_Final(bRandomData, &mCtx);
             position = 0;
         }
         key[i] = bRandomData[position];
         ++position;
     }
     rc4_init(ServerKey, key, 16);
}
Exemple #3
0
static struct ts_crypto_ctx *ts_crypto_rc4(const unsigned char *key, int len) {
    struct ts_crypto_ctx *ctx = ts_malloc(sizeof(struct ts_crypto_ctx));
    if (ctx) {
        rc4_init(&ctx->rc4.enc_state, key, len);
        rc4_init(&ctx->rc4.dec_state, key, len);
        ctx->encrypt = ts_rc4_encrypt;
        ctx->decrypt = ts_rc4_decrypt;
    }
    return ctx;
}
static void Password(const char *pass, const char *key) {
	size_t keylen = strlen(key);
	size_t passlen = strlen(pass);
	// Create a new key, which is the XOR of the "key" and the challenge that
	// was received from the server. If the vectors are of unequal length,
	// the end is padded with values from the longest.
	size_t newLength = keylen;
	if (gLoginChallenge.size() > newLength)
		newLength = gLoginChallenge.size();
	unsigned char newkey[newLength];
	for (size_t i=0; i<newLength; i++) newkey[i] = 0;
	for (size_t i=0; i<keylen; i++) newkey[i] = key[i];
	for (size_t i=0; i<gLoginChallenge.size(); i++) newkey[i] ^= gLoginChallenge[i];
#if 0
	printf("Password: Key (len %d): ", newLength);
	for (int i=0; i<newLength; i++) printf("%d ", newkey[i]);
	printf("\n");
#endif
	unsigned char b[passlen+3];
	// Build the message into 'b'.
	b[0] = passlen+3; // LSB of message length
	b[1] = 0;   // MSB of message length
	b[2] = CMD_RESP_PASSWORD;
	memcpy(b+3, pass, passlen); // Initialize with the clear text password
	rc4_init(newkey, newLength);
	rc4_xor(b+3, passlen); // Encrypt the password

#if 0
	printf("encr: ");
	for (int i=0; i<passlen; i++) printf(" %d", b[i+3]);
	printf("\n");
#endif

	SendMsg(b, passlen+3);
}
Exemple #5
0
static int
ldecompress(lua_State *L) {
	z_stream stream;
	stream.zalloc = Z_NULL;
	stream.zfree = Z_NULL;
	stream.opaque = Z_NULL;
	struct filter flt = {
		&stream,
		inflateEnd,
		inflate,
		CRYPT_NONE,
		NULL,
	};
	struct rc4_sbox sbox;
	if (lua_isstring(L, 2)) {
		size_t keysz;
		const char * key = lua_tolstring(L, 2, &keysz);
		if (keysz != 8) {
			return luaL_error(L, "Only support 8 bytes key");
		}
		rc4_init(&sbox, (const uint8_t *)key);
		flt.crypt_type = CRYPT_IN;
		flt.crypt_sbox = &sbox;
	}
	int result = inflateInit(&stream);
	lz_assert(L, result, &flt);
	return execute(L, &flt);
}
Exemple #6
0
static int
lload(lua_State *L) {
	struct reader rd;
	rd.source = luaL_checklstring(L,1,&rd.source_sz);
	const char * filename = luaL_checkstring(L, 2);
	struct filter flt = { &rd.stream, inflateEnd, NULL, 0, 0 };
	struct rc4_sbox sbox;
	if (lua_isstring(L, 3)) {
		size_t keysz;
		const char * key = lua_tolstring(L, 3, &keysz);
		if (keysz != 8) {
			return luaL_error(L, "Only support 8 bytes key");
		}
		rc4_init(&sbox, (const uint8_t *)key);
		rd.crypt_sbox = &sbox;
	} else {
		rd.crypt_sbox = NULL;
	}
	rd.flags = Z_NO_FLUSH;
	z_stream *stream = &rd.stream;
	stream->zalloc = Z_NULL;
	stream->zfree = Z_NULL;
	stream->opaque = Z_NULL;
	stream->avail_in = 0;
	stream->avail_out = 0;
	int result = inflateInit(stream);
	lz_assert(L, result, &flt);
	result = lua_load (L, zip_reader, &rd, filename, "bt");
	inflateEnd(stream);
	if (result != LUA_OK) {
		return lua_error(L);
	}
	return 1;
}
Exemple #7
0
void Protocol::encode(std::string &value) const
{
    Json::Value root;
    JsonStream stream(root);
    encode(stream);
    std::string plain = root.toStyledString();
    CCLOG("==== Client send:%s ====", plain.c_str());
    if (needcrypt)
    {
        
        
        size_t length = plain.length();
        char *temp = new char[length + sizeof(type)];
        memcpy(temp, &type, sizeof(type));
        rc4_state state;
        rc4_init(&state, (const u_char *)OUTKEY.c_str(), OUTKEY.size());
        rc4_crypt(&state, (const u_char *)plain.c_str(), (u_char *)(temp + sizeof(type)), length);
        value.assign(temp, length + sizeof(type));
        delete[] temp;
    }
    else
    {
        char *temp = new char[sizeof(type) + plain.length()];
        memcpy(temp, &type, sizeof(type));
        memcpy(temp + sizeof(type), plain.c_str(), plain.length());
        value.assign(temp, plain.length() + sizeof(type));
    }
}
Exemple #8
0
Protocol* Protocol::decode(const char *bytes, int32_t len, bool decrypt)
{
    int type;
    memcpy(&type, bytes, sizeof(type));
    bytes += sizeof(type);
    len -= 4;
    
    char *temp = new char[len];
    if (decrypt)
    {
        rc4_state state;
        rc4_init(&state, (const u_char *)INKEY.c_str(), INKEY.size());
        rc4_crypt(&state, (const u_char *)bytes, (u_char *)temp, len);
    }
    else
    {
        memcpy(temp, bytes, len);
    }
    std::string plain(temp, len);
    delete[] temp;
    CCLOG("==== Client received:%s ====", plain.c_str());
    Json::Reader reader;
    Json::Value root;
    if (!reader.parse(plain, root))
        return NULL;
    //int type = root["type"].asInt();
    Protocol *p = Protocol::create(type);
    if (p != NULL)
    {
        JsonStream stream(root);
        p->decode(stream);
    }
    return p;
}
Exemple #9
0
int main()
{
    unsigned char s[256] = {0}, s2[256] = {0}; //S-box
    char key[256] = {"just for test"};
    char pData[512] = "这是一个用来加密的数据Data";
    unsigned len = strlen(pData);
    int i;

    printf("pData = %s\n", pData);
    printf("key = %s, length = %d\n\n", key, strlen(key));
    rc4_init(s, (unsigned char *)key, strlen(key)); //已经完成了初始化
    printf("完成对S[i]的初始化,如下:\n\n");
    for (i = 0; i < 256; i++)
    {
        printf("%02X ", s[i]);
        if (i && (i + 1) % 16 == 0) putchar('\n');
    }
    printf("\n\n");
    for(i = 0; i < 256; i++) //用s2[i]暂时保留经过初始化的s[i],很重要的!!!
    {
        s2[i] = s[i];
    }
    printf("已经初始化,现在加密:\n\n");
    rc4_crypt(s, (unsigned char *)pData, len); //加密
    printf("pData = %s\n\n", pData);
    printf("已经加密,现在解密:\n\n");
    //rc4_init(s, (unsigned char *)key, strlen(key)); //初始化密钥
    rc4_crypt(s2, (unsigned char *)pData, len); //解密
    printf("pData = %s\n\n", pData);
    return 0;
}
Exemple #10
0
void rc4_encrypt(const u8 *key, uint keylen,
                 const u8 *p, uint plen, u8 *c)
{
  rc4_ctx ctx;
  rc4_init(&ctx, key, keylen);
  rc4_update(&ctx, p, plen, c);
  rc4_final(&ctx);
}
	void rc4_handler::set_outgoing_key(unsigned char const* key, int len)
	{
		m_encrypt = true;
		rc4_init(key, len, &m_rc4_outgoing);
		// Discard first 1024 bytes
		char buf[1024];
		std::vector<boost::asio::mutable_buffer> vec(1, boost::asio::mutable_buffer(buf, 1024));
		encrypt(vec);
	}
Exemple #12
0
int main(int argc, char *argv[]) {
	
	char *key = "jason";
	char *in = "how are you?";
	char out1[256] = {0};
	char out2[256] = {0};

	rc4_init(key, strlen(key));
	rc4_encode(in, strlen(in), out1);

	printf("out1 => %s\n", out1);

	rc4_init(key, strlen(key));
	rc4_encode(out1, strlen(out1), out2);

	printf("out2 => %s\n", out2);

	return 0;
}
void WardenMgr::LoadModuleAndGetKeys(WorldSession* const session)
{
    if (!m_WardenProcessStream)
        return;

    std::string modulekeyfile = sWorld->GetDataPath()+ "warden/" + session->m_WardenModule + ".key";
    std::string modulefile = sWorld->GetDataPath()+ "warden/" + session->m_WardenModule + ".bin";

    // Load .key file to get module length and module key
    FILE* mf = fopen(modulekeyfile.c_str(), "rb");
    if (!mf)
        return; // Modules have been tested at WardenMgr init, so have been deleted while core was running !

    uint32 modLength;
    uint8 rc4[16];
    fread(&modLength, 1, 4, mf);
    fread(rc4, sizeof(uint8)*16, 1, mf);
    fclose(mf);

    // Load the module encrypted binary
    mf = fopen(modulefile.c_str(), "rb");
    if (!mf)
        return; // Modules have been tested at WardenMgr init, so have been deleted while core was running !


    uint8* m_tmpModule = (uint8*)malloc(sizeof(uint8)*modLength);
    fread(m_tmpModule, sizeof(uint8)*modLength, 1, mf);
    fclose(mf);

    // Just decrypt it so that we don't even need to send the rc4
    uint8 m_tmpKey[0x102];
    rc4_init(m_tmpKey, rc4, 16);
    rc4_crypt(m_tmpKey, m_tmpModule, modLength);

    uint32 m_signature = *(uint32*)(m_tmpModule + modLength - 0x100 - 4); // - 256 bytes - sizeof(uint32)
    if (m_signature != 0x5349474E) // NGIS->SIGN string
    {
        sLog->outBasic("Warden: Module damaged on disk");
        return;
    }

    ByteBuffer pkt;
    pkt << uint8(MMSG_LOAD_MODULE);
    pkt << uint32(modLength - 0x100); // - 256 bytes certificate
    pkt << uint32(session->GetAccountId());
    pkt.append(m_tmpModule, modLength - 0x100);

    pkt.append(session->m_Socket->GetSessionKey().AsByteArray(40), 40);
    // Same as when we send this transformed seed request to client
    pkt << uint8(WARDS_SEED);
    pkt.append(&session->m_wardenSeed[0], 16);
    free(m_tmpModule);

    m_WardenProcessStream->send((char const*)pkt.contents(), pkt.size());
}
Exemple #14
0
kcl_err_t
kcl_rc4_init(void **ctxp, const void *key, size_t keysize)
{
	rc4_state_t *rc4;

	if ((rc4 = kcl_malloc(sizeof(rc4_state_t))) == NULL)
		return KCL_ERR_NOMEM;
	rc4_init(rc4, key, keysize);
	*ctxp = rc4;
	return KCL_ERR_NONE;
}
Exemple #15
0
void        smb_ntlm2_session_key(smb_ntlmh *hash_v2, void *ntlm2,
                                  smb_ntlmh *xkey, smb_ntlmh *xkey_crypt)
{
    struct rc4_state  rc4;
    smb_ntlmh         hmac_ntlm2;

    HMAC_MD5(&hash_v2, 16, ntlm2, 16, &hmac_ntlm2);

    rc4_init(&rc4, hmac_ntlm2, 16);
    rc4_crypt(&rc4, (void *)xkey, (void *)xkey_crypt, 16);
}
Exemple #16
0
void        smb_ntlm2_session_key(smb_ntlmh hash_v2, void *ntlm2,
                                  smb_ntlmh xkey, smb_ntlmh xkey_crypt)
{
    struct rc4_state  rc4;
    smb_ntlmh         hmac_ntlm2;

    HMAC_MD5(hash_v2, SMB_NTLM_HASH_SIZE, ntlm2, SMB_NTLM_HASH_SIZE, hmac_ntlm2);

    rc4_init(&rc4, hmac_ntlm2, 16);
    rc4_crypt(&rc4, (void *)xkey, (void *)xkey_crypt, 16);
}
	void rc4_handler::set_incoming_key(unsigned char const* key, int len)
	{
		m_decrypt = true;
		rc4_init(key, len, &m_rc4_incoming);
		// Discard first 1024 bytes
		char buf[1024];
		std::vector<boost::asio::mutable_buffer> vec(1, boost::asio::mutable_buffer(buf, 1024));
		int consume = 0;
		int produce = 0;
		int packet_size = 0;
		decrypt(vec, consume, produce, packet_size);
	}
Exemple #18
0
void rc4_test() {
    // Test of encryption, according to http://en.wikipedia.org/wiki/RC4#Test_vectors
    unsigned char buff[100];
    strcpy((char *)buff, "Plaintext");
    int l1 = strlen((char *)buff);
    rc4_init((const unsigned char*)"Key", 3);
    rc4_xor(buff, l1);
    printf("rc4_test: ");
    for (int i=0; i<l1; i++)
        printf("%d ", buff[i]);
    printf("\n");
}
Exemple #19
0
static
void
s_vSWencryption (
      PSDevice            pDevice,
      PSKeyItem           pTransmitKey,
      PBYTE               pbyPayloadHead,
      WORD                wPayloadSize
    )
{
    unsigned int   cbICVlen = 4;
    DWORD  dwICV = 0xFFFFFFFFL;
    PDWORD pdwICV;

    if (pTransmitKey == NULL)
        return;

    if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
        //=======================================================================
        // Append ICV after payload
        dwICV = CRCdwGetCrc32Ex(pbyPayloadHead, wPayloadSize, dwICV);//ICV(Payload)
        pdwICV = (PDWORD)(pbyPayloadHead + wPayloadSize);
        // finally, we must invert dwCRC to get the correct answer
        *pdwICV = cpu_to_le32(~dwICV);
        // RC4 encryption
        rc4_init(&pDevice->SBox, pDevice->abyPRNG, pTransmitKey->uKeyLength + 3);
        rc4_encrypt(&pDevice->SBox, pbyPayloadHead, pbyPayloadHead, wPayloadSize+cbICVlen);
        //=======================================================================
    } else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
        //=======================================================================
        //Append ICV after payload
        dwICV = CRCdwGetCrc32Ex(pbyPayloadHead, wPayloadSize, dwICV);//ICV(Payload)
        pdwICV = (PDWORD)(pbyPayloadHead + wPayloadSize);
        // finally, we must invert dwCRC to get the correct answer
        *pdwICV = cpu_to_le32(~dwICV);
        // RC4 encryption
        rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
        rc4_encrypt(&pDevice->SBox, pbyPayloadHead, pbyPayloadHead, wPayloadSize+cbICVlen);
        //=======================================================================
    }
}
bool ScRC4EncodeFilter::openFilter (void)
{
	freeData();

	m_filterData = (ScRC4EncodeFilterData*) malloc(sizeof(ScRC4EncodeFilterData));
	if (m_filterData == NULL)
		return false;

	rc4_init(&m_filterData->rc4_context, (uchar*) m_key.data(),m_key.length());
	m_filterData->available_in  = 0;
	m_openedFilter = true;
	return true;
}
Exemple #21
0
static int
lrc4(lua_State * L) {
  size_t len;
  const char * key = luaL_checklstring(L, 1, &len);

  struct rc4_state * rc4 = (struct rc4_state *)lua_newuserdata(L, sizeof(*rc4));
  luaL_getmetatable(L, RC4_METATABLE);
  lua_setmetatable(L, -2);

  rc4_init(rc4, (uint8_t*)key, len);

  return 1;
}
Exemple #22
0
struct ss_encryptor *ss_create_encryptor(enum ss_encrypt_method method,
					 const uint8_t *key, size_t key_len)
{
	struct ss_encryptor *encryptor;

	switch (method) {
	case XOR_METHOD:
		encryptor = calloc(1, sizeof(typeof(*encryptor)) + key_len);
		encryptor->enc_method = method;
		encryptor->xor_enc.key_len = key_len;
		memcpy(encryptor->xor_enc.key, key, key_len);
		return encryptor;
	case RC4_METHOD:
		encryptor = calloc(1, sizeof(typeof(*encryptor)) + key_len);
		encryptor->enc_method = method;
		encryptor->rc4_enc.key_len = key_len;
		memcpy(encryptor->rc4_enc.key, key, key_len);
		rc4_init(&encryptor->rc4_enc.en_state, key, key_len);
		rc4_init(&encryptor->rc4_enc.de_state, key, key_len);
		return encryptor;
	default:
		DIE("not support %d", method);
	}
}
Exemple #23
0
struct encryptor *create_encryptor(enum encrypt_method method,
					 const uint8_t *key, size_t key_len)
{
	struct encryptor *encryptor;
	MD5_CTX md5;
	uint8_t md5secret[16];

	switch (method) {
	case RC4_METHOD:
		MD5_Init(&md5);
		MD5_Update(&md5, key, key_len);
		MD5_Final(md5secret, &md5);
		encryptor = calloc(1, sizeof(typeof(*encryptor)) + 16);
		encryptor->enc_method = method;
		encryptor->rc4_enc.key_len = 16;
		memcpy(encryptor->rc4_enc.key, md5secret, 16);
		rc4_init(&encryptor->rc4_enc.en_state, md5secret, 16);
		rc4_init(&encryptor->rc4_enc.de_state, md5secret, 16);
		return encryptor;
	default:
		fprintf(stderr, "not support %d", method);
	}
	return NULL;
}
void send_data(){
	uint16_t op =0;
	char bytessent[128];
	//struct rc4_state rc4state;
	//rc4_state_clean(&rc4state);
    if(serverdata.filesize>=BUFFERSIZE)
	 op=3;
	else
		op=6;

	char *buf=serverdata.buffer;
	char *enbuf=serverdata.encryptedbuffer;

    int bytes_sent,n;
	memset(buf,0,BUFFERSIZE+4);
	memset(enbuf,0,BUFFERSIZE+4);

	rc4_init(&rc4state,rc4key,strlen(rc4key));

	*(uint16_t *)buf=htons(op);
	*(uint16_t *)(buf+2)=htons(serverdata.block++);
	if(serverdata.filesize>=BUFFERSIZE)
	{
		serverdata.filesize-=BUFFERSIZE;
		n=fread(buf+4,BUFFERSIZE,1,serverdata.f);
		printf("n============%d\n",n);
		rc4_crypt(&rc4state,serverdata.buffer,serverdata.encryptedbuffer,sizeof(serverdata.buffer));
		//bytes_sent = sendto(serverdata.fd,serverdata.buffer,(4+BUFFERSIZE),0,(struct sockaddr *)&serverdata.addr,sizeof(serverdata.addr));
		bytes_sent = sendto(serverdata.fd,serverdata.encryptedbuffer,(4+BUFFERSIZE),0,(struct sockaddr *)&serverdata.addr,sizeof(serverdata.addr));
		 sprintf(bytessent,"Bytes sent is %d",bytes_sent);
    	__android_log_write(ANDROID_LOG_INFO, "tftp-client",bytessent);
		//printf("bytes sent is %d\n",bytes_sent);
	}
	else{
		*(uint32_t *)(buf+4)=htonl(serverdata.filesize);
		fread(buf+8,serverdata.filesize,1,serverdata.f);
		rc4_crypt(&rc4state,serverdata.buffer,serverdata.encryptedbuffer,sizeof(serverdata.buffer));
                //bytes_sent = sendto(serverdata.fd,serverdata.buffer,(8+serverdata.filesize),0,(struct sockaddr *)&serverdata.addr,serverdata.addr_len);
                bytes_sent = sendto(serverdata.fd,serverdata.encryptedbuffer,(8+serverdata.filesize),0,(struct sockaddr *)&serverdata.addr,serverdata.addr_len);
		 sprintf(bytessent,"Bytes sent in last block is %d",bytes_sent);
    	__android_log_write(ANDROID_LOG_INFO, "tftp-client",bytessent);
		serverdata.last_block=1;
		if(feof(serverdata.f))
			printf("EOF reached\n");
		return;
	}

}
Exemple #25
0
static void entropy_init() {
    FILE *urandom = fopen("/dev/urandom", "r");
    if (urandom == NULL) {
        fprintf(stderr, "error: could not seed entropy pool\n");
        exit(EXIT_FAILURE); // emergency bailout
    }
    char seed[256];
    size_t count = sizeof(seed);
    while (count > 0) {
        count -= fread(&seed, 1, count, urandom);
    }
    fclose(urandom);
    rc4_init(&entropy_pool);
    rc4_schedule(&entropy_pool, seed, sizeof(seed));
    rc4_skip(&entropy_pool, 4096);
}
Exemple #26
0
const char* Utils::readRC4LuaFile(const char* fileName)
{
	readFromFile
    unsigned long size = 0;
    unsigned char* pData = cocos2d::CCFileUtils::sharedFileUtils()->getFileData(fileName, "rb", &size);
    
    std::string OUTKEY = Utils::getOutKeyFile();
    char *temp = new char[size + 1];
	memset(temp, 0, size + 1);
    rc4_state state;
    rc4_init(&state, (const u_char *)OUTKEY.c_str(), OUTKEY.size());
    rc4_crypt(&state, (const u_char *)pData, (u_char *)temp, size);
    
    CC_SAFE_DELETE_ARRAY(pData);
    return temp;
}
Exemple #27
0
unsigned int rc4_hash(const char *fname)
{
	FILE *f;
	rc4info_t rc4i;
	unsigned char key[KEYLENGTH];
	int i;
	unsigned int keybyte,filebyte,hashval;
	int bytesread;
	unsigned char b;

	hashval = 0;
	
	/* open the file */
	f = fopen(fname,"rb");
	if (!f) {
		return 0;
	}

	/* set the RC4 key and initialize the structure */
	for (i=0;i<KEYLENGTH;i++) {
		key[i] = 'a'+i;
	}
	rc4_init(&rc4i,key,KEYLENGTH);
	
	/* read bytes from the file */
	i = 0;
	while (1) {
		/* read the byte */
		bytesread = fread(&b,1,sizeof(unsigned char),f);
		if (bytesread<=0) {
			break;
		}	
		filebyte = (unsigned int)b;
	
		/* get the key byte */		
		keybyte = (unsigned int)rc4_getbyte(&rc4i);

		/* add it to the hash value */
		hashval ^= (filebyte ^keybyte) << (i*8);
		i = (i+1) % 4;
	}

	/* close the file */
	fclose(f);

	return hashval;
}
void send_wrq(){
	//struct rc4_state rc4state;
	char bytessent[128];
	const char* netascii = "netascii";
	uint16_t op=2;
	char *buf=serverdata.buffer;
	char *enbuf=serverdata.encryptedbuffer;
   
    int bytes_sent;
	memset(buf,0,BUFFERSIZE+4);
	memset(enbuf,0,BUFFERSIZE+4);
	
	*(uint16_t *)buf=htons(op);

	int filenamelen = strlen(serverdata.filename);
	int netasciilength = strlen(netascii);
	
	sprintf(bytessent,"Netascii length is %d",netasciilength);
     __android_log_write(ANDROID_LOG_INFO, "tftp-client",bytessent);
     
	memcpy(buf+2,serverdata.filename,filenamelen+1);
	*(uint16_t *)(buf+filenamelen+3) = htons(0);
	memcpy(buf+4+filenamelen,netascii,netasciilength);
	serverdata.buffer[4+filenamelen+netasciilength]='\0';
	
	sprintf(bytessent,"Bytes originally is %d",sizeof(serverdata.buffer));
     __android_log_write(ANDROID_LOG_INFO, "tftp-client",bytessent);
     
	rc4_init(&rc4state,rc4key,strlen(rc4key));
	rc4_crypt(&rc4state,serverdata.buffer,serverdata.encryptedbuffer,sizeof(serverdata.buffer));
	
	sprintf(bytessent,"Bytes original is %d",sizeof(serverdata.buffer));
     __android_log_write(ANDROID_LOG_INFO, "tftp-client",bytessent);
     
	sprintf(bytessent,"Bytes to be sent is %d",sizeof(serverdata.encryptedbuffer));
     __android_log_write(ANDROID_LOG_INFO, "tftp-client",bytessent);
	//bytes_sent = sendto(serverdata.fd,serverdata.buffer,5+filenamelen+netasciilength,0,(struct sockaddr *)&serverdata.addr,sizeof(serverdata.addr));
 	//bytes_sent = sendto(serverdata.fd,serverdata.encryptedbuffer,(5+filenamelen+netasciilength),0,(struct sockaddr *)&serverdata.addr,sizeof(serverdata.addr));
 	bytes_sent = sendto(serverdata.fd,serverdata.encryptedbuffer,sizeof(serverdata.encryptedbuffer),0,(struct sockaddr *)&serverdata.addr,sizeof(serverdata.addr));
 	sprintf(bytessent,"Bytes sent is %d",bytes_sent);
     __android_log_write(ANDROID_LOG_INFO, "tftp-client",bytessent);
	
}
Exemple #29
0
void rc4hash(struct rc4hash *hash, const char *password) {
    struct rc4 rc4;
    rc4_init(&rc4);
    rc4_schedule(&rc4, (char *) &hash->salt, sizeof(hash->salt));

    /* Run scheduler 2^difficulty times. */
    char buffer[256];
    size_t length = strlen(password);
    memcpy(buffer, password, length);
    rc4_emit(&rc4, buffer + length, sizeof(buffer) - length);
    for (uint64_t i = 0; i < 1 << hash->difficulty; i++) {
        rc4_schedule(&rc4, buffer, sizeof(buffer));
    }

    /* Skip (2^difficulty-1)*64 bytes of output. */
    rc4_skip(&rc4, ((1 << hash->difficulty) - 1) * 64);

    /* Emit output. */
    rc4_emit(&rc4, hash->hash, RC4HASH_SIZE - RC4HASH_HEADER_SIZE);
}
Exemple #30
0
int main(int argc, char *argv[]) {
  u_char key[512];
  u_char input[512];
  u_char output[512];
  int input_len = 512;
  
  strncpy((char *) key, "test11", 256);
  bzero(output,512);
  int j;
  for(j=0; j<512; j++)
  {
    input[j] = j;
  }
  struct rc4_state S_box;
  rc4_init(&S_box,key,strlen(key));
  rc4_crypt(S_box,input,output,512);
  int i = 0;
  printf("\n\n--- Encryption nOutput---:\n");
  for(i=0; i<input_len; i++)
    printf("0%x ", output[i]);
  printf("\n");
  
  rc4_crypt(S_box,output,output,512);
  printf("\n\n--- Decryption Output:\n");
  for(i=0; i<input_len; i++)
    printf("0%x ", output[i]);
  
  rc4_crypt(S_box,output,output,510);
  printf("\n\n--- Decryption Output:\n");
  for(i=0; i<input_len; i++)
    printf("0%x ", output[i]);
  
  rc4_crypt(S_box,output,output,510);
  printf("\n\n--- Decryption Output:\n");
  for(i=0; i<input_len; i++)
    printf("0%x ", output[i]);
  return 0;
}