473,382 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Encrypt/Decrypt

I'm looking for a good decrypt/encrypt function, all I want is a function in this form:

char* encrypt(char* normal)
{
...
return encrypted;
}

and

char* decrypt(char* encrypted)
{
...
return normal;
}

I realize there would be memory allocation and I would have to free() or delete [] after
running the functions.
My problem is I don't know the first thing about any encryption method, so actually I'm
searching for already made functions that are similar to the two mentioned above. I have
really tried to use google to find such functions but I failed, I would very much
appreciate if anybody could post a function, an usefull tutorial or a link to some
source...
Jul 19 '05 #1
4 28252
"Victor Bazarov"
Have you tried comp.sources.wanted? No, but will do...
BTW, I went on Google and entered 'encrypt decrypt C++ source'
(without the quotes, of course) and immediately got more than
9000 pages. The very first one had a whole project for AES
(whatever that is) encryption/decryption. I don't believe that
among those 9000 links there weren't _any_ that you could use. I need ine that doesn't require MFC, string classes or additional DLL's and that can
encrypt/decrypt strings, not files...

P.S. And, just to clarify, we don't do homeworks.

I understand and I'm not asking you for this, but as far as the encryption goes it is
really difficult to find a usefull website that doesn't get too complicated...
Jul 19 '05 #2
The openssl library (www.openssl.org) has many
encryption algorithms and is GPL'd (or some similar
licence). It works on Linux/UNIX/*BSD and Win32.

Here is some code that I put together on Linux, it
should run on Win32 with header name changes.
It just wraps openssl's Blowfish encryption with a
bit of housekeeping in IMHO nicer function calls.
Link against libssl.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <openssl/blowfish.h>

/************************************************** ****************
* ARGS:
* keydata == ascii text, the encryption passphrase
* keydatalen == how long keydata is
* in == the data to be encrypted
* out == the encrypted data.
* length(in) == length(out), apparently
* inlen == length of the in array
************************************************** ****************/
void bfencrypt(unsigned char *keydata, int keydatalen, char *in, char
*out, unsigned int inlen) {
BF_KEY key;
unsigned char ivec[32];
int num=0;
// set up for encryption
BF_set_key(&key, keydatalen, keydata);
memset(ivec, '\0', 32);
BF_cfb64_encrypt(in, out, inlen, &key, ivec, &num, BF_ENCRYPT);
}

void bfdecrypt(unsigned char *keydata, int keydatalen, char *in, char
*out, unsigned int inlen) {
BF_KEY key;
unsigned char ivec[32];
int num=0;
// set up for decryption
BF_set_key(&key, keydatalen, keydata);
memset(ivec, '\0', 32);
BF_cfb64_encrypt(in, out, inlen, &key, ivec, &num, BF_DECRYPT);
}

"Spikinsson" <no*@gonna.tell.ya> wrote in message news:<7D******************@afrodite.telenet-ops.be>...
I'm looking for a good decrypt/encrypt function, all I want is a function in this form:

char* encrypt(char* normal)


< ...snip... >
Jul 19 '05 #3
"Spikinsson" <no*@gonna.tell.ya> wrote in message news:<7D******************@afrodite.telenet-ops.be>...
I'm looking for a good decrypt/encrypt function, all I want is a function in this form:

char* encrypt(char* normal)
{
...
return encrypted;
}

and

char* decrypt(char* encrypted)
{
...
return normal;
}

I realize there would be memory allocation and I would have to free() or delete [] after
running the functions.
My problem is I don't know the first thing about any encryption method, so actually I'm
searching for already made functions that are similar to the two mentioned above. I have
really tried to use google to find such functions but I failed, I would very much
appreciate if anybody could post a function, an usefull tutorial or a link to some
source...


What kind of encryption are you looking for? The function could be as
simple as

char* encrypt(const char* plaintext)
{
int len = strlen(plaintext);
char* cyphertext = new char[len+1];

for(int i=0 ; i<len ; ++i)
{
cyphertext[i] = plaintext[i] + 1;
}
cyphertext[len] = 0; // to null-terminate...
return cyphertext;
}

(Of course, this would ideally be done with std::string and such.) To
decrypt simply change the +1 to a -1; you could even pass this in as
an argument and use one function for both encrypting or decrypting.
I'm think that this will wrap around ) right, but I'm not sure. In any
case, if you're just using text, as long as you don't shift it more
than 64 spaces you should be OK.

Here's a slightly more elaborate function (actually two of them, but
ROT13 provides the interface for what you use); it will both encode
and decode, but only letters. (These could be put into a class to keep
them together and the scope nice.)

char rot13letter(char c)
{
if(!isalpha(c)) return c; // non-letters are returned
if(c<='M') return c+13; // A through M maps to N through Z
if(c<='Z') return c-13; // N through Z maps to A through M
if(c<='m') return c+13; // and the same thing for lowercase
if(c<='z') return c-13; // ""
throw SomthingWentHorriblyAndTerriblyWrongException;
}

