/* Written by target0 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include #include #define base64_encode(a,b,c,d) base64_stuff(a,b,c,d,0) #define base64_decode(a,b,c,d) base64_stuff(a,b,c,d,1) int blowfish_encrypt (char *intext, char *outbuf) { int outlen, tmplen; /* la clé et le IV, évidemment il ne faut pas les laisser comme ca dans une implémentation plus sérieuse.. je suggère de faire un random IV avec les fonctions random(3) et srandomdev(3) et de laisser la clé au choix de l'utilisateur */ unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; unsigned char iv[] = {1,2,3,4,5,6,7,8}; EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX_init(&ctx); EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv); if (!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext))) return -1; if (!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) return -1; outlen += tmplen; EVP_CIPHER_CTX_cleanup(&ctx); return outlen; } int blowfish_decrypt (char *intext, char *outbuf) { int outlen, tmplen; /* même commentaire que pour blowfish_encrypt */ unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; unsigned char iv[] = {1,2,3,4,5,6,7,8}; EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX_init(&ctx); EVP_DecryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv); if (!EVP_DecryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext))) return -1; if (!EVP_DecryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) return -1; outlen += tmplen; EVP_CIPHER_CTX_cleanup(&ctx); return outlen; } void base64_stuff (char *msg, int len, char *result, int *size, int what) { gnutls_datum_t data; data.data = msg; data.size = len; if (what) gnutls_srp_base64_decode(&data, result, size); else gnutls_srp_base64_encode(&data, result, size); } int main() { char blow[512]; char base[512]; int len,size = 511; bzero(blow,512); bzero(base,512); len = blowfish_encrypt("hello", blow); base64_encode(blow, len, base, &size); printf("%s\n",base); char decbase[512]; char decblow[512]; bzero(decbase,512); bzero(decblow,512); base64_decode(base, strlen(base), decbase, &size); len = blowfish_decrypt(decbase, decblow); printf("%s\n",decblow); return 0; }