Skip to content

Instantly share code, notes, and snippets.

@codelance
Created December 2, 2012 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save codelance/4186459 to your computer and use it in GitHub Desktop.
Save codelance/4186459 to your computer and use it in GitHub Desktop.
Substitution Cipher
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class SubstitutionCipher {
private SubstitutionCipher(){ init(); }
private void init()
{
}
public static String encrypt(String key, String plaintext)
{
String ciphertext = "";
String[] keySpace = new String[key.length()];
for(int i = 0; i < key.length(); i++) keySpace[i] = String.valueOf(key.charAt(i));
for(int i = 0; i < plaintext.length(); i++)
{
int index = plaintext.charAt(i) - 65;
if( index > keySpace.length || index < 0)
{
ciphertext += String.valueOf(plaintext.charAt(i));
}
else
{
ciphertext += keySpace[index];
}
}
return ciphertext;
}
public static String decrypt(String key, String ciphertext)
{
String plaintext = "";
for(int i = 0; i < ciphertext.length(); i++)
{
char character = ciphertext.charAt(i);
int index = key.indexOf(character);
int ascii = index + 65;
if( ascii < 65 || ascii > 90)
{
plaintext += String.valueOf(character);
}
else
{
plaintext += String.valueOf((char)ascii);
}
}
return plaintext;
}
public static void main(String[] args) {
String operation = null, key = null, input = null, output = null;
if (args.length == 4) {
if( (operation = args[0]) == "e" ||
(operation = args[0]) == "d")
{
System.err.println("Operation Argument"
+ " must be e for encrypt or d for decrypt");
System.exit(1);
}
key = args[1];
if( !( new File(key) ).exists() )
{
System.err.println("Key Argument"
+ " key file must exist");
System.exit(1);
}
input = args[2];
if( !( new File(input) ).exists())
{
System.err.println("Input Argument"
+ " input file must exist");
System.exit(1);
}
output = args[3];
if( ( new File(output) ) == null )
{
System.err.println("Output Argument"
+ " output file already exists");
System.exit(1);
}
}
else
{
System.err.println("");
System.exit(1);
}
StringBuffer keyString = null, inputString = null, outputString;
try{
/*
* Read in the Key
*/
int ch = -1;
keyString = new StringBuffer("");
FileInputStream keyfis = new FileInputStream(new File(key));
while( (ch = keyfis.read()) != -1) keyString.append((char) ch);
keyfis.close();
/*
* Read in the input
*/
inputString = new StringBuffer("");
FileInputStream inputfis = new FileInputStream(new File(input));
while( (ch = inputfis.read()) != -1) inputString.append((char) ch);
inputfis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String result = "";
int case_op = (operation.equals("e") ? 1 : 2);
switch( case_op )
{
case 1:
{
String ciphertext = result = SubstitutionCipher.encrypt(keyString.toString(), inputString.toString());
System.out.println("Key: "+ keyString);
System.out.println("PlainText: "+ inputString);
System.out.println("CipherText: "+ ciphertext);
break;
}
case 2:
{
String plaintext = result = SubstitutionCipher.decrypt(keyString.toString(), inputString.toString());
System.out.println("Key: "+ keyString);
System.out.println("CipherText: "+ inputString);
System.out.println("PlainText: "+ plaintext);
break;
}
}
/*
* Write in the output
*/
try {
FileOutputStream outputfis = new FileOutputStream(new File(output));
outputfis.write(result.getBytes());
outputfis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment