Solved

Xor encrypting decrypting with readable string

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

737 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