Fix formatting in Ciphers package (#2756)

This commit is contained in:
Yang Libin
2021-10-29 13:19:42 +08:00
committed by GitHub
parent 0503c46680
commit 794719c773
11 changed files with 1269 additions and 1380 deletions

View File

@@ -1,36 +1,23 @@
//The key for the Affine cipher consists of 2 numbers, well 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;
import java.util.Scanner;
class AffineCipher {
class AffineCipher
{
static Scanner in = new Scanner(System.in);
// Key values of a and b
static int a = 17;
static int b = 20;
static String encryptMessage(char[] msg)
{
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
static String encryptMessage(char[] msg) {
/// Cipher Text initially empty
String cipher = "";
for (int i = 0; i < msg.length; i++)
{
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] != ' ')
{
if (msg[i] != ' ') {
cipher = cipher
+ (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
} else // append space character
} else // else simply append space character
{
cipher += msg[i];
}
@@ -38,42 +25,30 @@ class AffineCipher
return 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();
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++)
{
for (int i = 0; i < 26; i++) {
flag = (a * i) % 26;
// Check if (a*i)%26 == 1,
// if so, then i will be the multiplicative inverse of a
if (flag == 1)
{
// then i will be the multiplicative inverse of a
if (flag == 1) {
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
{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) != ' ')
{
if (cipher.charAt(i) != ' ') {
msg = msg + (char) (((a_inv *
((cipher.charAt(i) + 'A' - b)) % 26)) + 'A');
}
else // append space character
} else //else simply append space character
{
msg += cipher.charAt(i);
}
@@ -82,17 +57,17 @@ class AffineCipher
return msg;
}
// Main method
public static void main(String[] args)
{
// Driver code
public static void main(String[] args) {
String msg = "AFFINE CIPHER";
// Encrypting message
// Calling encryption function
String cipherText = encryptMessage(msg.toCharArray());
System.out.println("Encrypted Message is : " + cipherText);
// Decrypting message
// Calling Decryption function
System.out.println("Decrypted Message is: " + decryptCipher(cipherText));
}
}