summaryrefslogtreecommitdiff
authorplurSKI <black.gavin@gmail.com>2010-10-17 12:37:59 (GMT)
committer plurSKI <black.gavin@gmail.com>2010-10-17 12:37:59 (GMT)
commit0f1efbf7748183d78c9ef2d672c72b67d3cd7c52 (patch)
tree79e297d61f44cd4992d11f317b4958618a0d711c
downloadcipherSaber-master.zip
cipherSaber-master.tar.gz
Initial Commitmaster
-rw-r--r--cipherSaber.hs48
-rw-r--r--cipherSaberDecrypt.c77
-rw-r--r--tests/README4
-rw-r--r--tests/cknight.cs1bin0 -> 20473 bytes
-rw-r--r--tests/cstest1.cs11
-rw-r--r--tests/cstest2.cs1bin0 -> 430 bytes
6 files changed, 130 insertions, 0 deletions
diff --git a/cipherSaber.hs b/cipherSaber.hs
new file mode 100644
index 0000000..d73eae7
--- a/dev/null
+++ b/cipherSaber.hs
@@ -0,0 +1,48 @@
+import System.Environment (getArgs)
+import Random
+import Char
+import Array
+import Data.Word
+import Data.Bits
+import Data.ByteString.Internal
+
+-- Handle I/O
+handleArgs function operation key inputFile outputFile = do
+ message <- readFile inputFile
+ writeFile outputFile (function operation message key)
+
+main = mainWith doCypher
+ where mainWith function = do
+ args <- getArgs
+ case args of
+ [operation, key, input, output] -> handleArgs function operation key input output
+ _ -> putStrLn "Usage: cipherSaber operation key inputFile outputFile"
+
+-- Set up the key and message and call mix and crypt
+doCypher "d" msg key = crypt 0 1 0 (mix 0 0 [0..255] (key ++ (take 10 msg))) (drop 10 msg) []
+doCypher "e" msg key = let salt = "A#$hZVv46~"
+ in salt ++ (crypt 0 1 0 (mix 0 0 [0..255] (key ++ salt)) (msg) [])
+doCypher _ msg key = "Invalid Operation Specified: Use either d for decrypt or e for encrypt"
+
+
+-- Mix the state vector with the key
+mix 256 j state key = state
+mix i j state key = let j' = (j + (state !! i) + (ord (key !! (i `mod` (length key))))) `mod` 256
+ in mix (i+1) j' (swap i j' state) key
+
+-- Apply the state vector to the message
+
+crypt byte i j state msg xorArray
+ | byte == length msg = map w2c (zipWith xor (map c2w ( map chr xorArray)) (map c2w msg))
+ | byte /= length msg = let j' = ( j + (state !! i) ) `mod` 256
+ n = (( state !! i ) + (state !! j')) `mod` 256
+ in crypt (byte + 1) ((i+1) `mod` 256) j' (swap i j' state) msg
+ (xorArray ++ [ (swap i j' state) !! n])
+
+-- Array element swapping and indexing
+swap i j x | i == j = x
+ | i < j = realSwap i j x
+ | i > j = realSwap j i x
+realSwap i j x = ((take i x) ++ [x !! j] ++
+ (take (j - i - 1) (drop ( (i + 1) `mod` 256) x)) ++ [x !! i] ++
+ (drop ( (j + 1) `mod` 256) x))
diff --git a/cipherSaberDecrypt.c b/cipherSaberDecrypt.c
new file mode 100644
index 0000000..719ff16
--- a/dev/null
+++ b/cipherSaberDecrypt.c
@@ -0,0 +1,77 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<string.h>
+
+int main(int argc, char** argv)
+{
+ FILE *fp;
+ int keyLen = 0;
+ unsigned long messageLen = 0;
+
+ unsigned char* message;
+ unsigned char* key;
+ unsigned char state[256];
+ unsigned char keyEnd[10];
+
+ int byte = 0;
+ unsigned char i = 0;
+ unsigned char j = 0;
+ unsigned char c = 0;
+
+ // Handle arguments
+ if( argc != 4)
+ {
+ fprintf(stderr, "Usage: %s key inputFile outputFile\n", argv[0]);
+ return -1;
+ }
+ key =(unsigned char *)malloc(strlen(argv[1]) + 10 + 1);
+ memcpy(key, argv[1], strlen(argv[1]) + 1);
+
+ // Open file
+ fp=fopen(argv[2], "rb");
+ if( !fp )
+ {
+ fprintf(stderr, "File Not Found: %s\n", argv[2]);
+ return -1;
+ }
+ fseek(fp, 0, SEEK_END);
+ messageLen=ftell(fp) - 10;
+ rewind(fp);
+ fread((key + strlen(argv[1])), 10, 1, fp);
+ message =(unsigned char *)malloc(messageLen+1);
+ fread(message, messageLen, 1, fp);
+ fclose(fp);
+ keyLen = strlen(argv[1]) + 10;
+
+ // Setup
+ for( byte = 0; byte < 256; byte ++ ) state[byte] = byte;
+
+ // Mix
+ for( byte = 0; byte < 256; byte ++ )
+ {
+ j += state[i] + key[ i % keyLen ];
+ c = state[i];
+ state[i] = state[j];
+ state[j] = c;
+ i ++;
+ }
+ i = 0;
+ j = 0;
+
+ // Cipher
+ for(byte = 0; byte < messageLen; byte ++)
+ {
+ i ++;
+ j += state[i];
+ c = state[i];
+ state[i] = state[j];
+ state[j] = c;
+ c += state[i];
+ message[byte] ^= state[c];
+ }
+
+ // Write output
+ fp=fopen(argv[3], "wb");
+ fwrite(message, messageLen, 1, fp);
+ fclose(fp);
+}
diff --git a/tests/README b/tests/README
new file mode 100644
index 0000000..6c3f769
--- a/dev/null
+++ b/tests/README
@@ -0,0 +1,4 @@
+Passwords:
+ cstest1: asdfg
+ cstest2: SecretMessageforCongress
+ cknight: ThomasJefferson
diff --git a/tests/cknight.cs1 b/tests/cknight.cs1
new file mode 100644
index 0000000..445618f
--- a/dev/null
+++ b/tests/cknight.cs1
Binary files differ
diff --git a/tests/cstest1.cs1 b/tests/cstest1.cs1
new file mode 100644
index 0000000..a9794b6
--- a/dev/null
+++ b/tests/cstest1.cs1
@@ -0,0 +1 @@
+om g0wt縅CVH|OO_ \ No newline at end of file
diff --git a/tests/cstest2.cs1 b/tests/cstest2.cs1
new file mode 100644
index 0000000..31322dc
--- a/dev/null
+++ b/tests/cstest2.cs1
Binary files differ