char* ROT13(const char* plaintext)
{
int len = strlen(plaintext);
char* cyphertext = new char[len+1];

for(int i=0 ; i<len ; ++i)
{
cyphertext[i] = rot13letter(plaintext[i]);
}
cyphertext[len] = 0; // to null-terminate...
return cyphertext;
}
Note also that I'm guessing these would conventionally be implemented
as void functions that operate directly on plaintext rather than
passing out a whole other string. This has severl advantages:
*Often you won't need access to plaintext after you encrypt it, so
reusing that space is preferred (so you don't waste either the space
it would occupy or the allocation time)
*It gives your client function a better idea of what memory is in use,
which makes it less likely that you'll forget to delete[] the return.
*It takes away no flexibility, as the client function can always
duplicate the plaintext string itself before passing it in.
Jul 19 '05 #4

"Evan" <ee****@psu.edu> wrote in message
news:3f**************************@posting.google.c om...
"Spikinsson" <no*@gonna.tell.ya> wrote in message news:<7D******************@afrodite.telenet-ops.be>...
I'm looking for a good decrypt/encrypt function, all I want is a function in this form:
char* encrypt(char* normal)
{
...
return encrypted;
}

and

char* decrypt(char* encrypted)
{
...
return normal;
}

I realize there would be memory allocation and I would have to free() or delete [] after running the functions.
My problem is I don't know the first thing about any encryption method, so actually I'm searching for already made functions that are similar to the two mentioned above. I have really tried to use google to find such functions but I failed, I would very much appreciate if anybody could post a function, an usefull tutorial or a link to some source...


What kind of encryption are you looking for? The function could be as
simple as

char* encrypt(const char* plaintext)
{
int len = strlen(plaintext);
char* cyphertext = new char[len+1];

for(int i=0 ; i<len ; ++i)
{
cyphertext[i] = plaintext[i] + 1;
}
cyphertext[len] = 0; // to null-terminate...
return cyphertext;
}

(Of course, this would ideally be done with std::string and such.) To
decrypt simply change the +1 to a -1; you could even pass this in as
an argument and use one function for both encrypting or decrypting.
I'm think that this will wrap around ) right, but I'm not sure. In any
case, if you're just using text, as long as you don't shift it more
than 64 spaces you should be OK.


Substitution ciphers are evil. Never use them. (26 possible keys, 256 with
ASCII...that's breakabale in about 0,01s on a 386)
Here's a slightly more elaborate function (actually two of them, but
ROT13 provides the interface for what you use); it will both encode
and decode, but only letters. (These could be put into a class to keep
them together and the scope nice.)

char rot13letter(char c)
{
if(!isalpha(c)) return c; // non-letters are returned
if(c<='M') return c+13; // A through M maps to N through Z
if(c<='Z') return c-13; // N through Z maps to A through M
if(c<='m') return c+13; // and the same thing for lowercase
if(c<='z') return c-13; // ""
throw SomthingWentHorriblyAndTerriblyWrongException;
}

char* ROT13(const char* plaintext)
{
int len = strlen(plaintext);
char* cyphertext = new char[len+1];

for(int i=0 ; i<len ; ++i)
{
cyphertext[i] = rot13letter(plaintext[i]);
}
cyphertext[len] = 0; // to null-terminate...
return cyphertext;
}
Note also that I'm guessing these would conventionally be implemented
as void functions that operate directly on plaintext rather than
passing out a whole other string. This has severl advantages:
*Often you won't need access to plaintext after you encrypt it, so
reusing that space is preferred (so you don't waste either the space
it would occupy or the allocation time)
*It gives your client function a better idea of what memory is in use,
which makes it less likely that you'll forget to delete[] the return.
*It takes away no flexibility, as the client function can always
duplicate the plaintext string itself before passing it in.


Check out introduction to cryptography by Miller. It's not very complicated,
yet expect it to be complicated if you want to implement encryption
algoritms. My personal suggestion would be blowfish or DES, as they are
very easy to implement from scratch. You can always use cryptolibs, but if
security is an important issue, you are bound to get into gory maths.

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: wqhdebian | last post by:
As far as I know,when encrypt or decrypt ,a key must first be got,and the key is first generate by a tool or from SecurityRandom,that means I can not generate the same key with the same input.Does...
0
by: Mark Hanford | last post by:
I've been setting up a new MySQL/PHP site which will contain store some CC details, and have been wondering how to pass the keys. CC's are written in a similar way to: INSERT INTO cc (ccName,...
1
by: Benoît | last post by:
Hi, I have generated two keys : "C:>openssl req -nodes -new -x509 -keyout ben.key -out ben.crt -days 3650" I try to encrypt/decrypt a string like "JOHN" with these asymetrics keys. With the...
7
by: Matthias S. | last post by:
Hi, I had a look at the vast information on encryption in the MSDN and got pretty confused. All I want to do is to encrypt a string into an encrypted string and later decrypt that (encrypted)...
4
by: Hrvoje Voda | last post by:
Does anyone knows a good example of how to encrypt/decrypt a string? Hrcko
8
by: Gidi | last post by:
Hi, Is there Buid-In fuction in C# that Encrypt and Decrypt strings? i have a textbox which i'm writing into file, and i want to encrypt it before writing, i'm not looking for something fancy,...
8
by: toupeira23 | last post by:
Hello, I'm trying to encrypt passwords in my app. After discovering that there's no simple function to do this, I wrote a wrapper class which decodes a string using UTF8, encrypts it with...
4
by: Islamegy® | last post by:
I give up.. I tried everything to encrypt querystring and decrypt it back but this never success.. i use RSA encryption. I always get excption when Convert fromBase64String so i tried...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.