Skip to content

Instantly share code, notes, and snippets.

@SoftwareAssassin
Created August 9, 2012 00:43
Show Gist options
  • Select an option

  • Save SoftwareAssassin/3299991 to your computer and use it in GitHub Desktop.

Select an option

Save SoftwareAssassin/3299991 to your computer and use it in GitHub Desktop.
Fibonacci Encoder/Decoder
//
// Original Author: Software Assassin
//
// GNU All-Permissive License:
// Copying and distribution of this file, with or without modification,
// are permitted in any medium without royalty provided the copyright
// notice and this notice are preserved. This file is offered as-is,
// without any warranty.
//
using System;
using System.Text;
using System.Collections.Generic;
namespace Assassin
{
public static class Fibonacci
{
public static string Decode(string input)
{
StringBuilder retval = new StringBuilder();
int left = (int)input.Substring(0, 1)[0];
int right = (int)input.Substring(input.Length - 1)[0];
int together = System.Convert.ToInt32(String.Format("{0}{1}"
, (left - 32).ToString()
, (right - 32).ToString()
));
int[] fibonacciVals = Fibonacci.GetFibonacciValues(together);
for (int i = 0; i < together; i++)
{
int pad = i + 5;
retval.Append(input.Substring(fibonacciVals[pad] + 1, 1));
}
return retval.ToString();
}
public static string Encode(string input)
{
StringBuilder retval = new StringBuilder();
//get info
int length = input.Length;
//cache finobacci sequence
int[] fibonacciVals = Fibonacci.GetFibonacciValues(length);
//generate hashed value
Random r = new Random();
retval.Append(char.ConvertFromUtf32((length / 10) + 32));
retval.Append(char.ConvertFromUtf32(r.Next(32, 127)));
retval.Append(char.ConvertFromUtf32(r.Next(32, 127)));
retval.Append(char.ConvertFromUtf32(r.Next(32, 127)));
retval.Append(char.ConvertFromUtf32(r.Next(32, 127)));
for (int i = 0; i < length; i++)
{
int pad = i + 5;
retval.Append(input.Substring(i, 1));
for (int j = fibonacciVals[pad] + 1; j <= fibonacciVals[pad + 1] - 1; j++)
retval.Append(char.ConvertFromUtf32(r.Next(32, 127)));
}
retval.Append(char.ConvertFromUtf32((length % 10) + 32));
return retval.ToString();
}
//helper methods
public static int[] GetFibonacciValues(int length)
{
int colLength = length + 7;
int[] fibonacciVals = new int[colLength];
for (int i = 0; i < 3; i++)
fibonacciVals[i] = Fibonacci.fib(i);
for (int i = 3; i < colLength; i++)
fibonacciVals[i] = fibonacciVals[i - 1] + fibonacciVals[i - 2];
return fibonacciVals;
}
public static int fib(int num)
{
if (num <= 1)
return num;
else
return fib(num - 1) + fib(num - 2);
}
}
}
@SoftwareAssassin
Copy link
Copy Markdown
Author

Use static Encode/Decode methods to parse strings. Class uses Fibonacci sequence to render strings unreadable to human eye.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment