Solved

Xor encrypting decrypting with readable string

Posted on 2014-02-18
4
1,398 Views
Last Modified: 2014-02-22
Hello,
I'am looking for a method to encrypt a string with XOR to a usable string to save this to a file.
Later read the string from file and decrypt to original. Save and read from file is no problem but how to XOR a string to something that has no special characters?
Source is a char array.
0
Comment
Question by:Ingo Foerster
4 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
You've read our opinions about that in yout last thread already, but if you insist on pursuing that way: Why not base64 en- and decoding the XORed result to obtain an ASCII string from that? See http://www.adp-gmbh.ch/cpp/common/base64.html
0
 
LVL 14

Expert Comment

by:Giovanni Heward
Comment Utility
Have a look.

C
#include <stdio.h>
int*p,l;char*k;main(int c,char**v){FILE*f=fopen(*++v,"rb+");k=p=*++v;while(fgets(&l,2,f)){fseek(f,-1,1);putc(l^*k++,f);fflush(f);if(!*k)k=p;}}

Open in new window


C#
using System.IO;class a{static void Main(string[] b){var c=File.ReadAllBytes(b[0]);var d=File.ReadAllBytes(b[1]);for(int e=0;e<c.Length;e++) c[e]^=d[e%d.Length];File.WriteAllBytes(b[0],c);}}

Open in new window

0
 
LVL 32

Accepted Solution

by:
sarabande earned 310 total points
Comment Utility
if your input is ascii only you could xor only 7 bits and after that add 32 to the resulting 7-bit integer. that would move your non-printable encrypted characters with code from 0 to 31 into the printable part of ascii and the upper 32 ascii characters into the lower ansi (8-bit) part (codes 128 - 159) . those letters are all printable beside of 129, 141, 143, 144 and 157. if that is an issue you explicitly would check for those characters and add another 64 to them what would make them printable again.

for decrypting you would do the reverse:

char decrypt(char dec, char xor)
{
    // special handling of unprintable chars which were moved to upper part of ansi
    char special[5] = { char(193), char(205), char(207), char(208), char(221) };
    char enc;
    if (std::find(&special[0], &special[5], dec) != &special[5])
       dec -= char(64);  // specials need to be "re-moved"
    dec -= char(32);     // ansi -> ascii
    enc  = dec ^ xor;    // reverse xor
    return enc;
}

Open in new window


Sara
0
 

Author Closing Comment

by:Ingo Foerster
Comment Utility
Works great. Thank you.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now