20#include "blowfishtables.h"
27#define Q_BYTE_ORDER Q_BIG_ENDIAN
33 m_initialized =
false;
39 for (
int i = 0; i < 256; i++) {
52 for (
int i = 0; i < 18; i++) {
54 for (
int k = 0; k < 4; ++k) {
55 data = (data << 8) | ((
unsigned char *)m_key)[j++];
56 if (j >= m_keylen / 8) {
63 for (
int i = 0; i < 18; i += 2) {
64 encipher(&datal, &datar);
69 for (
int j = 0; j < 4; j++) {
70 for (
int i = 0; i < 256; i += 2) {
71 encipher(&datal, &datar);
73 m_S[j][i + 1] = datar;
79 for (
int i = 0; i < 255; i++) {
80 for (
int j = i + 1; j < 256; j++) {
81 if ((m_S[0][i] == m_S[0][j]) || (m_S[1][i] == m_S[1][j]) ||
82 (m_S[2][i] == m_S[2][j]) || (m_S[3][i] == m_S[3][j])) {
95 delete[](
unsigned char *)m_key;
99int BlowFish::keyLen()
const
104bool BlowFish::variableKeyLen()
const
109bool BlowFish::readyToGo()
const
111 return m_initialized;
114bool BlowFish::setKey(
void *key,
int bitlength)
116 if (bitlength <= 0 || bitlength > 448 || bitlength % 8 != 0) {
120 delete[](
unsigned char *)m_key;
122 m_key =
new unsigned char[bitlength / 8];
123 memcpy(m_key, key, bitlength / 8);
124 m_keylen = bitlength;
129#if Q_BYTE_ORDER == Q_BIG_ENDIAN
130#define shuffle(x) do { \
132 x = (r & 0xff000000) >> 24; \
133 x |= (r & 0x00ff0000) >> 8; \
134 x |= (r & 0x0000ff00) << 8; \
135 x |= (r & 0x000000ff) << 24; \
140int BlowFish::encrypt(
void *block,
int len)
142 uint32_t *d = (uint32_t *)block;
144 if (!m_initialized || len % _blksz != 0) {
148 for (
int i = 0; i < len / _blksz; i++) {
149#if Q_BYTE_ORDER == Q_BIG_ENDIAN
154#if Q_BYTE_ORDER == Q_BIG_ENDIAN
164int BlowFish::decrypt(
void *block,
int len)
166 uint32_t *d = (uint32_t *)block;
168 if (!m_initialized || len % _blksz != 0) {
172 for (
int i = 0; i < len / _blksz; i++) {
173#if Q_BYTE_ORDER == Q_BIG_ENDIAN
178#if Q_BYTE_ORDER == Q_BIG_ENDIAN
188uint32_t BlowFish::F(uint32_t x)
204 y = m_S[0][a] + m_S[1][b];
211void BlowFish::encipher(uint32_t *xl, uint32_t *xr)
217 for (
int i = 0; i < 16; ++i) {
221 temp = Xl; Xl = Xr; Xr = temp;
225 temp = Xl; Xl = Xr; Xr = temp;
234void BlowFish::decipher(uint32_t *xl, uint32_t *xr)
240 for (
int i = 17; i > 1; --i) {
244 temp = Xl; Xl = Xr; Xr = temp;
248 temp = Xl; Xl = Xr; Xr = temp;