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 14 Record Keyword
Java Program to Check whether Graph is Biconnected
Chuyển đổi giữa các kiểu dữ liệu trong Java
Send email with authentication
Spring Boot with Multiple SQL Import Files
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Java Program to Implement Patricia Trie
Spring Boot - CORS Support
Spring Boot - Exception Handling
Performance Difference Between save() and saveAll() in Spring Data
Spring Boot - Cloud Configuration Server
Tiêu chuẩn coding trong Java (Coding Standards)
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Xây dựng ứng dụng Client-Server với Socket trong Java
Cài đặt và sử dụng Swagger UI
Toán tử instanceof trong java
Migrating from JUnit 4 to JUnit 5
Java Program to Implement Ternary Search Algorithm
Introduction to Using Thymeleaf in Spring
Java Program to Implement Graph Coloring Algorithm
Java Program to Implement Extended Euclid Algorithm
Wrapper Classes in Java
Removing all Nulls from a List in Java
Java Program to Find All Pairs Shortest Path
Changing Annotation Parameters At Runtime
Tìm hiểu về xác thực và phân quyền trong ứng dụng
“Stream has already been operated upon or closed” Exception in Java
Java Program to Check whether Graph is a Bipartite using DFS
Java Program to Implement Find all Cross Edges in a Graph
Java Program to Implement Sparse Array
Java Program to Implement HashTable API