Skip to main content
gamedev.net
🔒 Locked

encryption algorithm

Started by Lode 2 replies 500+ views
Original Post
Lode
Lode
It's true that it's never a good idea to write your own encryption algorithm, because there are so much possible vulnerabilities, that it's always best to use existing and proven algorithms. But I tried to write something anyway. It's made to encrypt and decrypt text documents, with simple passwords. I don't think there's something like a key length in this algorithm, unless the length of the password, but it can be anything ranging from 1 to fiftythousandswiftyfive. There are two passes. In pass 1, it'll go forward. It'll xor the bytes depending on characters at earlier position and password characters. In pass 2 it does the same, but then it goes from the end to the beginning, so that in the resulting document, the value of the first character depends on ALL the values of other characters that were in the original file. I'm wondering if someone who knows something about this can see why this would be a bad algorithm that can easily be cracked and what attacks could exist on it. And, do there exist similar encryption tools out there?

/*
Copyright (c) 2007 by Lode Vandevenne. All rights reserved.
*/

#include <vector>
#include <fstream>
#include <iostream>

//write given buffer to the file, overwriting the file, it doesn't append to it.
void saveFile(const std::string& filename, const std::string& buffer)
{
  std::ofstream file(filename.c_str(), std::ios::out|std::ios::binary);
  file.write((char*)&buffer[0], buffer.size());
  file.close();
}

void loadFile(const std::string& filename, std::string& buffer)
{
  std::ifstream file(filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate);

  //get filesize
  file.seekg(0, std::ios::end);
  int size = file.tellg();
  file.seekg(0, std::ios::beg);
  size -= file.tellg();

  //read contents of the file into the vector
  buffer.resize(size);
  file.read((char*)(&buffer[0]), size);
  file.close();
}

void crypt(std::string& out, const std::string& in, const std::string password, bool encrypt)
{
  out.resize(in.size());
  std::string temp;
  temp.resize(in.size());
  unsigned long memory = 0; //it's 32-bit and not 8-bit because it determines the position of earlier characters chosen
  
  for(size_t i = 0; i < in.size(); i++)
  {
    temp[in.size() - 1 - i] = in ^ (memory % 256);
    unsigned char byte = encrypt ? temp[in.size() - 1 - (memory % (i + 1))] : in[memory % (i + 1)];
    unsigned char pass = password[memory % password.size()];
    memory = (memory << 5) ^ byte ^ pass;
  }

  memory = 0;
  
  for(size_t i = 0; i < in.size(); i++)
  {
    out = temp ^ (memory % 256);
    unsigned char byte = encrypt ? out[(memory % (i + 1))] : temp[memory % (i + 1)];
    unsigned char pass = password[memory % password.size()];
    memory = (memory << 5) ^ byte ^ pass;
  }
}

int main(int argc, char *argv[])
{
  if(argc < 4) { std::cout << "please provide e or d, a filename and a password\n"; return 0; }
  
  char ed = argv[1][0];
  if(ed != 'e' && ed != 'd') { std::cout << "first argument must be e for encrypt or d for decrypt\n"; return 0; }
  
  std::string password = argv[3];
  
  std::string in, out;
  loadFile(argv[2], in);
  
  //first pass
  crypt(out, in, password, ed == 'e');

  saveFile(argv[2], out);
  return 0;
}







Sneftel
Sneftel
It's vulnerable to a "known plaintext attack", where if you know what the plaintext version of a message is you can figure out the password.
Lode
Lode
What are solutions to the known plaintext attack? Because if you know the plaintext and the encrypted text, can't one always find out the password?
Sneftel
Sneftel
Quote:
Original post by Lode
Because if you know the plaintext and the encrypted text, can't one always find out the password?

Nope. With properly written modern cryptosystems, knowing the plaintext and the encrypted text doesn't give you any information about the key, or about other encrypted messages. (Which is good, because with public-key encryption, you can encrypt any plaintext you want.)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.