Java encryption program 🔑

preview_player
Показать описание
Java substitution encryption tutorial

IMPORTANT NOTE: I don't know much about encryption. I attempted to write this encryption program by popular request. This is a substitution cipher and not a OTP. My bad

#java #encryption #tutorial
Рекомендации по теме
Комментарии
Автор

IMPORTANT NOTE: This is a substitution cipher and not OTP. My bad

public class Main {

public static void main(String[] args) {

EncryptionProgram ep = new EncryptionProgram();

}

}

import java.util.*;

public class EncryptionProgram {

private Scanner scanner;
private Random random;
private ArrayList<Character> list;
private ArrayList<Character> shuffledList;
private char character;
private String line;
private char[] letters;

EncryptionProgram(){
scanner = new Scanner(System.in);
random = new Random();
list = new ArrayList();
shuffledList = new ArrayList();
character = ' ';

newKey();
askQuestion();
}
private void askQuestion(){
while(true) {

System.out.println("What do you want to do?");
System.out.println("(N)ewKey, (G)etKey, (E)ncrypt, (D)ecrypt, (Q)uit");
char response =

switch(response) {
case 'N':
newKey();
break;
case 'G':
getKey();
break;
case 'E':
encrypt();
break;
case 'D':
decrypt();
break;
case 'Q':
quit();
break;
default:
System.out.println("Not a valid answer :(");
}
}
}
private void newKey(){

character = ' ';
list.clear();
shuffledList.clear();

for(int i=32;i<127;i++) {

character++;
}

shuffledList = new ArrayList(list);

System.out.println("*A new key has been generated*");

}
private void getKey(){
System.out.println("Key: ");
for(Character x : list) {
System.out.print(x);
}
System.out.println();
for(Character x : shuffledList) {
System.out.print(x);
}
System.out.println();
}
private void encrypt(){
System.out.println("Enter a message to be encrypted: ");
String message = scanner.nextLine();

letters = message.toCharArray();

for(int i =0;i<letters.length;i++) {

for(int j =0;j<list.size();j++) {
{


}
}
}
");
for(char x : letters) {
System.out.print(x);
}
System.out.println();
}
private void decrypt(){
System.out.println("Enter a message to be decrypted: ");
String message = scanner.nextLine();

letters = message.toCharArray();

for(int i =0;i<letters.length;i++) {

for(int j =0;j<shuffledList.size();j++) {
{


}
}
}
");
for(char x : letters) {
System.out.print(x);
}
System.out.println();
}
private void quit(){
System.out.println("Thank you, have a nice day bro!");
System.exit(0);
}
}

BroCodez
Автор

Easily the best teacher I've come across for teaching programming! I'm not quite a beginner but my knowledge was pretty disorganized and scattered, your videos help me a great deal! thank you! keep up the awesome work Bro!!!!

riseofkingdoms
Автор

just found this channel by accident, subscribed without a hesitation. keep up with the UNIQUE and WHOLESOME work man!

winterSweet-km
Автор

Ideea: add two new methods:
saveKey() and setKey().
Save the key to a file and then reload that key from the file.

So, two way encrypted communication based on a common key file.

Ofcourse that key file can be encrypted with a known variable (like a password). Just shifted a few chars. That way even if someone has the keyfile, still needs to unshift the key in order to work.

radusimonica
Автор

Strains of, Car car equals new car, and, Dog dog equals new dog, keep rolling through my head. good job. thank you

stephaniepanah
Автор

Awesome. Thank you Bro! This project is very useful. The way you teach is just AWESOME!

DanielSmith-ujrr
Автор

I find it inconvenient as the all the data is clear when I terminate the program .

I want to restore the previous data when I start over the program .

I recall that you have taught me how save object's information and reload it by using serialization and deserialization .

I think it's a good practice and challenge for me to combine the encryption, serialization and deserialization together .

Here's my code .

Class : UserInfo

import java.io.Serializable;
import java.util.ArrayList;

public class UserInfo implements Serializable {

private static final long serialVersionUID = 1L;

ArrayList<Character> list ;
ArrayList<Character> shuffledList ;
String userMessage, encryptedUserMessage ;
}

Class : EncryptionProgram

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

import java.io.* ;

public class EncryptionProgram {

transient private Scanner scanner ;
private ArrayList<Character> list ;
private ArrayList<Character> shuffledList ; // shuffled(洗牌的)
private char character ;
private char[] letterArray ;
private String userMessage, encryptedUserMessage ;

EncryptionProgram() throws ClassNotFoundException, IOException {

scanner = new Scanner(System.in);
list = new ArrayList<>() ;
shuffledList = new ArrayList<>() ;
character = ' ' ; // ASCII values of [space] = 32

askQuestion() ;
}

private void askQuestion() throws ClassNotFoundException, IOException {

while (true) {
;
System.out.println("What do you want to do ?") ;
, (N)ewKey], (G)etKey, (E)ncrypt, (D)ecrypt, (M)essage, (Q)uit") ;
char responseChar = ;

switch (responseChar) {
'L' :
;
;
'N' :
;
;
'G' :
;
;
'E' :
;
;
'D' :
;
;
'M' :
;
;
'Q' :
;
;
:
a valid answer .") ;
;
}
}
}

// generate new key .
private void newKey() {
// reset the character to 32 .
character = ' ' ; // ASCII values of [space] = 32
list.clear() ;
shuffledList.clear() ;
encryptedUserMessage = "" ;

// ASCII decimal (32-126), 32 = [space], 127 = [DEL] ;
for (int i = 32 ; i < 127 ; i++ ) {
;
}

shuffledList = new ArrayList<>(list) ;
;
System.out.println("A new key has been generated .") ;
}

// retrieve the key .
private void getKey() {
System.out.println("Key : ") ;
for(Character x : list) {
System.out.print(x) ;
}
System.out.println() ;
key : ") ;
for(Character x : shuffledList) {
System.out.print(x) ;
}
System.out.println() ;
}

// encrypt the message .
private void encrypt() {
System.out.println("Please send a a message for encryption :") ;
String message = scanner.nextLine() ;
userMessage = message ;
encryptedUserMessage = "" ; // it will = null if no initialization .
letterArray = message.toCharArray() ;

int i = 0 ;
for (char x : letterArray) {
letterArray[i++] = ;
encryptedUserMessage += ;
}

;
}

// decrypt the message .
private void decrypt() {
System.out.println("Please send a a message for decryption :") ;
String message = scanner.nextLine() ;
letterArray = message.toCharArray() ;

int i = 0 ;
for (char x : letterArray) {
letterArray[i++] = ;
}

;
}

private void message() {
System.out.printf("Your message before encryption : \"%s\"\n", userMessage) ;
System.out.printf("Your message after encryption : \"%s\"\n", encryptedUserMessage) ;
}

private void load() throws IOException, ClassNotFoundException {
UserInfo userInfo = null ;

// create a file to store you object'sinformation .
FileInputStream fileIn = new ;
ObjectInputStream objectIn = new ObjectInputStream(fileIn) ;
// User class is not recorded in file so we need to cast it as the object type .
userInfo = (UserInfo) objectIn.readObject() ;

fileIn.close() ;
objectIn.close() ;

list = userInfo.list ;
shuffledList = userInfo.shuffledList ;
userMessage = userInfo.userMessage ;
encryptedUserMessage = userInfo.encryptedUserMessage ;
loaded .") ;
}

private void quit() throws IOException {

UserInfo userInfo = new UserInfo() ;
userInfo.list = list ;
userInfo.shuffledList = shuffledList ;
userInfo.userMessage = userMessage ;
= encryptedUserMessage ;

// create a file to store you object'sinformation .
FileOutputStream fileOut = new ;
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut) ;
;

fileOut.close() ;
objectOut.close() ;

saved .") ;
System.out.println("See you next time .") ;
System.exit(9) ;
}
}

Class : Main

import java.io.IOException;

public class Main {

public static void main(String[] args) throws ClassNotFoundException, IOException {

EncryptionProgram ep = new EncryptionProgram() ;
}
}

maxwong
Автор

man! this was so cool and easy to learn! Thanks for this :) 💜

kxnyshk
Автор

Program complete. This walkthrough is golden. Thanks, Bro!

dth
Автор

I would use 2 shuffled lists and took from each by index in order. It would allow to avoid double letters.

TyyylerDurden
Автор

7:27 🤔Why we declared "random" and initialized but didn't use it ?

Ahmed_TheProgramer
Автор

Bro!, You are amazing, I just wanna say Thank you.

meksudbuser
Автор

for encrypt method I think it will be better if we wrote it like this:

private void encrypt(){

System.out.print("\nEnter a message to be encrypted :");

line = scanner.next();
int lineLength = line.length();
this.letters = new char[lineLength];

for(int i = 0; i < lineLength; i++)
letters[i] = shuffledList.get( list.indexOf( line.charAt(i) ) );

System.out.println("Your encrypted message is :" + String.copyValueOf(letters));

}

Ahmed_TheProgramer
Автор

I find another quicker way to encrypt the message instead of nested loop .

I use the listName.indexOf(element) .
// You can use it with an non-primitive array by changing it to list first .
//

private void encrypt() {
System.out.println("Please send a a message for encryption :") ;
String message = scanner.nextLine() ;
letterArray = message.toCharArray() ;

int i = 0 ;
for (char x : letterArray) {
letterArray[i++] = ;
}


; // array cannot be print directly as only string representation will be shown, except char array .
}

maxwong
Автор

my bro, thanks for this java tutorial <3;

Geno
Автор

Nice tutorial but this is not One-time pad encryption. This is a simple replacement encryption and can be cracked with the proper program and a sufficient large message.

ΒαγγΣτανιος
Автор

thanks man, gonna use this skill for my upcoming internship!

jordanguada
Автор

Excellent. Learned a lot. Thank You, Sir

roryodonoghue
Автор

Thank you. This helps a lot. Am a beginner at this

rodelioliwag
Автор

so easy and so necessery)
very usefull, thnx

McMeil
welcome to shbcf.ru