Hi! So this is my first blog based on Programming. Since I'm majoring in Computer Science, I usually have coding problems while I'm working on various projects. And sometimes as you may have experienced at some point, it is very difficult to find the solution you're looking for, up on the internet. And sometimes, the solution is available but its either not up to date, or doesn't exactly have what you were looking for. But you eventually gotta find a solution anyway.
So, I have decided to put up codes I couldn't find on the internet but ended up modifying according to my needs. I'm doing this because I think this may help you get closer to your own solution.
OKAY, back to the point you are actually here for. Caeser Ciphering is one of the first and simplest type of substitution encryption. I'm gonna use Java programming language to encrypt and decrypt a Text/Message entered by the user.
Output:
Here, I used the same method "encrypt()" for encryption as well as decryption just by sending different parameters 'key' and '26-key' respectively.
You can try it yourself by clicking here.
Comment below if you have any questions.
So, I have decided to put up codes I couldn't find on the internet but ended up modifying according to my needs. I'm doing this because I think this may help you get closer to your own solution.
OKAY, back to the point you are actually here for. Caeser Ciphering is one of the first and simplest type of substitution encryption. I'm gonna use Java programming language to encrypt and decrypt a Text/Message entered by the user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package idk; import java.util.Scanner; /** * * @author dasja */ public class Caeser_encrypt_decrypt { public static StringBuffer encrypt(String text, int key) { StringBuffer result = new StringBuffer(); for (int i = 0; i < text.length(); i++) { if (Character.isUpperCase(text.charAt(i))) { char ch = (char) (((int) text.charAt(i) + key - 65) % 26 + 65); result.append(ch); } else { char ch = (char) (((int) text.charAt(i) + key - 97) % 26 + 97); result.append(ch); } } return result; } public static void main(String[] args) { int key = 3; Scanner sc = new Scanner(System.in); System.out.println("Enter your text message"); String message = sc.next(); //encrpt of your message StringBuffer cipherText=encrypt(message,key); System.out.println("Cipher : " + cipherText); //convert StringBuffer type to String inorder to pass it as a String parameter to your method StringBuffer str = new StringBuffer(cipherText); String strCipher=str.toString(); //decrpyt your message StringBuffer originalMsg=encrypt(strCipher,26-key); System.out.println("Decipher : " + originalMsg); } } |
Output:
Here, I used the same method "encrypt()" for encryption as well as decryption just by sending different parameters 'key' and '26-key' respectively.
You can try it yourself by clicking here.
Comment below if you have any questions.
Comments
Post a Comment