Java Program to Implement Affine Cipher

This is a java program to implement Affine Cipher. The affine cipher is a type of monoalphabetic substitution cipher, wherein each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a standard substitution cipher with a rule governing which letter goes to which. As such, it has the weaknesses of all substitution ciphers. Each letter is enciphered with the function (ax+b)mod(26), where b is the magnitude of the shift.

Here is the source code of the Java Program to Implement Affine Cipher. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

package com.maixuanviet.setandstring;
 
import java.util.Scanner;
 
public class AffineCipher
{
    public static String encryptionMessage(String Msg)
    {
        String CTxt = "";
        int a = 3;
        int b = 6;
        for (int i = 0; i < Msg.length(); i++)
        {
            CTxt = CTxt + (char) ((((a * Msg.charAt(i)) + b) % 26) + 65);
        }
        return CTxt;
    }
 
    public static String decryptionMessage(String CTxt)
    {
        String Msg = "";
        int a = 3;
        int b = 6;
        int a_inv = 0;
        int flag = 0;
        for (int i = 0; i < 26; i++)
        {
            flag = (a * i) % 26;
            if (flag == 1)
            {
                a_inv = i;
                System.out.println(i);
            }
        }
        for (int i = 0; i < CTxt.length(); i++)
        {
            Msg = Msg + (char) (((a_inv * ((CTxt.charAt(i) - b)) % 26)) + 65);
        }
        return Msg;
    }
 
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the message: ");
        String message = sc.next();
        System.out.println("Message is :" + message);
        System.out.println("Encrypted Message is : "
                + encryptionMessage(message));
        System.out.println("Decrypted Message is: "
                + decryptionMessage(encryptionMessage(message)));
        sc.close();
    }
}

Output:

$ javac AffineCipher.java
$ java AffineCipher
 
Enter the message: 
MAIXUANVIET
Message is :MAIXUANVIET
Encrypted Message is : VTGIJBGCSN
9
Decrypted Message is: MAIXUANVIET

Related posts:

Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Perform Searching Based on Locality of Reference
The Order of Tests in JUnit
How to Get the Last Element of a Stream in Java?
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Spring Boot - Enabling HTTPS
Case-Insensitive String Matching in Java
Java Program to Implement Hamiltonian Cycle Algorithm
Java Program to Perform Complex Number Multiplication
Creating Docker Images with Spring Boot
Using JWT with Spring Security OAuth (legacy stack)
Java Program to Implement a Binary Search Tree using Linked Lists
Lớp Collections trong Java (Collections Utility Class)
Java Program to Implement Splay Tree
Java Web Services – JAX-WS – SOAP
Java Program to Implement Johnson’s Algorithm
Java Program to implement Bit Set
Checked and Unchecked Exceptions in Java
Giới thiệu SOAP UI và thực hiện test Web Service
Java Program to Describe the Representation of Graph using Incidence Matrix
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Implement Sieve Of Atkin
Intro to the Jackson ObjectMapper
Marker Interface trong Java
Spring Boot - Quick Start
Guide to Java Instrumentation
Interface trong Java 8 – Default method và Static method
Enum trong java
Java Program to find the peak element of an array using Binary Search approach
Java Program to Perform LU Decomposition of any Matrix