Skip to content

Instantly share code, notes, and snippets.

@gaxar77
Created April 11, 2010 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaxar77/362397 to your computer and use it in GitHub Desktop.
Save gaxar77/362397 to your computer and use it in GitHub Desktop.
397 Description: This is a simple decoder program I wrote (very buggy though; certainly not perfect) that allows you to input encoded plain text according to my garf method, and reveals a sequences of numbers. If the text is actually encoded, it wil
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string ValuesToString(int[] values)
{
string nums = "";
for (int i = 0; i < values.Length; i++)
{
nums += values[i].ToString() + " ";
}
return nums;
}
private void theText_TextChanged(object sender, EventArgs e)
{
PrimaryStegLayer layer = new PrimaryStegLayer(theText.Text, 3);
int[] values = layer.Values;
string nums = "";
nums += ValuesToString(values) + "\r\n\r\n";
SecondaryStegLayer layer2 = new SecondaryStegLayer(layer);
nums += ValuesToString(layer2.Values) + "\r\n\r\n";
ThirdStegLayer layer3 = new ThirdStegLayer(layer2, false);
nums += ValuesToString(layer3.Values) + "\r\n\r\n";
GarfScanner stuff = new GarfScanner(layer3.Values);
nums += ValuesToString(stuff.Values) + "\r\n\r\n";
theCode.Text = nums;
}
private void Form1_Load(object sender, EventArgs e)
{
Assembly a = Assembly.GetExecutingAssembly();
Stream s = a.GetManifestResourceStream("WindowsFormsApplication1.Key.txt");
StreamReader sr = new StreamReader(s);
theKey.Text = sr.ReadToEnd();
sr.Close();
s.Close();
}
}
}
namespace WindowsFormsApplication1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.theText = new System.Windows.Forms.RichTextBox();
this.theCode = new System.Windows.Forms.RichTextBox();
this.theKey = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// theText
//
this.theText.Location = new System.Drawing.Point(195, 7);
this.theText.Name = "theText";
this.theText.Size = new System.Drawing.Size(442, 96);
this.theText.TabIndex = 0;
this.theText.Text = "";
this.theText.TextChanged += new System.EventHandler(this.theText_TextChanged);
//
// theCode
//
this.theCode.Location = new System.Drawing.Point(195, 109);
this.theCode.Name = "theCode";
this.theCode.Size = new System.Drawing.Size(442, 137);
this.theCode.TabIndex = 1;
this.theCode.Text = "";
//
// theKey
//
this.theKey.Location = new System.Drawing.Point(12, 12);
this.theKey.Name = "theKey";
this.theKey.ReadOnly = true;
this.theKey.Size = new System.Drawing.Size(177, 234);
this.theKey.TabIndex = 2;
this.theKey.Text = "";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(644, 252);
this.Controls.Add(this.theKey);
this.Controls.Add(this.theCode);
this.Controls.Add(this.theText);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.RichTextBox theText;
private System.Windows.Forms.RichTextBox theCode;
private System.Windows.Forms.RichTextBox theKey;
}
}
A = 0
B = 1
C = 2
D = 3
E = 0
F = 1
G = 2
H = 3
I = 0
J = 1
K = 2
L = 3
M = 0
N = 1
O = 2
P = 3
Q = 0
R = 1
S = 2
T = 3
U = 0
V = 1
W = 2
X = 3
Y = 0
Z = 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
class StegCoding
{
}
class PrimaryStegLayer : List<int>
{
private int[] values = null;
public PrimaryStegLayer(string codedString, int max)
{
string str = codedString.ToLower();
string[] words = str.Split(new String[] {" ", ",", "?", ".", "!"},
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < words.Length; i++)
{
byte firstByteValue = (byte) words[i][0];
byte alphabetStart = (byte)"a"[0];
int value = (firstByteValue - alphabetStart) % (max + 1);
this.Add(value);
}
values = this.ToArray();
}
public int[] Values
{
get { return values; }
}
}
class SecondaryStegLayer : List<int>
{
public SecondaryStegLayer(int[] primaryValues)
{
ConstructFromArray(primaryValues);
}
public SecondaryStegLayer(PrimaryStegLayer primaryStegLayer)
{
ConstructFromArray(primaryStegLayer.Values);
}
private int[] values = null;
private void ConstructFromArray(int[] primaryValues)
{
int pl = primaryValues.Length;
for (int i = 0; i < pl / 2 + pl % 2; i++)
{
int ci = 2 * i;
if (ci + 1 < pl)
{
this.Add(primaryValues[ci] + primaryValues[ci+1]);
}
else
{
this.Add(primaryValues[ci]);
}
}
values = this.ToArray();
}
public int[] Values
{
get
{
return values;
}
}
}
class ThirdStegLayer : List<int>
{
private int[] values = null;
public ThirdStegLayer(int[] secondaryValues, bool signDifferences)
{
ConstructFromArray(secondaryValues, signDifferences);
}
public ThirdStegLayer(SecondaryStegLayer secondaryStegLayer, bool signDifferences)
{
ConstructFromArray(secondaryStegLayer.Values, signDifferences);
}
private void ConstructFromArray(int[] secondaryValues, bool signDifferences)
{
int vl = secondaryValues.Length;
for (int i = 1; i < vl; i++)
{
int v = secondaryValues[i - 1] - secondaryValues[i];
if (!signDifferences) v = Math.Abs(v);
this.Add(v);
}
values = this.ToArray();
}
public int[] Values
{
get
{
return values;
}
}
}
class GarfScanner : List<int>
{
int index = 0;
int[] _sequence = null;
int[] values = null;
public GarfScanner(int[] sequence)
{
_sequence = new int[sequence.Length];
sequence.CopyTo(_sequence, 0);
while (SegmentsLeft())
{
int sv = Math.Abs(ProcessSegment());
this.Add(sv);
}
values = this.ToArray();
}
public int[] Values
{
get
{
return values;
}
}
private bool SegmentsLeft()
{
return index < _sequence.Length;
}
private int ProcessSegment()
{
int[] segmentNums = ReadSegment();
int l = segmentNums.Length;
if (l == 0) return 0;
if (l == 1) return segmentNums[0];
if (l == 2) return segmentNums[0] + segmentNums[1];
int result = 1;
int mode = segmentNums[0];
for (int i = 1; i < l - 1; i++)
{
result *= segmentNums[i];
}
switch (mode)
{
case 1:
result *= segmentNums[l-1];
break;
case 2:
result += segmentNums[l-1];
break;
case 3:
result -= segmentNums[l-1];
break;
}
return result;
}
private int[] ReadSegment()
{
int l = _sequence.Length;
int i = 0;
List<int> segmentNums = new List<int>();
while (_sequence[index + i] != 0)
{
int num = _sequence[index + i];
segmentNums.Add(num);
i++;
if (index + i == l) break;
}
i++;
index += i;
return segmentNums.ToArray();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment