Realistic 6 - XECryption

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Realistic 6 - XECryption

Post by a649b9e0 on Tue Sep 09, 2014 9:10 am
([msg=83450]see Realistic 6 - XECryption[/msg])

Finished off realistic 6 last night. I did this as a quick exercise. The key being such a weak point, I beefed it up a little.

It still can be easily broken with multiple cipher texts that use the same key. There are attacks against the underlying encryption I borrowed from as well. So it's nothing super secure by any stretch of the imagination.

Just thought I'd share:

Code: Select all
//
//  Public Domain 2014 - Do what you like, don't blame me.
//
//  Seriously this is nowhere near good encryption, don't treat it as such.
//

using System;
using Cipher;
using System.Text;

// Beefed up a little. Real-ish key handling. 1 <= key.Length <= 256
// It silently trims anything over 256 bytes and returns null on 0.
// Decrypt parsing is kind of picky, make sure you start with a '.' and
// have no whitespace. Also cleaned up the output a little.

public class XECrypt2 {

   private byte[] state;
   private int s1, s2;

   private void InitState(byte[] key) {
      if (key.Length < 1)
         return;
      s1 = 0;
      s2 = 0;
      state = new byte[256];
      int keylength;
      if (key.Length > 256)
         keylength = 256;
      else keylength = key.Length;
      for (int i = 0; i < 256; i++)
         state[i] = (byte) i;
      int j = 0;
      for (int i = 0; i < 256; i++) {
         j = (j + state[i] + key[i % keylength]) % 256;
         int k = state[i];
         state[i] = state[j];
         state[j] = (byte) k;
      }
   }

   private void InitState(string passphrase) {
      InitState(Encoding.ASCII.GetBytes(passphrase));
   }

   private byte NextByte() {
      s1 = (s1 + 1) % 256;
      s2 = (s2 + state[s1]) % 256;
      int k = state[s1];
      state[s1] = state[s2];
      state[s2] = (byte) k;
      return state[(state[s1] + state[s2]) % 256];
   }

   public string Encrypt(string plaintext, byte[] key) {
      if (key.Length < 1)
         return null;
      InitState(key);
      var rnd = new Random();
      var buf = Encoding.ASCII.GetBytes(plaintext);
      var sb = new StringBuilder();
      foreach (byte b in buf) {
         int i = b + NextByte();
         int out1 = (int) ((i / 3) + 10 - (unchecked((uint) rnd.Next()) % 21));
         int out2 = (int) ((i / 3) + 10 - (unchecked((uint) rnd.Next()) % 21));
         int out3 = i - out1 - out2;
         sb.AppendFormat(".{0:d3}.{1:d3}.{2:d3}", out1, out2, out3);
      }
      return sb.ToString();
   }

   public string Encrypt(string plaintext, string key) {
      return Encrypt(plaintext, Encoding.ASCII.GetBytes(key));
   }

   public string Decrypt(string ciphertext, byte[] key) {
      if (key.Length < 1)
         return null;
      ciphertext = ciphertext.Substring(1);
      var chunks = ciphertext.Split('.');
      if (chunks == null)
         return null;
      if ((chunks.Length % 3) != 0)
         return null;
      InitState(key);
      var sb = new StringBuilder();
      for (int i = 0; i < (chunks.Length / 3); i++) {
         int u1 = int.Parse(chunks[i * 3]);
         int u2 = int.Parse(chunks[(i * 3) + 1]);
         int u3 = int.Parse(chunks[(i * 3) + 2]);
         sb.Append((char) (u1 + u2 + u3 - NextByte()));
      }
      return sb.ToString();
   }

   public string Decrypt(string ciphertext, string key) {
      return Decrypt(ciphertext, Encoding.ASCII.GetBytes(key));
   }
}


If you'd like a go at breaking it:

Code: Select all
.055.063.055.095.085.104.068.065.051.083.063.074.042.040.038.033.023.026.115.123.108.090.081.096.127.133.109.057.061.037.073.082.090.028.036.041.093.091.110.086.080.071.066.046.056.039.049.029.066.069.067.025.027.031.047.058.042.028.046.037.112.112.108.083.098.094.067.066.065.062.057.060.036.036.034.063.049.058.064.063.061.045.039.028.107.107.101.127.122.107.066.050.059.063.065.065.055.065.052.077.062.063.035.037.058.108.109.136.121.128.121.109.118.099.111.112.096.081.079.061.029.039.032.064.067.044.073.072.084.091.095.078.094.096.094.077.065.062.116.105.114.100.105.089.106.111.128.041.040.034.039.051.053.051.048.062.073.077.066.105.103.103.112.105.109.048.048.055.093.077.092.123.116.120.049.048.060.060.076.067.051.044.062.045.037.033.096.089.099.036.034.029.068.059.053.045.040.045.053.047.062.059.061.052.116.108.128.051.047.062.112.110.085.061.058.052.088.079.071.109.114.103.092.092.124.064.067.054.073.072.070.032.043.044.004.003.031.075.064.066.086.088.101.036.043.039.107.113.124.071.067.079.054.057.069.033.046.047.075.084.096.091.082.103.124.113.121.044.038.025.071.061.078.108.106.113.010.012.040.046.042.062.060.066.064.040.037.041.070.073.095.098.101.116.055.044.059.055.056.055.042.031.047.094.085.087.055.065.069.102.091.088.081.073.060.075.075.065.072.079.082.060.074.059.126.127.103.028.013.020.080.089.100.101.097.076.115.114.116.063.059.079.078.089.079.065.061.071.104.099.079.093.086.076.119.100.113.071.085.071.123.118.107.093.107.104.053.036.050.088.078.084.050.058.062.070.072.060.016.001.018.117.110.126.100.105.119.119.120.093.074.071.053.073.072.048.061.055.062.053.063.056.070.055.070.043.048.053.107.108.123.058.064.064.044.038.064.107.096.093.035.032.013.116.112.099.098.100.090.114.107.120.051.043.051.074.076.089.112.113.099.063.071.069.053.058.063.095.105.108.086.091.107.051.038.048.128.119.113.049.044.025.046.041.037.043.041.062.036.042.044.052.053.059.085.083.110.112.100.105.060.052.063.060.062.037.067.068.050.067.075.089.089.082.099.069.060.059.057.053.050.029.042.038.115.128.123.109.111.104.063.044.056.090.090.078.052.056.074.110.105.097.093.103.088.054.040.040.110.096.097.064.059.052.034.023.041.075.066.067.050.046.071.099.098.128.043.045.031.108.101.095.088.093.108.076.087.070.077.081.080.045.052.059.033.039.047.070.067.081.022.023.031.085.092.102.061.073.073.094.091.118.052.037.050.053.070.058.035.052.047.100.098.099.027.036.030.058.048.055.040.037.036.043.039.046.039.041.061.123.110.107.041.037.057.096.095.084.025.028.043.046.047.073.097.113.107.061.067.066.027.033.052.075.074.090.075.065.061.055.048.056.061.052.057.068.076.091.052.049.061.091.105.103.025.041.037.072.082.064.011.026.026.075.078.053.088.091.098.112.116.110.106.118.109.087.089.088.113.120.130.118.102.109.103.117.110.038.041.044.045.036.055.086.085.086.041.052.048.065.062.074.042.046.041.083.074.077.094.090.101.069.075.067.038.046.055.093.089.094.040.034.032.012.022.006.102.097.119.089.102.101.055.065.052.072.074.085.065.062.082.092.086.102.054.047.048.057.067.071.109.100.100.114.105.108.039.054.045.093.093.093.101.106.125.110.109.086.073.088.082.051.040.043.098.097.085.103.099.085.047.037.030.086.069.077.069.075.086.049.037.051.075.088.081.101.096.090.042.038.064.087.077.083.078.084.066.088.083.071.033.043.041.100.088.108.059.058.089.042.047.067.090.096.094.066.055.073.055.059.037.052.060.074.069.062.054.123.116.113.069.061.063.041.032.046.098.092.094.038.031.047.095.092.073.064.080.080.079.090.084.040.037.036.069.072.049.053.053.045.111.107.102.080.087.076.117.126.114.058.053.034.059.050.068.035.043.052.053.049.039.047.040.037.067.063.084.105.108.132.097.100.103.050.049.061.051.059.062.112.125.112.075.069.071.103.105.089.039.048.048.078.073.092.014.028.027.047.056.037.037.049.031.053.052.063.043.028.045.085.097.101.102.106.111.096.090.100.061.074.062.060.067.085.043.047.034.094.085.097.063.059.079.078.067.078.079.076.104.048.041.045.064.065.090.062.081.070.046.053.070.057.064.046.106.115.115.042.034.048.071.077.085.122.120.129.079.079.111.048.057.040.048.045.034.043.034.047.121.108.112.040.027.045.053.066.063.072.072.075.115.115.129.089.094.112.079.073.095.083.077.081.059.069.078.040.035.062.046.060.045.068.075.090.099.087.095.031.036.058.124.121.109.032.041.040.045.042.038.089.096.077.068.077.058.075.064.060.061.065.059.084.087.081.081.083.084.113.110.088.060.046.063.083.073.065.121.124.102.075.079.083.070.062.053.059.046.042.108.113.091.058.075.063.077.066.065.097.083.101.062.066.055.102.113.120.061.072.067.100.096.107.063.077.063.101.096.093.076.078.090.067.070.096.091.095.097.120.122.120.102.107.088.034.036.056.096.096.074.059.067.047.117.104.111.064.066.073.109.118.116.082.076.093.091.076.085.062.077.077.031.035.031.066.073.070.087.088.096.104.095.090.053.054.059.082.084.096.062.050.059.110.116.126.095.096.107.095.100.116.076.070.076.093.090.082.121.108.126.088.091.090.054.049.038.073.073.102.059.058.044.040.057.055.020.013.024.051.065.057.044.045.033.069.064.073.063.061.035.089.093.075.104.121.115.054.066.054.031.037.057.054.047.058.085.071.088.062.063.038.091.105.102.124.117.111.044.036.054.056.055.063.051.041.033.043.056.041.080.068.074.071.071.074.082.062.074.015.029.030.046.042.060.088.079.096.081.090.080.113.126.114.052.048.051.060.070.059.069.070.082.070.080.086.109.117.125.052.045.034.064.054.049.103.107.115.074.072.055.102.115.108.094.105.099


-- Tue Sep 09, 2014 7:15 am --

Also, misclicked the subforum... feel free to move. It's still early give me a break ;)
a649b9e0
New User
New User
 
Posts: 3
Joined: Mon Sep 01, 2014 3:22 pm
Blog: View Blog (0)


Re: Realistic 6 - XECryption

Post by cyberdrain on Tue Sep 09, 2014 9:25 am
([msg=83451]see Re: Realistic 6 - XECryption[/msg])

While this could be seen as a spoiler for the mission, I'm actually going to applaud you for trying to improve the algorithm. Well done, this is pretty cool. I'd still rather use AES though, no offense :)
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests