{"id":8245,"date":"2019-12-22T21:26:31","date_gmt":"2019-12-22T15:56:31","guid":{"rendered":"https:\/\/java2blog.com\/?p=8245"},"modified":"2021-01-11T18:40:23","modified_gmt":"2021-01-11T13:10:23","slug":"caesar-cipher-in-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/caesar-cipher-in-java\/","title":{"rendered":"Caesar Cipher in Java"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Algorithm\">Algorithm<\/a><\/li><li><a href=\"#Implementation\">Implementation<\/a><ul><li><a href=\"#Step_1Mapping_Plaintext\">Step 1(Mapping Plaintext) :<\/a><\/li><li><a href=\"#Step_2Encrypting_and_Obtaining_CipherText\">Step 2(Encrypting and Obtaining CipherText) :<\/a><\/li><li><a href=\"#Step_3Decrypting_and_Obtaining_our_original_Text\">Step 3(Decrypting and Obtaining our original Text) :<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<p>In this post, we will see about Caesar Cipher in Java.<\/p>\n<p>In cryptography, we used to study different algorithms or techniques to encrypt and decrypt a different sets of messages to gain confidentiality, integrity or say some kind of security.<\/p>\n<p>Usually, these things are achieved by implementing such kind of techniques, sometimes clubbed with other algorithms to increase the security level.<\/p>\n<p>In this article, we will discuss about one such <strong>encryption technique<\/strong> which is one of the earliest and easiest methods, named after Julius Caesar.<\/p>\n<p>Before we discuss the actual algorithm and procedure of this technique we have to get familiar with the few terms which we might need to understand as a prerequisite to get to know the working of the technique.<\/p>\n<ol>\n<li><strong>Plaintext <\/strong>: It is text that is not formatted specially, or computed. In simple words, it is nothing but the original message we are planning to send to the end-user.<\/li>\n<li><strong>Encryption <\/strong>: It is the process by which the given message or say plaintext is encoded using the different encryption algorithms to ensure that only authorized users or parties can decode it and access them for their use.<\/li>\n<li><strong>Ciphertext <\/strong>: It is the formatted text or says it is the scrambled form of the data after being encrypted.The encoded text or message we get after applying some set of encryption algorithms on the given plaintext, is termed as the ciphertext.\n<\/li>\n<li><strong>Decryption : <\/strong>It is the process by which we obtain the encrypted message or the ciphertext back to its original form. In most of the cases, the algorithms are reversible in nature, therefore in those cases, we directly apply the same encryption algorithm in a reverse way.<\/li>\n<\/ol>\n<p>Note : The terms are explained in the order in which they are used in cryptography.<\/p>\n<p>Now since we know all the related terms let\u2019s discuss the actual algorithm of Caesar Cipher<\/p>\n<h2><span id=\"Algorithm\"><strong>Algorithm<\/strong><\/span><\/h2>\n<p>It is a simple type of substitution cipher, in this, each letter or word of a given text message is replaced by a letter some fixed number down the original alphabet.<\/p>\n<p>We decide that fixed number, for example, if we select that number as 2 then A will be replaced by C, B will be replaced by D, and so on.<\/p>\n<p>This fixed number here indicates the shift, which means the number of positions by which each letter of the text has to be moved down.<\/p>\n<blockquote><p><em>Modified Approach<\/em><\/p>\n<p><em>(We can also modify the algorithm for moving up the character it\u2019s up to the user, in that scenario if shift equals 2, A will be replaced by Y, B will be replaced by Z, and so on.)<\/em><\/p><\/blockquote>\n<h2><span id=\"Implementation\"><strong>Implementation<\/strong><\/span><\/h2>\n<h3><span id=\"Step_1Mapping_Plaintext\"><span style=\"color: #f89820;\"><strong>Step 1(Mapping Plaintext) : <\/strong><\/span><\/span><\/h3>\n<p>We start this by representing or transforming each letter into numbers, therefore the alphabets will be mapped to the numbers starting from 0, example, A = 0, B = 1, . . . . . , Z = 25.<\/p>\n<h3><span id=\"Step_2Encrypting_and_Obtaining_CipherText\"><span style=\"color: #f89820;\"><strong>Step 2(Encrypting and Obtaining CipherText) : <\/strong><\/span><\/span><\/h3>\n<p>To encrypt the plaintext or the message we add the shift, taken as input from the user to the mapped representation of the extracted letter in the Step 1.<\/p>\n<p>We iterate this through the input text and repeat the Step 2 for each alphabet in the text to obtain the ciphertext.<\/p>\n<p><strong>E<sub>n<\/sub> = ( x + n ) mod 26 , <\/strong>where n represents shift.<\/p>\n<h3><span id=\"Step_3Decrypting_and_Obtaining_our_original_Text\"><span style=\"color: #f89820;\"><strong>Step 3(Decrypting and Obtaining our original Text) :<\/strong><\/span><\/span><\/h3>\n<p>Now, to decrypt it we follow the same algorithm but in reverse as we discussed before, here we will subtract the shift from the obtained representation in the Step 2 to get back our original text.<\/p>\n<p><strong>D<sub>n<\/sub> = ( x &#8211; n ) mod 26 , <\/strong>where n represents shift.<\/p>\n<p>Here is java program to implement Caesar Cipher in java.<\/p>\n<pre class=\"java\" name=\"code\" title=\"CaesarCipherMain.java\">package org.arpit.java2blog;\n\nimport java.util.Scanner;\n\npublic class CaesarCipherMain {\n    public static final String alph = \"abcdefghijklmnopqrstuvwxyz\";\n    \/\/ static because all other functions are static, and we can't reference from a\n    \/\/ non-static\n\n    public static String encoding(String plainT, int shift) {\n        plainT = plainT.toLowerCase();\n        \/\/ converting the text to lowercase\n        String cipherT = \"\";\n        \/\/ initializing empty string to add alphabets iteratively\n        for (int i = 0; i < plainT.length(); i++) {\n            int mappingV = alph.indexOf(plainT.charAt(i));\n            \/\/ value of each alphabet in integers like for A=0, B=1 ...\n            int enVal = (shift + mappingV) % 26;\n            char Val = alph.charAt(enVal); \/\/ the character to be replaced\n            cipherT = cipherT + Val; \/\/ adding to ciphertext\n        }\n        return cipherT;\n    }\n\n    \/\/ following same algorithm but in reverse way, plaintext becomes\n    \/\/ ciphertext and vice versa\n    public static String decoding(String cipherT, int shift) {\n        cipherT = cipherT.toLowerCase();\n        \/\/ converting the text to lowercase\n        String plainT = \"\";\n        \/\/ initializing empty string to add alphabets iteratively\n        for (int i = 0; i < cipherT.length(); i++) {\n            int mappingV = alph.indexOf(cipherT.charAt(i));\n            int deVal = (mappingV - shift) % 26;\n            if (deVal < 0) \/\/ to handle the negative values\n            {\n                deVal = alph.length() + deVal;\n            }\n            char Val = alph.charAt(deVal); \/\/ the character to be replaced\n            plainT = plainT + Val; \/\/ adding to plaintext\n        }\n        return plainT;\n    }\n\n    public static void main(String[] args) {\n        Scanner scan = new Scanner(System.in);\n        System.out.println(\"Enter the text message to be encrypted \");\n        String msg = new String();\n        msg = scan.next();\n        System.out.println(\" Encrypted Text : \" + encoding(msg, 4));\n        System.out.print(\" Decryptd Text : \");\n        System.out.print(decoding(encoding(msg, 4), 4));\n        scan.close();\n    }\n}\n<\/pre>\n<p>The output for the above code is shown below, as we can see the entered text was obtained in encrypted form and then we again decrypted it to obtain the original text.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nEnter the text message to be encrypted hiarpit<br \/>\nEncrypted Text : lmevtmx<br \/>\nDecryptd Text : hiarpit\n<\/div>\n<p>That's all about Caesar Cipher in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsAlgorithmImplementationStep 1(Mapping Plaintext) :Step 2(Encrypting and Obtaining CipherText) :Step 3(Decrypting and Obtaining our original Text) : In this post, we will see about Caesar Cipher in Java. In cryptography, we used to study different algorithms or techniques to encrypt and decrypt a different sets of messages to gain confidentiality, integrity or say some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[71],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/8245"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=8245"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/8245\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=8245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=8245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=8245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}