112
113
encKeyLen = decKeyLen;
114
115
// both keys missing
115
setLastError(__tr("Missing both encrypt and decrypt key: at least one is needed"));
116
setLastError(__tr2qs("Missing both encrypt and decrypt key: at least one is needed"));
120
121
int defLen = getKeyLen();
122
char * encryptKey = (char *)kvi_malloc(defLen);
123
char * decryptKey = (char *)kvi_malloc(defLen);
123
char * encryptKey = (char *)KviMemory::allocate(defLen);
124
char * decryptKey = (char *)KviMemory::allocate(defLen);
125
126
if(encKeyLen > defLen)encKeyLen = defLen;
126
kvi_memmove(encryptKey,encKey,encKeyLen);
127
if(encKeyLen < defLen)kvi_memset(encryptKey + encKeyLen,'0',defLen - encKeyLen);
127
KviMemory::move(encryptKey,encKey,encKeyLen);
128
if(encKeyLen < defLen)KviMemory::set(encryptKey + encKeyLen,'0',defLen - encKeyLen);
129
130
if(decKeyLen > defLen)decKeyLen = defLen;
130
kvi_memmove(decryptKey,decKey,decKeyLen);
131
if(decKeyLen < defLen)kvi_memset(decryptKey + decKeyLen,'0',defLen - decKeyLen);
131
KviMemory::move(decryptKey,decKey,decKeyLen);
132
if(decKeyLen < defLen)KviMemory::set(decryptKey + decKeyLen,'0',defLen - decKeyLen);
133
134
m_pEncryptCipher = new Rijndael();
134
135
int retVal = m_pEncryptCipher->init(Rijndael::CBC,Rijndael::Encrypt,(unsigned char *)encryptKey,getKeyLenId());
135
kvi_free(encryptKey);
136
KviMemory::free(encryptKey);
136
137
if(retVal != RIJNDAEL_SUCCESS)
138
kvi_free(decryptKey);
139
KviMemory::free(decryptKey);
139
140
delete m_pEncryptCipher;
140
141
m_pEncryptCipher = 0;
141
142
setLastErrorFromRijndaelErrorCode(retVal);
165
case RIJNDAEL_SUCCESS: setLastError(__tr("Error 0: Success ?")); break;
166
case RIJNDAEL_UNSUPPORTED_MODE: setLastError(__tr("Unsupported crypt mode")); break;
167
case RIJNDAEL_UNSUPPORTED_DIRECTION: setLastError(__tr("Unsupported direction")); break;
168
case RIJNDAEL_UNSUPPORTED_KEY_LENGTH: setLastError(__tr("Unsupported key length")); break;
169
case RIJNDAEL_BAD_KEY: setLastError(__tr("Bad key data")); break;
170
case RIJNDAEL_NOT_INITIALIZED: setLastError(__tr("Engine not initialized")); break;
171
case RIJNDAEL_BAD_DIRECTION: setLastError(__tr("Invalid direction for this engine")); break;
172
case RIJNDAEL_CORRUPTED_DATA: setLastError(__tr("Corrupted message data or invalid decrypt key")); break;
173
default: setLastError(__tr("Unknown error")); break;
166
case RIJNDAEL_SUCCESS: setLastError(__tr2qs("Error 0: Success ?")); break;
167
case RIJNDAEL_UNSUPPORTED_MODE: setLastError(__tr2qs("Unsupported crypt mode")); break;
168
case RIJNDAEL_UNSUPPORTED_DIRECTION: setLastError(__tr2qs("Unsupported direction")); break;
169
case RIJNDAEL_UNSUPPORTED_KEY_LENGTH: setLastError(__tr2qs("Unsupported key length")); break;
170
case RIJNDAEL_BAD_KEY: setLastError(__tr2qs("Bad key data")); break;
171
case RIJNDAEL_NOT_INITIALIZED: setLastError(__tr2qs("Engine not initialized")); break;
172
case RIJNDAEL_BAD_DIRECTION: setLastError(__tr2qs("Invalid direction for this engine")); break;
173
case RIJNDAEL_CORRUPTED_DATA: setLastError(__tr2qs("Corrupted message data or invalid decrypt key")); break;
174
default: setLastError(__tr2qs("Unknown error")); break;
177
KviCryptEngine::EncryptResult KviRijndaelEngine::encrypt(const char * plainText,KviStr &outBuffer)
178
KviCryptEngine::EncryptResult KviRijndaelEngine::encrypt(const char * plainText,KviCString &outBuffer)
179
180
if(!m_pEncryptCipher)
181
setLastError(__tr("Ops...encrypt cipher not initialized"));
182
setLastError(__tr2qs("Ops...encrypt cipher not initialized"));
182
183
return KviCryptEngine::EncryptError;
184
185
int len = (int)kvi_strLen(plainText);
185
char * buf = (char *)kvi_malloc(len + 16);
186
char * buf = (char *)KviMemory::allocate(len + 16);
187
188
int retVal = m_pEncryptCipher->padEncrypt((const unsigned char *)plainText,len,(unsigned char *)buf);
191
KviMemory::free(buf);
191
192
setLastErrorFromRijndaelErrorCode(retVal);
192
193
return KviCryptEngine::EncryptError;
195
196
if(!binaryToAscii(buf,retVal,outBuffer))
198
KviMemory::free(buf);
198
199
return KviCryptEngine::EncryptError;
201
KviMemory::free(buf);
202
203
if(outBuffer.len() > maxEncryptLen())
204
205
if(maxEncryptLen() > 0)
206
setLastError(__tr("Data buffer too long"));
207
setLastError(__tr2qs("Data buffer too long"));
207
208
return KviCryptEngine::EncryptError;
210
outBuffer.prepend(KVI_TEXT_CRYPTESCAPE);
211
outBuffer.prepend(KviControlCodes::CryptEscape);
211
212
return KviCryptEngine::Encrypted;
214
KviCryptEngine::DecryptResult KviRijndaelEngine::decrypt(const char * inBuffer,KviStr &plainText)
215
KviCryptEngine::DecryptResult KviRijndaelEngine::decrypt(const char * inBuffer,KviCString &plainText)
216
217
if(!m_pDecryptCipher)
218
setLastError(__tr("Ops...decrypt cipher not initialized"));
219
setLastError(__tr2qs("Ops...decrypt cipher not initialized"));
219
220
return KviCryptEngine::DecryptError;
222
if(*inBuffer != KVI_TEXT_CRYPTESCAPE)
223
if(*inBuffer != KviControlCodes::CryptEscape)
224
225
plainText = inBuffer;
225
226
return KviCryptEngine::DecryptOkWasPlainText;
514
483
//byteswap_buffer((unsigned char *)plain.ptr(),plain.len());
516
unsigned char * out =(unsigned char *)kvi_malloc(plain.len()); // we use this to avoid endiannes problems
485
unsigned char * out =(unsigned char *)KviMemory::allocate(plain.len()); // we use this to avoid endiannes problems
518
487
BlowFish bf((unsigned char *)m_szEncryptKey.ptr(),m_szEncryptKey.len());
520
489
bf.Encrypt((unsigned char *)plain.ptr(),out,plain.len(),BlowFish::ECB);
522
// FIXME: this is probably needed only on LittleEndian machines!
523
byteswap_buffer((unsigned char *)out,plain.len());
525
// da uglybase64 encoding
526
unsigned char * outb = out;
527
unsigned char * oute = out + plain.len();
529
int ll = (plain.len() * 3) / 2;
530
encoded.setLength(ll);
532
unsigned char * p = (unsigned char *)encoded.ptr();
535
quint32 * dd1 = (quint32 *)outb;
537
quint32 * dd2 = (quint32 *)outb;
539
*p++ = fake_base64[*dd2 & 0x3f]; *dd2 >>= 6;
540
*p++ = fake_base64[*dd2 & 0x3f]; *dd2 >>= 6;
541
*p++ = fake_base64[*dd2 & 0x3f]; *dd2 >>= 6;
542
*p++ = fake_base64[*dd2 & 0x3f]; *dd2 >>= 6;
543
*p++ = fake_base64[*dd2 & 0x3f]; *dd2 >>= 6;
544
*p++ = fake_base64[*dd2 & 0x3f];
546
*p++ = fake_base64[*dd1 & 0x3f]; *dd1 >>= 6;
547
*p++ = fake_base64[*dd1 & 0x3f]; *dd1 >>= 6;
548
*p++ = fake_base64[*dd1 & 0x3f]; *dd1 >>= 6;
549
*p++ = fake_base64[*dd1 & 0x3f]; *dd1 >>= 6;
550
*p++ = fake_base64[*dd1 & 0x3f]; *dd1 >>= 6;
551
*p++ = fake_base64[*dd1 & 0x3f];
491
UglyBase64::encode(out, plain.len(), encoded);
492
KviMemory::free(out);
558
bool KviMircryptionEngine::doDecryptECB(KviStr &encoded,KviStr &plain)
496
bool KviMircryptionEngine::doDecryptECB(KviCString &encoded,KviCString &plain)
560
// encoded is in this strange base64...
561
// make sure its length is multiple of 12 (eventually pad with zeroes)
562
if(encoded.len() % 12)
564
int oldL = encoded.len();
565
encoded.setLength(encoded.len() + (12 - (encoded.len() % 12)));
566
char * padB = encoded.ptr() + oldL;
567
char * padE = encoded.ptr() + encoded.len();
568
while(padB < padE)*padB++ = 0;
571
// a fake base64 decoding algo, use a different character set
572
// and stuff 6 bytes at a time into a 32 bit long...
573
int ll = (encoded.len() * 2) / 3;
575
unsigned char * buf = (unsigned char *)kvi_malloc(ll);
576
unsigned char * p = (unsigned char *)encoded.ptr();
577
unsigned char * e = p + encoded.len();
579
unsigned char * bufp = buf;
582
quint32 * dw1 = (quint32 *)bufp;
584
quint32 * dw2 = (quint32 *)bufp;
587
for(i=0;i < 6;i++)*dw2 |= (fake_base64dec(*p++)) << (i * 6);
589
for(i=0;i < 6;i++)*dw1 |= (fake_base64dec(*p++)) << (i * 6);
592
// FIXME: this is probably needed only on LittleEndian machines!
593
byteswap_buffer((unsigned char *)buf,ll);
498
unsigned char * buf=0;
500
UglyBase64::decode(encoded, &buf, &len);
502
plain.setLength(len);
596
503
BlowFish bf((unsigned char *)m_szDecryptKey.ptr(),m_szDecryptKey.len());
598
bf.Decrypt(buf,(unsigned char *)plain.ptr(),ll,BlowFish::ECB);
600
//byteswap_buffer((unsigned char *)plain.ptr(),ll);
505
bf.Decrypt(buf,(unsigned char *)plain.ptr(),len,BlowFish::ECB);
507
KviMemory::free(buf);
607
bool KviMircryptionEngine::doEncryptCBC(KviStr &plain,KviStr &encoded)
512
bool KviMircryptionEngine::doEncryptCBC(KviCString &plain,KviCString &encoded)
609
514
// make sure it is a multiple of 8 bytes (eventually pad with zeroes)
610
515
if(plain.len() % 8)
718
623
// FIXME: Maybe convert this repeated code to a function eh ?
720
625
KviCryptEngineDescription * d = new KviCryptEngineDescription;
721
d->szName = "Rijndael128Hex";
722
d->szAuthor = "Szymon Stefanek";
723
d->szDescription.sprintf(format.ptr(),__tr("hexadecimal"),128);
724
d->iFlags = KVI_CRYPTENGINE_CAN_ENCRYPT | KVI_CRYPTENGINE_CAN_DECRYPT |
725
KVI_CRYPTENGINE_WANT_ENCRYPT_KEY | KVI_CRYPTENGINE_WANT_DECRYPT_KEY;
726
d->allocFunc = allocRijndael128HexEngine;
727
d->deallocFunc = deallocRijndaelCryptEngine;
728
m->registerCryptEngine(d);
730
d = new KviCryptEngineDescription;
731
d->szName = "Rijndael192Hex";
732
d->szAuthor = "Szymon Stefanek";
733
d->szDescription.sprintf(format.ptr(),__tr("hexadecimal"),192);
734
d->iFlags = KVI_CRYPTENGINE_CAN_ENCRYPT | KVI_CRYPTENGINE_CAN_DECRYPT |
735
KVI_CRYPTENGINE_WANT_ENCRYPT_KEY | KVI_CRYPTENGINE_WANT_DECRYPT_KEY;
736
d->allocFunc = allocRijndael192HexEngine;
737
d->deallocFunc = deallocRijndaelCryptEngine;
738
m->registerCryptEngine(d);
740
d = new KviCryptEngineDescription;
741
d->szName = "Rijndael256Hex";
742
d->szAuthor = "Szymon Stefanek";
743
d->szDescription.sprintf(format.ptr(),__tr("hexadecimal"),256);
744
d->iFlags = KVI_CRYPTENGINE_CAN_ENCRYPT | KVI_CRYPTENGINE_CAN_DECRYPT |
745
KVI_CRYPTENGINE_WANT_ENCRYPT_KEY | KVI_CRYPTENGINE_WANT_DECRYPT_KEY;
746
d->allocFunc = allocRijndael256HexEngine;
747
d->deallocFunc = deallocRijndaelCryptEngine;
748
m->registerCryptEngine(d);
750
d = new KviCryptEngineDescription;
751
d->szName = "Rijndael128Base64";
752
d->szAuthor = "Szymon Stefanek";
753
d->szDescription.sprintf(format.ptr(),__tr("base64"),128);
754
d->iFlags = KVI_CRYPTENGINE_CAN_ENCRYPT | KVI_CRYPTENGINE_CAN_DECRYPT |
755
KVI_CRYPTENGINE_WANT_ENCRYPT_KEY | KVI_CRYPTENGINE_WANT_DECRYPT_KEY;
756
d->allocFunc = allocRijndael128Base64Engine;
757
d->deallocFunc = deallocRijndaelCryptEngine;
758
m->registerCryptEngine(d);
760
d = new KviCryptEngineDescription;
761
d->szName = "Rijndael192Base64";
762
d->szAuthor = "Szymon Stefanek";
763
d->szDescription.sprintf(format.ptr(),__tr("base64"),192);
764
d->iFlags = KVI_CRYPTENGINE_CAN_ENCRYPT | KVI_CRYPTENGINE_CAN_DECRYPT |
765
KVI_CRYPTENGINE_WANT_ENCRYPT_KEY | KVI_CRYPTENGINE_WANT_DECRYPT_KEY;
766
d->allocFunc = allocRijndael192Base64Engine;
767
d->deallocFunc = deallocRijndaelCryptEngine;
768
m->registerCryptEngine(d);
770
d = new KviCryptEngineDescription;
771
d->szName = "Rijndael256Base64";
772
d->szAuthor = "Szymon Stefanek";
773
d->szDescription.sprintf(format.ptr(),__tr("base64"),256);
774
d->iFlags = KVI_CRYPTENGINE_CAN_ENCRYPT | KVI_CRYPTENGINE_CAN_DECRYPT |
775
KVI_CRYPTENGINE_WANT_ENCRYPT_KEY | KVI_CRYPTENGINE_WANT_DECRYPT_KEY;
776
d->allocFunc = allocRijndael256Base64Engine;
777
d->deallocFunc = deallocRijndaelCryptEngine;
778
m->registerCryptEngine(d);
781
d = new KviCryptEngineDescription;
782
d->szName = "Mircryption";
783
d->szAuthor = "Szymon Stefanek";
784
d->szDescription = __tr("Popular cryptographic engine based on the\n" \
626
d->m_szName = "Rijndael128Hex";
627
d->m_szAuthor = "Szymon Stefanek";
628
d->m_szDescription = QString(szFormat).arg(__tr2qs("hexadecimal")).arg(128);
629
d->m_iFlags = KviCryptEngine::CanEncrypt | KviCryptEngine::CanDecrypt |
630
KviCryptEngine::WantEncryptKey | KviCryptEngine::WantDecryptKey;
631
d->m_allocFunc = allocRijndael128HexEngine;
632
d->m_deallocFunc = deallocRijndaelCryptEngine;
633
m->registerCryptEngine(d);
635
d = new KviCryptEngineDescription;
636
d->m_szName = "Rijndael192Hex";
637
d->m_szAuthor = "Szymon Stefanek";
638
d->m_szDescription = QString(szFormat).arg(__tr2qs("hexadecimal")).arg(192);
639
d->m_iFlags = KviCryptEngine::CanEncrypt | KviCryptEngine::CanDecrypt |
640
KviCryptEngine::WantEncryptKey | KviCryptEngine::WantDecryptKey;
641
d->m_allocFunc = allocRijndael192HexEngine;
642
d->m_deallocFunc = deallocRijndaelCryptEngine;
643
m->registerCryptEngine(d);
645
d = new KviCryptEngineDescription;
646
d->m_szName = "Rijndael256Hex";
647
d->m_szAuthor = "Szymon Stefanek";
648
d->m_szDescription = QString(szFormat).arg(__tr2qs("hexadecimal")).arg(256);
649
d->m_iFlags = KviCryptEngine::CanEncrypt | KviCryptEngine::CanDecrypt |
650
KviCryptEngine::WantEncryptKey | KviCryptEngine::WantDecryptKey;
651
d->m_allocFunc = allocRijndael256HexEngine;
652
d->m_deallocFunc = deallocRijndaelCryptEngine;
653
m->registerCryptEngine(d);
655
d = new KviCryptEngineDescription;
656
d->m_szName = "Rijndael128Base64";
657
d->m_szAuthor = "Szymon Stefanek";
658
d->m_szDescription = QString(szFormat).arg(__tr2qs("base64")).arg(128);
659
d->m_iFlags = KviCryptEngine::CanEncrypt | KviCryptEngine::CanDecrypt |
660
KviCryptEngine::WantEncryptKey | KviCryptEngine::WantDecryptKey;
661
d->m_allocFunc = allocRijndael128Base64Engine;
662
d->m_deallocFunc = deallocRijndaelCryptEngine;
663
m->registerCryptEngine(d);
665
d = new KviCryptEngineDescription;
666
d->m_szName = "Rijndael192Base64";
667
d->m_szAuthor = "Szymon Stefanek";
668
d->m_szDescription = QString(szFormat).arg(__tr2qs("base64")).arg(192);
669
d->m_iFlags = KviCryptEngine::CanEncrypt | KviCryptEngine::CanDecrypt |
670
KviCryptEngine::WantEncryptKey | KviCryptEngine::WantDecryptKey;
671
d->m_allocFunc = allocRijndael192Base64Engine;
672
d->m_deallocFunc = deallocRijndaelCryptEngine;
673
m->registerCryptEngine(d);
675
d = new KviCryptEngineDescription;
676
d->m_szName = "Rijndael256Base64";
677
d->m_szAuthor = "Szymon Stefanek";
678
d->m_szDescription = QString(szFormat).arg(__tr2qs("base64")).arg(256);
679
d->m_iFlags = KviCryptEngine::CanEncrypt | KviCryptEngine::CanDecrypt |
680
KviCryptEngine::WantEncryptKey | KviCryptEngine::WantDecryptKey;
681
d->m_allocFunc = allocRijndael256Base64Engine;
682
d->m_deallocFunc = deallocRijndaelCryptEngine;
683
m->registerCryptEngine(d);
686
d = new KviCryptEngineDescription;
687
d->m_szName = "Mircryption";
688
d->m_szAuthor = "Szymon Stefanek";
689
d->m_szDescription = __tr2qs("Popular cryptographic engine based on the\n" \
785
690
"old Blowfish encryption algorithm.\n" \
786
691
"The text is first encrypted with Blowfish \n" \
787
692
"and then converted to base64 notation.\n" \