예제 #1
0
void rijndael256Encrypt(const char *plaindata, unsigned char password[KEYLENGTH(KEYBITS)], unsigned char cipherdata[]) {
    unsigned long rk[RKLENGTH(KEYBITS)];
    unsigned char key[KEYLENGTH(KEYBITS)];
    int nrounds;
    for (int i = 0; i < sizeof(key); i++)
        key[i] = *password != 0 ? *password++ : 0;
    
    // Add PKCS7 padding.
    unsigned long data_len = strlen(plaindata);
    unsigned char new_plaindata[data_len];
    memcpy(new_plaindata, plaindata, data_len);
    unsigned int pad = 16 - (data_len%16);
    int y = 0;
    for (; y < pad; new_plaindata[data_len + y++] = (char)pad);
    new_plaindata[data_len + y + 1] = 0;
    data_len = data_len + y;

    nrounds = rijndaelSetupEncrypt(rk, key, KEYBITS);
    unsigned char iv[16];
    for (int i = 0; i < sizeof(iv); i++) {
        iv[i] = 0;
    }
    for (int i = 0; i < data_len/16; i++) {
        unsigned char plainblock[16];
        unsigned char cipherblock[16];
        for (int j = 0; j < sizeof(plainblock); j++) {
            plainblock[j] = new_plaindata[i*16+j] ^ iv[j];
        }
        rijndaelEncrypt(rk, nrounds, plainblock, cipherblock);
        for (int k = 0; k < sizeof(cipherblock); k++) {
            cipherdata[i*16+k] = iv[k] = cipherblock[k];
        }
    }
}
예제 #2
0
파일: decrypt.c 프로젝트: modsix/destroyd
int main(int argc, char **argv)
{
  unsigned long rk[RKLENGTH(KEYBITS)];
  unsigned char key[KEYLENGTH(KEYBITS)];
  int i;
  int nrounds;
  char *password;
  FILE *input;
  if (argc < 3)
  {
    fputs("Missing argument", stderr);
    return 1;
  }
  password = argv[1];
  for (i = 0; i < sizeof(key); i++)
    key[i] = *password != 0 ? *password++ : 0;
  input = fopen(argv[2], "rb");
  if (input == NULL)
  {
    fputs("File read error", stderr);
    return 1;
  }
  nrounds = rijndaelSetupDecrypt(rk, key, 256);
  while (1)
  {
    unsigned char plaintext[16];
    unsigned char ciphertext[16];
    int j;
    if (fread(ciphertext, sizeof(ciphertext), 1, input) != 1)
      break;
    rijndaelDecrypt(rk, nrounds, ciphertext, plaintext);
    fwrite(plaintext, sizeof(plaintext), 1, stdout);
  }
  fclose(input);
}
예제 #3
0
void rijndael256Decrypt(const char *cipherdata, unsigned char password[KEYLENGTH(KEYBITS)], unsigned char plaindata[]) {
    unsigned long rk[RKLENGTH(KEYBITS)];
    unsigned char key[KEYLENGTH(KEYBITS)];
    int nrounds;
    for (int i = 0; i < sizeof(key); i++)
        key[i] = *password != 0 ? *password++ : 0;
    
    unsigned long data_len = strlen(cipherdata);
    
    nrounds = rijndaelSetupDecrypt(rk, key, KEYBITS);
    unsigned char iv[16];
    for (int i = 0; i < sizeof(iv); i++) {
        iv[i] = 0;
    }
    unsigned char tmp_plaindata[data_len];
    for (int i = 0; i < data_len/16; i++) {
        unsigned char plainblock[16];
        unsigned char cipherblock[16];
        for (int j = 0; j < sizeof(cipherblock); j++) {
            cipherblock[j] = cipherdata[i*16+j];
        }
        rijndaelDecrypt(rk, nrounds, cipherblock, plainblock);
        for (int k = 0; k < sizeof(plainblock); k++) {
            tmp_plaindata[i*16+k] = plainblock[k] ^ iv[k];
            iv[k] = cipherblock[k];
        }
    }
    // Strip PKCS7 padding.
    unsigned int padding = 1;
    unsigned int pad = tmp_plaindata[data_len - 1];
    for (int i = 0; i < pad; i++) {
        if ((unsigned int)tmp_plaindata[data_len - i - 1] != pad) {
            padding = 0;
            break;
        }
    }
    if (padding == 1) {
        data_len = data_len - pad;
    }
    memcpy(plaindata, tmp_plaindata, data_len);
    plaindata[data_len] = 0;
}
예제 #4
0
char *snippet_encrypt(const char *plain_text, const char *password)
{
    unsigned long rk[RKLENGTH(KEYBITS)];
    unsigned char key[KEYLENGTH(KEYBITS)];
    int i;
    int nrounds;
    for (i = 0; i < sizeof(key); i++)
    {
        key[i] = *password != 0 ? *password++ : 0;
    }
    
    nrounds = rijndaelSetupEncrypt(rk, key, KEYBITS);    
    char **blocks = __snippet_split_to_blocks(plain_text);
    __snippet_encrypt_blocks(rk, nrounds, blocks);
    return NULL;
}
예제 #5
0
char *snippet_decrypt(const char *cipher_text, const char *password)
{
    unsigned long rk[RKLENGTH(KEYBITS)];
    unsigned char key[KEYLENGTH(KEYBITS)];
    int i;
    int nrounds;
    for (i = 0; i < sizeof(key); i++)
    {
        key[i] = *password != 0 ? *password++ : 0;
    }
    
    nrounds = rijndaelSetupDecrypt(rk, key, KEYBITS);
    
    char *plain_text = (char *)malloc(sizeof(char) * strlen(cipher_text));
    rijndaelDecrypt(rk, nrounds, cipher_text, plain_text);
    return plain_text;
    
}
예제 #6
0
파일: AES.cpp 프로젝트: qifuluo/Humble
void CAES::setKey(const char *pszKey, const unsigned int uiKeyType)
{
    size_t iRKLens(RKLENGTH(uiKeyType));
    size_t iKeyLens(KEYLENGTH(uiKeyType));

    m_pEncodeRK = new(std::nothrow) unsigned long[iRKLens + 1];
    H_ASSERT(NULL != m_pEncodeRK, "malloc memory error.");
    H_Zero(m_pEncodeRK, iRKLens + 1);

    m_pDecodeRK = new(std::nothrow) unsigned long[iRKLens + 1];
    H_ASSERT(NULL != m_pDecodeRK, "malloc memory error.");
    H_Zero(m_pDecodeRK, iRKLens + 1);

    unsigned char * pKey = new(std::nothrow) unsigned char[iKeyLens + 1];
    H_ASSERT(NULL != pKey, "malloc memory error.");
    H_Zero(pKey, iKeyLens + 1);
    memcpy(pKey, pszKey, strlen(pszKey) > iKeyLens ? iKeyLens : strlen(pszKey));

    m_iEncodeRounds = rijndaelSetupEncrypt(m_pEncodeRK, pKey, uiKeyType);
    m_iDecodeRounds = rijndaelSetupDecrypt(m_pDecodeRK, pKey, uiKeyType);

    H_SafeDelArray(pKey);
}
예제 #7
0
crypto_cstring aesDecrypt(const char *data, const unsigned long dataLength, const char* password, unsigned long passwordLength)
{
    assert(passwordLength == AES_KEYBYTE);
    
    int n = 0;
    
    if (dataLength / AES_BLOCKBYTE == dataLength * 1.0 / AES_BLOCKBYTE) n = dataLength / AES_BLOCKBYTE;
    else n = dataLength / AES_BLOCKBYTE + 1;
    
    crypto_cstring answer;
    
    answer.length = n * AES_BLOCKBYTE;
    
    answer.context = malloc(sizeof(char) * (answer.length) + 100);
    
    memcpy(answer.context, data, sizeof(char) * dataLength);
    
    unsigned long longi;

    for (longi = dataLength ; longi < (answer.length) ; ++longi) answer.context[longi] = '\0';
    
    unsigned long rk[RKLENGTH(AES_KEYBIT)];
	unsigned char key[KEYLENGTH(AES_KEYBIT)];
    
    int i = 0;
	for (i = 0; i < sizeof(key); i++) key[i] = password != 0 ? *password++ : 0;
	
	int nrounds = rijndaelSetupDecrypt(rk, key, AES_KEYBIT);
	
    int index = 0;
    int lastIndex;
	
	while (1)
	{
        lastIndex = index;
		unsigned char plaintext[32];
		unsigned char ciphertext[32];
		int j;
		for (j = 0; j < 16; j++)
		{
			if (index >= answer.length)
				break;
			
			plaintext[j] = answer.context[index++];
		}
		if (j == 0)
			break;
		for (; j < sizeof(plaintext); j++)
			plaintext[j] = '\0';
        
		rijndaelDecrypt(rk, nrounds, plaintext, ciphertext);
        memcpy(answer.context + lastIndex, ciphertext, AES_BLOCKBYTE);
        
	}
    
    answer.length = ((uint64_t*) answer.context)[LENGTHPOSITION(answer.length)];
    
    for (longi = dataLength - 1 ; i >= (answer.length) ; --i) answer.context[i] = '\0';
    
    return answer;
}