We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / crypto / qxtblowfish.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtCrypto module of the Qt eXTension library
6 **
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
9 ** IBM.
10 **
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
15 **
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24
25
26 #include "qxtblowfish.h"
27 #include <openssl/blowfish.h>
28
29
30
31 /**
32 \class QxtBlowFish QxtBlowFish
33
34 \ingroup QxtCrypto
35
36 \brief  Blowfish Encryption Class
37
38
39 useage:
40 \code
41 QxtBlowFish() fish;
42 fish.setPassword("foobar").
43
44 QByteArray a("barblah");
45
46 a= fish.encrypt(a);
47 a= fish.decrypt(a);
48 \endcode
49 */
50
51
52
53
54
55 QxtBlowFish::QxtBlowFish(QObject * parent) :QObject(parent)
56 {
57     key=new BF_KEY;
58 }
59
60 QxtBlowFish::~QxtBlowFish()
61 {
62     delete(key);
63 }
64
65
66 void QxtBlowFish::setPassword(QByteArray k )
67 {
68     BF_set_key(key, k.count() , (unsigned char *)k.constData ());
69 }
70
71
72
73 QByteArray  QxtBlowFish::encrypt(QByteArray in)
74 {
75     QByteArray out(in);
76     int num =0;
77     unsigned char  ivec [9];
78     ivec[0]=(unsigned char )3887;
79     ivec[1]=(unsigned char )3432;
80     ivec[2]=(unsigned char )3887;
81     ivec[3]=(unsigned char )2344;
82     ivec[4]=(unsigned char )678;
83     ivec[5]=(unsigned char )3887;
84     ivec[6]=(unsigned char )575;
85     ivec[7]=(unsigned char )2344;
86     ivec[8]=(unsigned char )2222;
87
88
89     BF_cfb64_encrypt(
90         (unsigned char *)in.constData (),
91         (unsigned char *)out.data(),
92         in.size(),
93         key,
94         ivec,
95         &num,
96         BF_ENCRYPT
97     );
98
99
100     out=out.toBase64();
101     return out;
102
103 }
104
105
106 QByteArray  QxtBlowFish::decrypt(QByteArray in)
107
108 {
109     in=QByteArray::fromBase64(in);
110
111     QByteArray out(in);
112
113     int num =0;
114     unsigned char  ivec [9];
115     ivec[0]=(unsigned char )3887;
116     ivec[1]=(unsigned char )3432;
117     ivec[2]=(unsigned char )3887;
118     ivec[3]=(unsigned char )2344;
119     ivec[4]=(unsigned char )678;
120     ivec[5]=(unsigned char )3887;
121     ivec[6]=(unsigned char )575;
122     ivec[7]=(unsigned char )2344;
123     ivec[8]=(unsigned char )2222;
124
125
126     BF_cfb64_encrypt(
127         (unsigned char *)in.constData (),
128         (unsigned char *)out.data(),
129         in.size(),
130         key,
131         ivec,
132         &num,
133         BF_DECRYPT
134     );
135
136
137     return out;
138 }