linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Huber, George K CECOM RDEC STCD SRI" <George.K.Huber@us.army.mil>
To: 'Fabio Miranda Hamburger' <fabmirha@ns.isi.ulatina.ac.cr>,
	linux-c-programming@vger.kernel.org
Subject: RE: Basic C encryption
Date: Tue, 8 Jul 2003 13:48:14 -0400	[thread overview]
Message-ID: <DDFD9B60F648D411AD670000F80822EA03FCAAB6@mail7.monmouth.army.mil> (raw)

Just add one.

For example, assume that there is a function (ReadByte()) that reads
a single byte from a file and returns it as a char.  Also assume that 
there is a function (WriteByte(char)) that writes a single character
to an output file.  With these two functions, you can write your
encryption routine as:

unsigned char  ch;

while(!EOF)
{
   ch = ReadByte();
   int    nVal = (int)ch + 1;
   if(nVal > 255) nVal = 0;
   WriteByte(char(nVal));
}

So, for example if we read the character 'Z' from the input file which
has the ACSII value of 90, after adding one we would have the value 
91, which is character [.

The reason for the test is that in reading a binary file it is possible 
to return 0xFF as a possible value, which on adding on would yield 0x100
which would not fit into a single-byte quanity.  The decryption routine
would be as simple,

unsigned char  ch;

while(!EOF)
{
    ch = ReadByte();
    int   nVal = (int)ch - 1;
    if(nVal < 0) nVal = 255;
    WriteByte(char(nVal));
}



As others have pointed out, a simple subsitituion cypher is very easy
to break.  With a few additional lines of code, you can get a stronger
encryption.  The following example assumes that the values in the 
input file are between 0 and 127 (i.e. the normal ASCII values).  It is
left as an exercise for the reader to figure out how to modify this 
for the case when the values are between 0 and 255.

char*    lpKey = "This is a key"
char     ch;
int      nIdx = 0;


while(!EOF)
{
    ch = ReadByte();
    if(nIdx >= strlen(lpKey)) nIdx = 0;
    ch = (int)ch + int(lpKey[nIdx++]);
    WriteByte(ch);
}

George Huber




-----Original Message-----
From: Fabio Miranda Hamburger [mailto:fabmirha@ns.isi.ulatina.ac.cr]
Sent: Tuesday, July 08, 2003 10:13 AM
To: linux-c-programming@vger.kernel.org
Subject: Basic C encryption


Hi, I need a small, basic way to encrypt a binary file saving a custom
class from a windoze 32 proyect.

For example, add +1 to each character that is about to be saved in the
disk. How would that be?

---
Fabio Andres Miranda
Ingenieria de sistemas informaticos
Universidad Latina - Costa Rica


-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

             reply	other threads:[~2003-07-08 17:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-08 17:48 Huber, George K CECOM RDEC STCD SRI [this message]
2003-07-08 18:52 ` Basic C encryption Jan-Benedict Glaw
  -- strict thread matches above, loose matches on Subject: below --
2003-07-08 20:03 Huber, George K CECOM RDEC STCD SRI
2003-07-08 20:09 ` Jan-Benedict Glaw
2003-07-08 14:13 Fabio Miranda Hamburger
2003-07-08 14:41 ` Jan-Benedict Glaw
2003-07-08 14:33   ` Fabio Miranda Hamburger
2003-07-08 14:48     ` Jan-Benedict Glaw
2003-07-08 15:08 ` Glynn Clements
2003-07-08 15:26   ` Fabio Miranda Hamburger
2003-07-08 16:01     ` Jan-Benedict Glaw

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DDFD9B60F648D411AD670000F80822EA03FCAAB6@mail7.monmouth.army.mil \
    --to=george.k.huber@us.army.mil \
    --cc=fabmirha@ns.isi.ulatina.ac.cr \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).