示例#1
0
int main(int argc, char *argv[])
{
  // Key and 16 byte block as per FIPS 197, Appendix B (Cipher Example)

  byte_ard pKey[] = {
    0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c 
  };

  // The ciphertext according to the FIPS and block_e.cpp
  byte_ard pCipherBlock[] = {
    0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32 
  };

  // Allocate memory and generate the key schedule
  byte_ard pKeys[KEY_BYTES*12];
  KeyExpansion(pKey, pKeys);

  printf("\nCiphertext: \n");
  printBytes2((unsigned char *)pCipherBlock, BLOCK_SIZE);

  #ifdef print_key_schedule
  printf("\nKey Schdedule: \n");
  printBytes2((unsigned char *)pKeys, BLOCK_SIZE*11);
  printf("\n");
  #endif

  // Decrypt the single block
  DecryptBlock(pCipherBlock, (const u_int32_ard*)pKeys);
  printf("\nPlaintext: \n");
  printBytes2((unsigned char *)pCipherBlock, BLOCK_SIZE);
  
  return 0;
}
示例#2
0
std::vector<char> Blowfish::Decrypt(const std::vector<char> &src) const {
    std::vector<char> dst = src;

    for (int i = 0; i < dst.size() / sizeof(uint64_t); ++i) {
        uint32_t *left = &reinterpret_cast<uint32_t *>(dst.data())[i * 2];
        uint32_t *right = &reinterpret_cast<uint32_t *>(dst.data())[i * 2 + 1];
        DecryptBlock(left, right);
    }

    size_t padding_length = PKCS5PaddingLength(dst);
    dst.resize(dst.size() - padding_length);
    return dst;
}
示例#3
0
void Blowfish::Decrypt(unsigned char* dst, const unsigned char* src, int byte_length) const
{
    if (dst != src)
    {
        memcpy(dst, src, byte_length);
    }
    
    for (int i = 0; i < byte_length / sizeof(uint64_t); ++i)
    {
        uint32_t* left  = &reinterpret_cast<uint32_t*>(dst)[i * 2];
        uint32_t* right = &reinterpret_cast<uint32_t*>(dst)[i * 2 + 1];
        DecryptBlock(left, right);
    }
}
示例#4
0
// CBCDecrypt()
//
// C_0 = IV
// P_i = D_k(C_i) XOR C_{i-1} 
//
// Removing the padding probably isnt neccesary, since the CBC
// leaves the null char intact at the end of the cstring. But what 
// about binary files and etc?
void CBCDecrypt(void* pText, void* pBuffer, u_int32_ard length,
                const u_int32_ard *pKeys, const u_int16_ard *pIV)
{
  byte_ard *cText = (byte_ard*)pText;
  byte_ard *cBuffer = (byte_ard*)pBuffer;
  byte_ard lastblock[BLOCK_BYTE_SIZE];
  byte_ard tempblock[BLOCK_BYTE_SIZE];
  byte_ard currblock[BLOCK_BYTE_SIZE];
  u_int32_ard blocks = length/BLOCK_BYTE_SIZE;
  
  memcpy(lastblock,pIV,BLOCK_BYTE_SIZE);

  for (u_int32_ard i = 0; i < blocks; i++)
  {
    #if defined(unroll_cbc_decrypt_loop)
    currblock[0] = cText[(i*BLOCK_BYTE_SIZE)+0];
    tempblock[0] = currblock[0];
    currblock[1] = cText[(i*BLOCK_BYTE_SIZE)+1];
    tempblock[1] = currblock[1];
    currblock[2] = cText[(i*BLOCK_BYTE_SIZE)+2];
    tempblock[2] = currblock[2];
    currblock[3] = cText[(i*BLOCK_BYTE_SIZE)+3];
    tempblock[3] = currblock[3];
    currblock[4] = cText[(i*BLOCK_BYTE_SIZE)+4];
    tempblock[4] = currblock[4];
    currblock[5] = cText[(i*BLOCK_BYTE_SIZE)+5];
    tempblock[5] = currblock[5];
    currblock[6] = cText[(i*BLOCK_BYTE_SIZE)+6];
    tempblock[6] = currblock[6];
    currblock[7] = cText[(i*BLOCK_BYTE_SIZE)+7];
    tempblock[7] = currblock[7];
    currblock[8] = cText[(i*BLOCK_BYTE_SIZE)+8];
    tempblock[8] = currblock[8];
    currblock[9] = cText[(i*BLOCK_BYTE_SIZE)+9];
    tempblock[9] = currblock[9];
    currblock[10] = cText[(i*BLOCK_BYTE_SIZE)+10];
    tempblock[10] = currblock[10];
    currblock[11] = cText[(i*BLOCK_BYTE_SIZE)+11];
    tempblock[11] = currblock[11];
    currblock[12] = cText[(i*BLOCK_BYTE_SIZE)+12];
    tempblock[12] = currblock[12];
    currblock[13] = cText[(i*BLOCK_BYTE_SIZE)+13];
    tempblock[13] = currblock[13];
    currblock[14] = cText[(i*BLOCK_BYTE_SIZE)+14];
    tempblock[14] = currblock[14];
    currblock[15] = cText[(i*BLOCK_BYTE_SIZE)+15];
    tempblock[15] = currblock[15];

    DecryptBlock((void*)currblock, pKeys);

    cBuffer[(i*BLOCK_BYTE_SIZE)+0] = currblock[0]  ^ lastblock[0];
    lastblock[0] = tempblock[0];
    cBuffer[(i*BLOCK_BYTE_SIZE)+1] = currblock[1]  ^ lastblock[1];
    lastblock[1] = tempblock[1];
    cBuffer[(i*BLOCK_BYTE_SIZE)+2] = currblock[2]  ^ lastblock[2];
    lastblock[2] = tempblock[2];
    cBuffer[(i*BLOCK_BYTE_SIZE)+3] = currblock[3]  ^ lastblock[3];
    lastblock[3] = tempblock[3];
    cBuffer[(i*BLOCK_BYTE_SIZE)+4] = currblock[4]  ^ lastblock[4];
    lastblock[4] = tempblock[4];
    cBuffer[(i*BLOCK_BYTE_SIZE)+5] = currblock[5]  ^ lastblock[5];
    lastblock[5] = tempblock[5];
    cBuffer[(i*BLOCK_BYTE_SIZE)+6] = currblock[6]  ^ lastblock[6];
    lastblock[6] = tempblock[6];
    cBuffer[(i*BLOCK_BYTE_SIZE)+7] = currblock[7]  ^ lastblock[7];
    lastblock[7] = tempblock[7];
    cBuffer[(i*BLOCK_BYTE_SIZE)+8] = currblock[8]  ^ lastblock[8];
    lastblock[8] = tempblock[8];
    cBuffer[(i*BLOCK_BYTE_SIZE)+9] = currblock[9]  ^ lastblock[9];
    lastblock[9] = tempblock[9];
    cBuffer[(i*BLOCK_BYTE_SIZE)+10] = currblock[10]  ^ lastblock[10];
    lastblock[10] = tempblock[10];
    cBuffer[(i*BLOCK_BYTE_SIZE)+11] = currblock[11]  ^ lastblock[11];
    lastblock[11] = tempblock[11];
    cBuffer[(i*BLOCK_BYTE_SIZE)+12] = currblock[12]  ^ lastblock[12];
    lastblock[12] = tempblock[12];
    cBuffer[(i*BLOCK_BYTE_SIZE)+13] = currblock[13]  ^ lastblock[13];
    lastblock[13] = tempblock[13];
    cBuffer[(i*BLOCK_BYTE_SIZE)+14] = currblock[14]  ^ lastblock[14];
    lastblock[14] = tempblock[14];
    cBuffer[(i*BLOCK_BYTE_SIZE)+15] = currblock[15]  ^ lastblock[15];
    lastblock[15] = tempblock[15];
    #else

    // copy the data to 'currblock', to be deciphered. 
    for (u_int16_ard j = 0; j < BLOCK_BYTE_SIZE; j++)
    {
      currblock[j] = cText[(i*BLOCK_BYTE_SIZE)+j];
      tempblock[j] = currblock[j];
    }

    DecryptBlock((void*)currblock, pKeys);

    // xor the decphered block with last latest cipherblock, C_{i-1} 
    for (u_int16_ard j = 0; j < BLOCK_BYTE_SIZE; j++)
    {
      cBuffer[(i*BLOCK_BYTE_SIZE)+j] = currblock[j]  ^ lastblock[j];
      lastblock[j] = tempblock[j];
    }

    #endif
  }
  
} // CBCDecrypt()
示例#5
0
// 解密PNG图片
void DecryptPNG(const std::vector<std::string> &filelist, const aes_key &key)
{
	for (auto &filename : filelist)
	{
		std::ifstream in_file(filename, std::ios::binary | std::ios::ate);
		if (!in_file.is_open())
		{
			std::cerr << "打开" << filename << " 失败!" << std::endl;
			return;
		}

		// 读取数据块位置
		uint64_t end_pos = in_file.tellg();
		in_file.seekg(end_pos - sizeof(uint64_t));
		uint64_t block_start_pos = *reinterpret_cast<uint64_t *>(&(ReadSome<sizeof(uint64_t)>(in_file)[0]));
		in_file.seekg(block_start_pos);

		// 解密数据块信息
		auto block_info = ReadLarge(in_file, uint32_t(end_pos - sizeof(uint64_t) - block_start_pos));
		DecryptBlock(block_info, key);

		// 验证数据块内容
		auto block_head = ReadSome<sizeof(BLOCK_HEAD)>(block_info);
		for (unsigned int i = 0; i < block_head.size(); ++i)
		{
			if (block_head[i] != BLOCK_HEAD[i])
			{
				std::cerr << "密钥错误,解密" << filename << " 失败!" << std::endl;
				return;
			}
		}

		std::ofstream out_file(path::splitext(filename)[0] + ".png", std::ios::binary);
		if (!out_file.is_open())
		{
			std::cerr << "创建" << path::splitext(filename)[1] << ".png" << " 失败!" << std::endl;
			continue;
		}

		// 写入文件头
		WriteToSteam(HEAD_DATA, sizeof(HEAD_DATA), out_file);

		// 读取数据块
		uint64_t read_size = 0;
		while (true)
		{
			// 读取数据块信息
			Block block;
			memcpy(&block, &ReadSome<sizeof(Block)>(block_info)[0], sizeof(Block));
			if (block_info.eof())
			{
				out_file.clear();
				std::cerr << "the %s file format error!" << std::endl;
				break;
			}

			// 写入数据块长度
			char reverse_size[sizeof(block.size)];
			memcpy(reverse_size, &block.size, sizeof(reverse_size));
			std::reverse(reverse_size, reverse_size + sizeof(reverse_size));
			WriteToSteam(reverse_size, sizeof(reverse_size), out_file);

			// 写入数据块名称
			WriteToSteam(&block.name, sizeof(block.name), out_file);

			// 写入数据块内容
			std::string s_name(block.name, sizeof(block.name));
			if (strcmp(s_name.c_str(), "IHDR") == 0)
			{
				IHDRBlock ihdr;
				memcpy(&ihdr, &block, sizeof(Block));
				memcpy(((char *)&ihdr) + sizeof(Block), &ReadSome<sizeof(IHDRBlock) - sizeof(Block)>(block_info)[0], sizeof(IHDRBlock) - sizeof(Block));
				WriteToSteam(ihdr.data, sizeof(ihdr.data), out_file);
			}
			else if (strcmp(s_name.c_str(), "IEND") == 0)
			{
				WriteToSteam(IEND_DATA, sizeof(IEND_DATA), out_file);
				std::cout << "成功解密:" << filename << std::endl;
				break;
			}
			else
			{
				in_file.seekg(read_size);
				StreamMove(out_file, in_file, block.size + CRC_SIZE);
				read_size += block.size + CRC_SIZE;
			}
		}
	}
}
void Cbc512NoPaddingCryptoProvider::Decrypt(const uint8_t *pbIn,
                                            uint32_t       cbInUnsafe,
                                            uint32_t       dwStartingBlockNumberUnsafe,
                                            bool           isFinal,
                                            uint8_t       *pbOut,
                                            uint32_t       cbOutUnsafe,
                                            uint32_t      *pcbOut)
{
  auto cbIn                  = cbInUnsafe, cbOut = cbOutUnsafe,
       dwStartingBlockNumber = dwStartingBlockNumberUnsafe;

  if (pbIn == nullptr) {
    throw exceptions::RMSCryptoNullPointerException("Null pointer pbIn exception");
  }

  if (!isFinal && (0 != cbIn % CBC512_BLOCK_SIZE)) {
    throw exceptions::RMSCryptoInvalidArgumentException("Block is not aligned");
  }

  if (0 != cbIn % AES128_BLOCK_SIZE) {
    throw exceptions::RMSCryptoInvalidArgumentException("Block is not aligned");
  }

  if (nullptr == pcbOut) {
    throw exceptions::RMSCryptoNullPointerException("Null pointer pcbOut exception");
  }

  if (nullptr == pbOut)
  {
    // No need to do the decryption, just return the number of bytes required
    *pcbOut = cbIn;
    return;
  }

  if ((cbIn % AES128_BLOCK_SIZE) != 0) {
    throw exceptions::RMSCryptoInvalidArgumentException("Block is not aligned");
  }

  if (nullptr == pbOut)
  {
    // No need to do the decryption, just return the number of uint8_ts required
    *pcbOut = cbIn;
    return;
  }

  if (cbOut < cbIn) {
    throw exceptions::RMSCryptoInsufficientBufferException("Insufficient buffer");
  }

  auto cbResult = 0;

  while (cbIn >= CBC512_BLOCK_SIZE)
  {
    if (cbOut < CBC512_BLOCK_SIZE) {
      throw exceptions::RMSCryptoInsufficientBufferException("Insufficient buffer");
    }

    DecryptBlock(pbIn,
                 CBC512_BLOCK_SIZE,
                 dwStartingBlockNumber,
                 false,
                 pbOut,
                 cbOut);

    pbIn += CBC512_BLOCK_SIZE;
    cbIn -= CBC512_BLOCK_SIZE;

    pbOut += CBC512_BLOCK_SIZE;
    cbOut -= CBC512_BLOCK_SIZE;

    ++dwStartingBlockNumber;

    cbResult += CBC512_BLOCK_SIZE;
  }

  if (!isFinal && (cbIn != 0)) {
    throw exceptions::RMSCryptoInvalidArgumentException("Invalid aligment");
  }

  if (isFinal && (cbIn > 0))
  {
    // Note that the cbIn can't be 0, the final block always should have at
    // least AES128_BLOCK_SIZE (16) uint8_ts
    if (cbIn < AES128_BLOCK_SIZE) {
      throw exceptions::RMSCryptoInsufficientBufferException("Insufficient buffer");
    }
    cbResult +=
      DecryptBlock(pbIn, cbIn, dwStartingBlockNumber, true, pbOut, cbOut);
  }

  *pcbOut = cbResult;
}
示例#7
0
void
LRWMode::Decrypt(ThreadContext& context, uint8 *data, size_t length)
{
	DecryptBlock(context, data, length, fOffset);
}
示例#8
0
void
XTSMode::Decrypt(ThreadContext& context, uint8 *data, size_t length)
{
	DecryptBlock(context, data, length, 0);
}
示例#9
0
void RX_ISR(void)
{
    received = U1ARXREG;
    PORTD = 2;
    if(received == 10)
    {
        i = 0;
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -2;
        }
        TMR2 = 0;
        while(username[i] != NULL)
        {
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = username[i];
            }
            i++;
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = -2;
            }
        }
        DelayMsec(5000);

        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -3;
        }
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -2;
        }
        i = 0;
        while(password[i] != NULL)
        {
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = password[i];
            }
            i++;
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = -2;
            }
        }
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -3;
        }

    }

    else if(received == -7)
    {
        if(receive_user == -1)
            receive_user = 1;
        else if(receive_user == 1)
            receive_user = 0;
        else
            receive_user = -1;
        i = 0;
    }

    else if(received == -4)
    {
        receive_file = 1;
        file_pt = 0;
    }

    else if(received == -5)
    {
        receive_file = 0;
        file_pt = 0;
        //BYTE Key[2] = {'1','2'};
	BYTE RoundKey[6];
	Extend_Key(Key,RoundKey);
	while(file[file_pt] != NULL)
	{
		BYTE temp[2];
		BYTE temp_result[2];
		temp[0] = file[file_pt];
		temp[1] = file[file_pt+1];
		EncryptBlock(temp,RoundKey,temp_result);
		result[file_pt] = temp_result[0];
		result[file_pt+1] = temp_result[1];
		file_pt = file_pt + 2;
	}
        file_pt = 0;
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -2;
        }
        while(result[file_pt] != NULL)
        {
            char to_send = result[file_pt];
            file_pt++;
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = to_send;
            }
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = -2;
            }
        }
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -3;
        }
        file_pt = 0;
    }

    else if(received == -6)
    {
        receive_file = 0;
        file_pt = 0;
        //BYTE Key[2] = {'1','2'};
	BYTE RoundKey[6];
	Extend_Key(Key,RoundKey);
	while(file[file_pt] != NULL)
	{
		BYTE temp[2];
		BYTE temp_result[2];
		temp_result[0] = file[file_pt];
		temp_result[1] = file[file_pt+1];
		DecryptBlock(temp_result,RoundKey,temp);
		file[file_pt] = temp[0];
		file[file_pt+1] = temp[1];
		file_pt = file_pt + 2;
	}
        file_pt = 0;
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -2;
        }
        while(file[file_pt] != NULL)
        {
            char to_send = file[file_pt];
            file_pt++;
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = to_send;
            }
            TMR2 = 0;
            while(TMR2 < 400)
            {
                U1ATXREG = -2;
            }
        }
        TMR2 = 0;
        while(TMR2 < 400)
        {
            U1ATXREG = -3;
        }
        file_pt = 0;
    }

    if(receive_file == 1 && received != -4 && received != -5)
    {
        file[file_pt] = received;
        file_pt ++;
    }

    if(receive_user == 1 && received != -7)
    {
        username[i] = received;
        i++;
    }

    else if(receive_user == 0 && received != -7)
    {
        password[i] = received;
        i++;
    }
    PORTDINV = 2;
    IFS0bits.U1RXIF = 0;
}