?
Solved

Xor encrypting decrypting with readable string

Posted on 2014-02-18
4
Medium Priority
?
1,606 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 35

Accepted Solution

by:
sarabande earned 1240 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

771 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