Ejemplo n.º 1
0
Archivo: AES.cpp Proyecto: ebakan/AES
//Encrypts stream with AES cipher
void AES::encryptStream (int keySize, uint8_t* iv, std::istream* in, std::ostream* out, uint8_t* key) {
    uint8_t* expandedKey=expandKey(keySize,key); //generate expanded key

    uint8_t newiv[16]; //temp iv to encrypt
    for(int i=0;i<16;i++)
        newiv[i]=iv[i];
    encryptBlock(keySize,newiv,expandedKey);
    out->write((char*)newiv,16);

    uint8_t block[16];
    uint8_t pnum=1; //padding counter
    char c;
    while(!in->eof()) {
        for(int i=0;i<16;i++) { //read block
            c=in->get(); //check if next char is eof
            if(!in->eof())
                block[i]=(uint8_t) c;
            else  //pad with chars
                block[i]=pnum++;
        }
        encryptBlock(keySize,block,expandedKey);
        out->write((char*)block,16);
    }
    if(pnum==1) {
        for(int i=0;i<16;i++)
            block[i]=(uint8_t)i;
        encryptBlock(keySize,block,expandedKey);
        out->write((char*)block,16);
    }
}
Ejemplo n.º 2
0
/*
 * CBC encryption routine
 * size of 'input' has to be multiple of 16
 * 'input' contains ciphertext on exit
 * 'iv' must hold a 16 byte initialization vector
 */  
int encryptCBC(unsigned char *input, int len, unsigned char *key, int klen,
               unsigned char *iv)
{
	int i;
	unsigned char w[32 * 15];
	unsigned char k[32] = {0};
    unsigned char piv[16];

	if (input == 0 || key == 0 || iv == 0) {
		return 0;
	}

    memcpy(piv, iv, 16);

	memcpy(k, key, min(klen, 32));
	KeyExpansion(k, w, 8);

	for (i = 0; i < len; i += 16) {
        int n;
        for (n = 0; n < 16; n++) {
            input[i+n] ^= piv[n];
        }
		encryptBlock(&input[i], &input[i], w, 14);
        memcpy(piv, &input[i], 16);
	}

	return 1;
}
Ejemplo n.º 3
0
Archivo: AES.cpp Proyecto: ebakan/AES
//Encrypts plaintext with AES cipher
void AES::encrypt(int keySize, uint64_t numBytes, uint8_t* iv, uint8_t*& data, uint8_t* key) {
    int size=numBytes+16; //input array length + iv length
    if(numBytes%16!=0)
        size+=16-numBytes%16; //pads up to block size
    else
        size+=16; //else pads another block

    uint8_t* encrypted=new uint8_t[size]; //output array

    for(int i=0;i<16;i++) //copy over iv
        encrypted[i]=iv[i];

    uint8_t pnum=1;
    for(int i=16;i<size;i++) { //copy over array, pad if necessary
        if(i<(int) numBytes)
            encrypted[i]=data[i];
        else
            encrypted[i]=pnum++;
    }

    data=encrypted;

    uint8_t* expandedKey=expandKey(keySize,key); //generate expanded key

    for(int i=0;i<size/16;i++) { //encrypt each block
        encryptBlock(keySize,data+i*16,expandedKey);
    }
    
}
Ejemplo n.º 4
0
QString QtAes::encrypt(const QString &input) const
{
    // padding 0
    QByteArray inArray = input.toUtf8();
    int len = inArray.length();
    if(len % AES_BLOCK_SIZE != 0) {
        int pad = AES_BLOCK_SIZE - len % AES_BLOCK_SIZE;
        for(int i =0; i < pad; i++) {
            inArray.append('\0');
        }
        len = inArray.length();
    }

    //  encrypt every block
    QByteArray outArray;
    const unsigned char *inbuffer = (const unsigned char *)inArray.data();
    int remain = len;
    while(remain > 0) {
        encryptBlock(inbuffer, outArray);
        remain -= AES_BLOCK_SIZE;
        inbuffer += AES_BLOCK_SIZE;
    }

    // to base64
    QByteArray base64Array = outArray.toBase64();

    // DONE
    return QString(base64Array);
}
Ejemplo n.º 5
0
void Blowfish::init(const u_char *key, u_int len)
{
    int i, j, k;
    for (i = 0; i < 4; i++)
        for (j = 0; j < 256; j++)
            S[i][j] = ORIG_S[i][j];

    j = 0;
    u_int data;
    for (i = 0; i < N + 2; i++)
    {
        data = 0x00000000;
        for (k = 0; k < 4; k++)
        {
            data = (data << 8) | key[j];
            j = (j+1)%len;
        }
        P[i] = ORIG_P[i] ^ data;
    }

    u_int l = 0x00000000;
    u_int r = 0x00000000;
    for (i = 0; i < N + 2; i += 2)
    {
        encryptBlock(&l, &r);
        P[i] = l;
        P[i + 1] = r;
    }

    for (i = 0; i < 4; i ++)
        for (j = 0; j < 256; j+=2)
        {
            encryptBlock(&l, &r);
            S[i][j] = l;
            S[i][j + 1] = r;
        }
}
Ejemplo n.º 6
0
/*
 * ECB encryption routine
 * size of 'input' has to be multiple of 16
 * 'input' contains ciphertext on exit
 */  
int encrypt(unsigned char *input, int len, unsigned char *key, int klen)
{
	int i;
	unsigned char w[32 * 15];
	unsigned char k[32] = {0};

	if (input == 0 || key == 0) {
		return 0;
	}

	memcpy(k, key, min(klen, 32));
	KeyExpansion(k, w, 8);

	for (i = 0; i < len; i += 16) {
		encryptBlock(&input[i], &input[i], w, 14);
	}

	return 1;
}
Ejemplo n.º 7
0
u_char * Blowfish::encode(const u_char *message, u_int Size)
{
    u_char* result;
    u_int resSize = Size%8;
    if (resSize != 0)
        resSize = Size + 8 - resSize;
    else
        resSize = Size;
    result = new u_char[resSize];
    for (u_int i = 0; i < Size; i++)
        result[i] = message[i];
    for (u_int i = Size; i < resSize; i++)
        result[i] = 0;
    for (u_int i = 0; i < resSize; i+= 8)
    {
        encryptBlock((u_int*)(result+i), (u_int*)(result+i+4));
    }
    return result;
}
Ejemplo n.º 8
0
int main(int argc, char *argv[])
{
	byte shortkey[16] = {0x2b, 0x7e, 0x15, 0x16, 0x028, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};

	byte expandedkey[176];

	keyExpansion(shortkey, expandedkey);

	byte state[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};

	//correct cipher text is 3ad77bb40d7a3660a89ecaf32466ef97
	encryptBlock(state, expandedkey);

	int i;
	printf("\n");
	for(i = 0;i < 16;i++){
			printf(" %02x ", state[i]);
	}

	return 0;
}