Solved

Xor encrypting decrypting with readable string

Posted on 2014-02-18
4
1,452 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
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 14

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 33

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 75
xamarin c# deserialize Json containing nested object 2 91
Arduino EDI - Programming language 3 94
outlook office 365 8 121
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

776 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