Update some classes in Ciphers package
This commit is contained in:
parent
7858187d59
commit
b159835878
@ -55,7 +55,9 @@ public class AES {
|
|||||||
0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
|
0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Inverse Rijndael S-box Substitution table used for decryption in the subBytesDec step. */
|
/**
|
||||||
|
* Inverse Rijndael S-box Substitution table used for decryption in the subBytesDec step.
|
||||||
|
*/
|
||||||
private static final int[] INVERSE_SBOX = {
|
private static final int[] INVERSE_SBOX = {
|
||||||
0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
|
0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
|
||||||
0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
|
0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
|
||||||
|
@ -1,13 +1,8 @@
|
|||||||
package Ciphers;
|
package Ciphers;
|
||||||
|
|
||||||
|
import javax.crypto.*;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import javax.crypto.BadPaddingException;
|
|
||||||
import javax.crypto.Cipher;
|
|
||||||
import javax.crypto.IllegalBlockSizeException;
|
|
||||||
import javax.crypto.KeyGenerator;
|
|
||||||
import javax.crypto.NoSuchPaddingException;
|
|
||||||
import javax.crypto.SecretKey;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This example program shows how AES encryption and decryption can be done in Java. Please note
|
* This example program shows how AES encryption and decryption can be done in Java. Please note
|
||||||
@ -17,6 +12,7 @@ import javax.crypto.SecretKey;
|
|||||||
public class AESEncryption {
|
public class AESEncryption {
|
||||||
|
|
||||||
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
|
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 1. Generate a plain text for encryption 2. Get a secret key (printed in hexadecimal form). In
|
* 1. Generate a plain text for encryption 2. Get a secret key (printed in hexadecimal form). In
|
||||||
* actual use this must by encrypted and kept safe. The same key is required for decryption.
|
* actual use this must by encrypted and kept safe. The same key is required for decryption.
|
||||||
|
@ -1,36 +1,23 @@
|
|||||||
//The ‘key’ for the Affine cipher consists of 2 numbers, we’ll call them a and b.
|
|
||||||
// The following discussion assumes the use of a 26 character alphabet (m = 26).
|
|
||||||
// a should be chosen to be relatively prime to m (i.e. a should have no factors in common with m).
|
|
||||||
|
|
||||||
package Ciphers;
|
package Ciphers;
|
||||||
|
|
||||||
import java.util.Scanner;
|
class AffineCipher {
|
||||||
|
|
||||||
class AffineCipher
|
// Key values of a and b
|
||||||
{
|
static int a = 17;
|
||||||
static Scanner in = new Scanner(System.in);
|
static int b = 20;
|
||||||
|
|
||||||
static String encryptMessage(char[] msg)
|
static String encryptMessage(char[] msg) {
|
||||||
{
|
/// Cipher Text initially empty
|
||||||
System.out.println("Enter key value a for encryption : ");
|
|
||||||
int a = in.nextInt();
|
|
||||||
|
|
||||||
System.out.println("Enter key value b for encryption : ");
|
|
||||||
int b = in.nextInt();
|
|
||||||
|
|
||||||
/// Initially empty cipher String
|
|
||||||
String cipher = "";
|
String cipher = "";
|
||||||
for (int i = 0; i < msg.length; i++)
|
for (int i = 0; i < msg.length; i++) {
|
||||||
{
|
|
||||||
// Avoid space to be encrypted
|
// Avoid space to be encrypted
|
||||||
/* applying encryption formula ( a x + b ) mod m
|
/* applying encryption formula ( a x + b ) mod m
|
||||||
{here x is msg[i] and m is 26} and added 'A' to
|
{here x is msg[i] and m is 26} and added 'A' to
|
||||||
bring it in range of ascii alphabet[ 65-90 | A-Z ] */
|
bring it in range of ascii alphabet[ 65-90 | A-Z ] */
|
||||||
if (msg[i] != ' ')
|
if (msg[i] != ' ') {
|
||||||
{
|
|
||||||
cipher = cipher
|
cipher = cipher
|
||||||
+ (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
|
+ (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
|
||||||
} else // append space character
|
} else // else simply append space character
|
||||||
{
|
{
|
||||||
cipher += msg[i];
|
cipher += msg[i];
|
||||||
}
|
}
|
||||||
@ -38,42 +25,30 @@ class AffineCipher
|
|||||||
return cipher;
|
return cipher;
|
||||||
}
|
}
|
||||||
|
|
||||||
static String decryptCipher(String cipher)
|
static String decryptCipher(String cipher) {
|
||||||
{
|
|
||||||
System.out.println("Enter key value a for decryption : ");
|
|
||||||
int a = in.nextInt();
|
|
||||||
|
|
||||||
System.out.println("Enter key value b for decryption : ");
|
|
||||||
int b = in.nextInt();
|
|
||||||
|
|
||||||
String msg = "";
|
String msg = "";
|
||||||
int a_inv = 0;
|
int a_inv = 0;
|
||||||
int flag = 0;
|
int flag = 0;
|
||||||
|
|
||||||
//Find a^-1 (the multiplicative inverse of a
|
//Find a^-1 (the multiplicative inverse of a
|
||||||
//in the group of integers modulo m.)
|
//in the group of integers modulo m.)
|
||||||
for (int i = 0; i < 26; i++)
|
for (int i = 0; i < 26; i++) {
|
||||||
{
|
|
||||||
flag = (a * i) % 26;
|
flag = (a * i) % 26;
|
||||||
|
|
||||||
// Check if (a*i)%26 == 1,
|
// Check if (a*i)%26 == 1,
|
||||||
// if so, then i will be the multiplicative inverse of a
|
// then i will be the multiplicative inverse of a
|
||||||
if (flag == 1)
|
if (flag == 1) {
|
||||||
{
|
|
||||||
a_inv = i;
|
a_inv = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < cipher.length(); i++)
|
for (int i = 0; i < cipher.length(); i++) {
|
||||||
{
|
|
||||||
/*Applying decryption formula a^-1 ( x - b ) mod m
|
/*Applying decryption formula a^-1 ( x - b ) mod m
|
||||||
{here x is cipher[i] and m is 26} and added 'A'
|
{here x is cipher[i] and m is 26} and added 'A'
|
||||||
to bring it in range of ASCII alphabet[ 65-90 | A-Z ] */
|
to bring it in range of ASCII alphabet[ 65-90 | A-Z ] */
|
||||||
if (cipher.charAt(i) != ' ')
|
if (cipher.charAt(i) != ' ') {
|
||||||
{
|
|
||||||
msg = msg + (char) (((a_inv *
|
msg = msg + (char) (((a_inv *
|
||||||
((cipher.charAt(i) + 'A' - b)) % 26)) + 'A');
|
((cipher.charAt(i) + 'A' - b)) % 26)) + 'A');
|
||||||
}
|
} else //else simply append space character
|
||||||
else // append space character
|
|
||||||
{
|
{
|
||||||
msg += cipher.charAt(i);
|
msg += cipher.charAt(i);
|
||||||
}
|
}
|
||||||
@ -82,17 +57,17 @@ class AffineCipher
|
|||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main method
|
// Driver code
|
||||||
public static void main(String[] args)
|
public static void main(String[] args) {
|
||||||
{
|
|
||||||
String msg = "AFFINE CIPHER";
|
String msg = "AFFINE CIPHER";
|
||||||
|
|
||||||
// Encrypting message
|
// Calling encryption function
|
||||||
String cipherText = encryptMessage(msg.toCharArray());
|
String cipherText = encryptMessage(msg.toCharArray());
|
||||||
System.out.println("Encrypted Message is : " + cipherText);
|
System.out.println("Encrypted Message is : " + cipherText);
|
||||||
|
|
||||||
// Decrypting message
|
// Calling Decryption function
|
||||||
System.out.println("Decrypted Message is: " + decryptCipher(cipherText));
|
System.out.println("Decrypted Message is: " + decryptCipher(cipherText));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,7 +114,9 @@ public class ColumnarTranspositionCipher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return charValues */
|
/**
|
||||||
|
* @return charValues
|
||||||
|
*/
|
||||||
private static Object[] findElements() {
|
private static Object[] findElements() {
|
||||||
Object[] charValues = new Object[keyword.length()];
|
Object[] charValues = new Object[keyword.length()];
|
||||||
for (int i = 0; i < charValues.length; i++) {
|
for (int i = 0; i < charValues.length; i++) {
|
||||||
|
@ -1,169 +1,162 @@
|
|||||||
package Ciphers;
|
package Ciphers;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Scanner;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Java Implementation of Hill Cipher
|
* Java Implementation of Hill Cipher
|
||||||
* Hill cipher is a polyalphabetic substitution cipher. Each letter is represented by a number belonging to the set Z26 where A=0 , B=1, ..... Z=25.
|
* Hill cipher is a polyalphabetic substitution cipher. Each letter is represented by a number belonging to the set Z26 where A=0 , B=1, ..... Z=25.
|
||||||
* To encrypt a message, each block of n letters (since matrix size is n x n) is multiplied by an invertible n × n matrix, against modulus 26.
|
* To encrypt a message, each block of n letters (since matrix size is n x n) is multiplied by an invertible n × n matrix, against modulus 26.
|
||||||
* To decrypt the message, each block is multiplied by the inverse of the matrix used for encryption.
|
* To decrypt the message, each block is multiplied by the inverse of the matrix used for encryption.
|
||||||
* The cipher key and plaintext/ciphertext are user inputs.
|
* The cipher key and plaintext/ciphertext are user inputs.
|
||||||
* @author Ojasva Jain
|
* @author Ojasva Jain
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class HillCipher{
|
public class HillCipher {
|
||||||
static Scanner in = new Scanner (System.in);
|
static Scanner in = new Scanner(System.in);
|
||||||
|
|
||||||
/* Following function encrypts the message
|
/* Following function encrypts the message
|
||||||
*/
|
*/
|
||||||
static void encrypt(String message)
|
static void encrypt(String message) {
|
||||||
{
|
|
||||||
message = message.toUpperCase();
|
message = message.toUpperCase();
|
||||||
// Get key matrix
|
// Get key matrix
|
||||||
System.out.println("Enter key matrix size");
|
System.out.println("Enter key matrix size");
|
||||||
int n = in.nextInt();
|
int n = in.nextInt();
|
||||||
System.out.println("Enter Key/encryptionKey matrix ");
|
System.out.println("Enter Key/encryptionKey matrix ");
|
||||||
int keyMatrix[][] = new int [n][n];
|
int keyMatrix[][] = new int[n][n];
|
||||||
for(int i=0;i<n;i++){
|
for (int i = 0; i < n; i++) {
|
||||||
for(int j=0;j<n;j++){
|
for (int j = 0; j < n; j++) {
|
||||||
keyMatrix[i][j] = in.nextInt();
|
keyMatrix[i][j] = in.nextInt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//check if det = 0
|
//check if det = 0
|
||||||
if(determinant(keyMatrix,n)%26 == 0)
|
if (determinant(keyMatrix, n) % 26 == 0) {
|
||||||
{
|
|
||||||
System.out.println("Invalid key, as determinant = 0. Program Terminated");
|
System.out.println("Invalid key, as determinant = 0. Program Terminated");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int [][]messageVector = new int[n][1];
|
int[][] messageVector = new int[n][1];
|
||||||
String CipherText="";
|
String CipherText = "";
|
||||||
int cipherMatrix [][] = new int [n][1];
|
int cipherMatrix[][] = new int[n][1];
|
||||||
int j = 0;
|
int j = 0;
|
||||||
while(j<message.length()){
|
while (j < message.length()) {
|
||||||
for (int i = 0; i < n; i++){
|
for (int i = 0; i < n; i++) {
|
||||||
if(j>=message.length()){ messageVector[i][0] = 23;}
|
if (j >= message.length()) {
|
||||||
else
|
messageVector[i][0] = 23;
|
||||||
messageVector[i][0] = (message.charAt(j))%65;
|
} else
|
||||||
|
messageVector[i][0] = (message.charAt(j)) % 65;
|
||||||
System.out.println(messageVector[i][0]);
|
System.out.println(messageVector[i][0]);
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
int x, i;
|
int x, i;
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++) {
|
||||||
{
|
|
||||||
cipherMatrix[i][0] = 0;
|
cipherMatrix[i][0] = 0;
|
||||||
|
|
||||||
for (x = 0; x < n; x++)
|
for (x = 0; x < n; x++) {
|
||||||
{
|
|
||||||
cipherMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0];
|
cipherMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0];
|
||||||
}
|
}
|
||||||
System.out.println(cipherMatrix[i][0]);
|
System.out.println(cipherMatrix[i][0]);
|
||||||
cipherMatrix[i][0] = cipherMatrix[i][0] % 26;
|
cipherMatrix[i][0] = cipherMatrix[i][0] % 26;
|
||||||
}
|
}
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
CipherText += (char)(cipherMatrix[i][0] + 65);
|
CipherText += (char) (cipherMatrix[i][0] + 65);
|
||||||
}
|
}
|
||||||
System.out.println("Ciphertext: "+ CipherText);
|
System.out.println("Ciphertext: " + CipherText);
|
||||||
}
|
}
|
||||||
//Following function decrypts a message
|
|
||||||
static void decrypt(String message)
|
//Following function decrypts a message
|
||||||
{
|
static void decrypt(String message) {
|
||||||
message = message.toUpperCase();
|
message = message.toUpperCase();
|
||||||
// Get key matrix
|
// Get key matrix
|
||||||
System.out.println("Enter key matrix size");
|
System.out.println("Enter key matrix size");
|
||||||
int n = in.nextInt();
|
int n = in.nextInt();
|
||||||
System.out.println("Enter inverseKey/decryptionKey matrix ");
|
System.out.println("Enter inverseKey/decryptionKey matrix ");
|
||||||
int keyMatrix[][] = new int [n][n];
|
int keyMatrix[][] = new int[n][n];
|
||||||
for(int i=0;i<n;i++){
|
for (int i = 0; i < n; i++) {
|
||||||
for(int j=0;j<n;j++){
|
for (int j = 0; j < n; j++) {
|
||||||
keyMatrix[i][j] = in.nextInt();
|
keyMatrix[i][j] = in.nextInt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//check if det = 0
|
//check if det = 0
|
||||||
if(determinant(keyMatrix,n)%26 == 0)
|
if (determinant(keyMatrix, n) % 26 == 0) {
|
||||||
{
|
|
||||||
System.out.println("Invalid key, as determinant = 0. Program Terminated");
|
System.out.println("Invalid key, as determinant = 0. Program Terminated");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//solving for the required plaintext message
|
//solving for the required plaintext message
|
||||||
int [][]messageVector = new int[n][1];
|
int[][] messageVector = new int[n][1];
|
||||||
String PlainText="";
|
String PlainText = "";
|
||||||
int plainMatrix [][] = new int [n][1];
|
int plainMatrix[][] = new int[n][1];
|
||||||
int j = 0;
|
int j = 0;
|
||||||
while(j<message.length()){
|
while (j < message.length()) {
|
||||||
for (int i = 0; i < n; i++){
|
for (int i = 0; i < n; i++) {
|
||||||
if(j>=message.length()){ messageVector[i][0] = 23;}
|
if (j >= message.length()) {
|
||||||
else
|
messageVector[i][0] = 23;
|
||||||
messageVector[i][0] = (message.charAt(j))%65;
|
} else
|
||||||
|
messageVector[i][0] = (message.charAt(j)) % 65;
|
||||||
System.out.println(messageVector[i][0]);
|
System.out.println(messageVector[i][0]);
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
int x, i;
|
int x, i;
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++) {
|
||||||
{
|
|
||||||
plainMatrix[i][0] = 0;
|
plainMatrix[i][0] = 0;
|
||||||
|
|
||||||
for (x = 0; x < n; x++)
|
for (x = 0; x < n; x++) {
|
||||||
{
|
|
||||||
plainMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0];
|
plainMatrix[i][0] += keyMatrix[i][x] * messageVector[x][0];
|
||||||
}
|
}
|
||||||
|
|
||||||
plainMatrix[i][0] = plainMatrix[i][0] % 26;
|
plainMatrix[i][0] = plainMatrix[i][0] % 26;
|
||||||
}
|
}
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
PlainText += (char)(plainMatrix[i][0] + 65);
|
PlainText += (char) (plainMatrix[i][0] + 65);
|
||||||
|
}
|
||||||
|
System.out.println("Plaintext: " + PlainText);
|
||||||
}
|
}
|
||||||
System.out.println("Plaintext: "+PlainText);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determinant calculator
|
// Determinant calculator
|
||||||
public static int determinant(int a[][], int n){
|
public static int determinant(int a[][], int n) {
|
||||||
int det = 0, sign = 1, p = 0, q = 0;
|
int det = 0, sign = 1, p = 0, q = 0;
|
||||||
|
|
||||||
if(n==1){
|
if (n == 1) {
|
||||||
det = a[0][0];
|
det = a[0][0];
|
||||||
}
|
} else {
|
||||||
else{
|
int b[][] = new int[n - 1][n - 1];
|
||||||
int b[][] = new int[n-1][n-1];
|
for (int x = 0; x < n; x++) {
|
||||||
for(int x = 0 ; x < n ; x++){
|
p = 0;
|
||||||
p=0;q=0;
|
q = 0;
|
||||||
for(int i = 1;i < n; i++){
|
for (int i = 1; i < n; i++) {
|
||||||
for(int j = 0; j < n;j++){
|
for (int j = 0; j < n; j++) {
|
||||||
if(j != x){
|
if (j != x) {
|
||||||
b[p][q++] = a[i][j];
|
b[p][q++] = a[i][j];
|
||||||
if(q % (n-1) == 0){
|
if (q % (n - 1) == 0) {
|
||||||
p++;
|
p++;
|
||||||
q=0;
|
q = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
det = det + a[0][x] *determinant(b, n-1) * sign;
|
det = det + a[0][x] * determinant(b, n - 1) * sign;
|
||||||
sign = -sign;
|
sign = -sign;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return det;
|
return det;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to implement Hill Cipher
|
// Function to implement Hill Cipher
|
||||||
static void hillcipher(String message)
|
static void hillcipher(String message) {
|
||||||
{
|
|
||||||
message.toUpperCase();
|
message.toUpperCase();
|
||||||
System.out.println("What do you want to process from the message?");
|
System.out.println("What do you want to process from the message?");
|
||||||
System.out.println("Press 1: To Encrypt");
|
System.out.println("Press 1: To Encrypt");
|
||||||
System.out.println("Press 2: To Decrypt");
|
System.out.println("Press 2: To Decrypt");
|
||||||
short sc = in.nextShort();
|
short sc = in.nextShort();
|
||||||
if(sc == 1)
|
if (sc == 1)
|
||||||
encrypt(message);
|
encrypt(message);
|
||||||
else if(sc == 2)
|
else if (sc == 2)
|
||||||
decrypt(message);
|
decrypt(message);
|
||||||
else
|
else
|
||||||
System.out.println("Invalid input, program terminated.");
|
System.out.println("Invalid input, program terminated.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Driver code
|
// Driver code
|
||||||
public static void main(String[] args)
|
public static void main(String[] args) {
|
||||||
{
|
|
||||||
// Get the message to be encrypted
|
// Get the message to be encrypted
|
||||||
System.out.println("Enter message");
|
System.out.println("Enter message");
|
||||||
String message = in.nextLine();
|
String message = in.nextLine();
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package Ciphers;
|
package Ciphers;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import javax.swing.JOptionPane;
|
|
||||||
|
|
||||||
/** @author Nguyen Duy Tiep on 23-Oct-17. */
|
/**
|
||||||
|
* @author Nguyen Duy Tiep on 23-Oct-17.
|
||||||
|
*/
|
||||||
public final class RSA {
|
public final class RSA {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -1,18 +1,17 @@
|
|||||||
package ciphers;
|
package ciphers;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* The simple substitution cipher is a cipher that has been in use for many hundreds of years
|
* The simple substitution cipher is a cipher that has been in use for many hundreds of years
|
||||||
* (an excellent history is given in Simon Singhs 'the Code Book').
|
* (an excellent history is given in Simon Singhs 'the Code Book').
|
||||||
* It basically consists of substituting every plaintext character for a different ciphertext character.
|
* It basically consists of substituting every plaintext character for a different ciphertext character.
|
||||||
* It differs from the Caesar cipher in that the cipher alphabet is not simply the alphabet shifted,
|
* It differs from the Caesar cipher in that the cipher alphabet is not simply the alphabet shifted,
|
||||||
* it is completely jumbled.
|
* it is completely jumbled.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class simpleSubCipher {
|
public class SimpleSubCipher {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypt text by replacing each element with its opposite character.
|
* Encrypt text by replacing each element with its opposite character.
|
||||||
@ -25,7 +24,7 @@ public class simpleSubCipher {
|
|||||||
String encoded = "";
|
String encoded = "";
|
||||||
|
|
||||||
// This map is used to encode
|
// This map is used to encode
|
||||||
Map<Character,Character> cipherMap = new HashMap<Character,Character>();
|
Map<Character, Character> cipherMap = new HashMap<>();
|
||||||
|
|
||||||
char beginSmallLetter = 'a';
|
char beginSmallLetter = 'a';
|
||||||
char beginCapitalLetter = 'A';
|
char beginCapitalLetter = 'A';
|
||||||
@ -34,13 +33,13 @@ public class simpleSubCipher {
|
|||||||
String cipherCapital = cipherSmall.toUpperCase();
|
String cipherCapital = cipherSmall.toUpperCase();
|
||||||
|
|
||||||
// To handle Small and Capital letters
|
// To handle Small and Capital letters
|
||||||
for(int i = 0; i < cipherSmall.length(); i++){
|
for (int i = 0; i < cipherSmall.length(); i++) {
|
||||||
cipherMap.put(beginSmallLetter++,cipherSmall.charAt(i));
|
cipherMap.put(beginSmallLetter++, cipherSmall.charAt(i));
|
||||||
cipherMap.put(beginCapitalLetter++,cipherCapital.charAt(i));
|
cipherMap.put(beginCapitalLetter++, cipherCapital.charAt(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0; i < message.length(); i++){
|
for (int i = 0; i < message.length(); i++) {
|
||||||
if(Character.isAlphabetic(message.charAt(i)))
|
if (Character.isAlphabetic(message.charAt(i)))
|
||||||
encoded += cipherMap.get(message.charAt(i));
|
encoded += cipherMap.get(message.charAt(i));
|
||||||
else
|
else
|
||||||
encoded += message.charAt(i);
|
encoded += message.charAt(i);
|
||||||
@ -60,7 +59,7 @@ public class simpleSubCipher {
|
|||||||
String decoded = "";
|
String decoded = "";
|
||||||
|
|
||||||
|
|
||||||
Map<Character,Character> cipherMap = new HashMap<Character,Character>();
|
Map<Character, Character> cipherMap = new HashMap<Character, Character>();
|
||||||
|
|
||||||
char beginSmallLetter = 'a';
|
char beginSmallLetter = 'a';
|
||||||
char beginCapitalLetter = 'A';
|
char beginCapitalLetter = 'A';
|
||||||
@ -68,13 +67,13 @@ public class simpleSubCipher {
|
|||||||
cipherSmall = cipherSmall.toLowerCase();
|
cipherSmall = cipherSmall.toLowerCase();
|
||||||
String cipherCapital = cipherSmall.toUpperCase();
|
String cipherCapital = cipherSmall.toUpperCase();
|
||||||
|
|
||||||
for(int i = 0; i < cipherSmall.length(); i++){
|
for (int i = 0; i < cipherSmall.length(); i++) {
|
||||||
cipherMap.put(cipherSmall.charAt(i),beginSmallLetter++);
|
cipherMap.put(cipherSmall.charAt(i), beginSmallLetter++);
|
||||||
cipherMap.put(cipherCapital.charAt(i),beginCapitalLetter++);
|
cipherMap.put(cipherCapital.charAt(i), beginCapitalLetter++);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0; i < encryptedMessage.length(); i++){
|
for (int i = 0; i < encryptedMessage.length(); i++) {
|
||||||
if(Character.isAlphabetic(encryptedMessage.charAt(i)))
|
if (Character.isAlphabetic(encryptedMessage.charAt(i)))
|
||||||
decoded += cipherMap.get(encryptedMessage.charAt(i));
|
decoded += cipherMap.get(encryptedMessage.charAt(i));
|
||||||
else
|
else
|
||||||
decoded += encryptedMessage.charAt(i);
|
decoded += encryptedMessage.charAt(i);
|
||||||
@ -84,8 +83,8 @@ public class simpleSubCipher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String a = encode("defend the east wall of the castle","phqgiumeaylnofdxjkrcvstzwb");
|
String a = encode("defend the east wall of the castle", "phqgiumeaylnofdxjkrcvstzwb");
|
||||||
String b = decode(a,"phqgiumeaylnofdxjkrcvstzwb");
|
String b = decode(a, "phqgiumeaylnofdxjkrcvstzwb");
|
||||||
System.out.println(b);
|
System.out.println(b);
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
|||||||
package Ciphers;
|
package Ciphers;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The simple substitution cipher is a cipher that has been in use for many hundreds of years (an
|
* The simple substitution cipher is a cipher that has been in use for many hundreds of years (an
|
||||||
@ -74,7 +75,9 @@ public class SimpleSubstitutionCipher {
|
|||||||
return decoded.toString();
|
return decoded.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** TODO remove main and make JUnit Testing */
|
/**
|
||||||
|
* TODO remove main and make JUnit Testing
|
||||||
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String a = encode("defend the east wall of the castle", "phqgiumeaylnofdxjkrcvstzwb");
|
String a = encode("defend the east wall of the castle", "phqgiumeaylnofdxjkrcvstzwb");
|
||||||
String b = decode(a, "phqgiumeaylnofdxjkrcvstzwb");
|
String b = decode(a, "phqgiumeaylnofdxjkrcvstzwb");
|
||||||
|
@ -1,83 +0,0 @@
|
|||||||
package Ciphers;
|
|
||||||
class affineCipher
|
|
||||||
{
|
|
||||||
|
|
||||||
// Key values of a and b
|
|
||||||
static int a = 17;
|
|
||||||
static int b = 20;
|
|
||||||
|
|
||||||
static String encryptMessage(char[] msg)
|
|
||||||
{
|
|
||||||
/// Cipher Text initially empty
|
|
||||||
String cipher = "";
|
|
||||||
for (int i = 0; i < msg.length; i++)
|
|
||||||
{
|
|
||||||
// Avoid space to be encrypted
|
|
||||||
/* applying encryption formula ( a x + b ) mod m
|
|
||||||
{here x is msg[i] and m is 26} and added 'A' to
|
|
||||||
bring it in range of ascii alphabet[ 65-90 | A-Z ] */
|
|
||||||
if (msg[i] != ' ')
|
|
||||||
{
|
|
||||||
cipher = cipher
|
|
||||||
+ (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
|
|
||||||
} else // else simply append space character
|
|
||||||
{
|
|
||||||
cipher += msg[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return cipher;
|
|
||||||
}
|
|
||||||
|
|
||||||
static String decryptCipher(String cipher)
|
|
||||||
{
|
|
||||||
String msg = "";
|
|
||||||
int a_inv = 0;
|
|
||||||
int flag = 0;
|
|
||||||
|
|
||||||
//Find a^-1 (the multiplicative inverse of a
|
|
||||||
//in the group of integers modulo m.)
|
|
||||||
for (int i = 0; i < 26; i++)
|
|
||||||
{
|
|
||||||
flag = (a * i) % 26;
|
|
||||||
|
|
||||||
// Check if (a*i)%26 == 1,
|
|
||||||
// then i will be the multiplicative inverse of a
|
|
||||||
if (flag == 1)
|
|
||||||
{
|
|
||||||
a_inv = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 0; i < cipher.length(); i++)
|
|
||||||
{
|
|
||||||
/*Applying decryption formula a^-1 ( x - b ) mod m
|
|
||||||
{here x is cipher[i] and m is 26} and added 'A'
|
|
||||||
to bring it in range of ASCII alphabet[ 65-90 | A-Z ] */
|
|
||||||
if (cipher.charAt(i) != ' ')
|
|
||||||
{
|
|
||||||
msg = msg + (char) (((a_inv *
|
|
||||||
((cipher.charAt(i) + 'A' - b)) % 26)) + 'A');
|
|
||||||
}
|
|
||||||
else //else simply append space character
|
|
||||||
{
|
|
||||||
msg += cipher.charAt(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Driver code
|
|
||||||
public static void main(String[] args)
|
|
||||||
{
|
|
||||||
String msg = "AFFINE CIPHER";
|
|
||||||
|
|
||||||
// Calling encryption function
|
|
||||||
String cipherText = encryptMessage(msg.toCharArray());
|
|
||||||
System.out.println("Encrypted Message is : " + cipherText);
|
|
||||||
|
|
||||||
// Calling Decryption function
|
|
||||||
System.out.println("Decrypted Message is: " + decryptCipher(cipherText));
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user