Solved

Xor encrypting decrypting with readable string

Posted on 2014-02-18
4
1,560 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39869290
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 15

Expert Comment

by:Giovanni Heward
ID: 39869425
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 34

Accepted Solution

by:
sarabande earned 310 total points
ID: 39870027
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
ID: 39879134
Works great. Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

728 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