Java encryption program 🔑

Поділитися
Вставка
  • Опубліковано 14 січ 2025

КОМЕНТАРІ • 87

  • @riseofkingdoms2469
    @riseofkingdoms2469 3 роки тому +29

    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!!!!

  • @winterSweet-k4m
    @winterSweet-k4m 4 роки тому +15

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

  • @BroCodez
    @BroCodez  4 роки тому +29

    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 list;
    private ArrayList 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("********************************************");
    System.out.println("What do you want to do?");
    System.out.println("(N)ewKey,(G)etKey,(E)ncrypt,(D)ecrypt,(Q)uit");
    char response = Character.toUpperCase(scanner.nextLine().charAt(0));

    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

  • @stephaniepanah1387
    @stephaniepanah1387 2 роки тому +3

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

  • @radusimonica5550
    @radusimonica5550 3 роки тому +17

    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.

    • @Michel-dx1bn
      @Michel-dx1bn 2 роки тому +3

      You have such a hacker brain :)

    • @andromydous
      @andromydous 2 роки тому

      I second that. I have a project that involves a login screen, a create account screen, and a user's screen. Each user has their own .ser file, so that when they log in, the login page looks for their username.ser file and checks out if it(and their password) matches. I got stumped on the administration page, though. As administrator, I want to be able to see all of the users. For some reason, the program only remembers the last user created and overwrites any that were created before. I can't seem to make the arraylist permanent.

    • @sounavamandal9f223
      @sounavamandal9f223 2 роки тому

      i have the same idea of taht set key and i tried to implemnt that but somehow my array isnt geeting coverted into array list

  • @DanielSmith-uj7rr
    @DanielSmith-uj7rr 3 роки тому +2

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

  • @kxnyshk
    @kxnyshk 3 роки тому +5

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

  • @Ahmed_TheProgramer
    @Ahmed_TheProgramer Рік тому +4

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

    • @MyBoatHasAFlatTire
      @MyBoatHasAFlatTire 2 місяці тому

      I am a year late, but I am assuming that he assumed that that we will not notice and not assume that it's pointless, but I also assume that he assumed that we will use it in the video.

  • @11d7th
    @11d7th Рік тому

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

  • @jordanguada4354
    @jordanguada4354 4 роки тому +1

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

  • @maxwong1768
    @maxwong1768 2 роки тому +2

    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 list ;
    ArrayList 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 list ;
    private ArrayList 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("**************************") ;
    System.out.println("What do you want to do ?") ;
    System.out.println("[(L)oad , (N)ewKey] , (G)etKey , (E)ncrypt , (D)ecrypt , (M)essage , (Q)uit") ;
    char responseChar = Character.toUpperCase(scanner.nextLine().charAt(0)) ;

    switch (responseChar) {
    case 'L' :
    load() ;
    break ;
    case 'N' :
    newKey() ;
    break ;
    case 'G' :
    getKey() ;
    break ;
    case 'E' :
    encrypt() ;
    break ;
    case 'D' :
    decrypt() ;
    break ;
    case 'M' :
    message() ;
    break ;
    case 'Q' :
    quit() ;
    break ;
    default :
    System.out.println("Not a valid answer .") ;
    break ;
    }
    }
    }

    // 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++ ) {
    list.add(Character.valueOf(character++)) ;
    }

    shuffledList = new ArrayList(list) ;
    Collections.shuffle(shuffledList) ;
    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() ;
    System.out.println("Shuffled 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++] = shuffledList.get(list.indexOf(x)) ;
    encryptedUserMessage += shuffledList.get(list.indexOf(x)) ;
    }

    System.out.println(letterArray) ;
    }

    // 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++] = list.get(shuffledList.indexOf(x)) ;
    }

    System.out.println(letterArray) ;
    }

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

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

    // create a file to store you object'sinformation .
    FileInputStream fileIn = new FileInputStream("C:\\Users\\wwwha\\eclipse-workspace\\Serializer\\src\\userInfo.ser") ;
    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 ;
    System.out.println("UserInfo loaded .") ;
    }

    private void quit() throws IOException {

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

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

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

    System.out.println("UserInfo 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() ;
    }
    }

  • @ΒαγγΣτανιος
    @ΒαγγΣτανιος 4 роки тому +4

    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.

  • @Ahmed_TheProgramer
    @Ahmed_TheProgramer Рік тому +2

    for encrypt method I think it will be better if we wrote it like this:
    private void encrypt(){
    System.out.print("
    Enter 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
      @Ahmed_TheProgramer Рік тому +1

      and of course for decrypt method it will be the same except we will replace shuffledList with list and list with shuffledList

    • @Clockwerk777
      @Clockwerk777 10 місяців тому +1

      You actually dont need the list.indexOf part. You could just write:
      letters[i] = shuffledList.get(line.charAt(i) - 32);
      Since the ASCII character value at index of i in line is 32 ahead of where it positionally is in list/shuffledList. This way, you don't need to hunt thru list to find the index of that character.

    • @Ahmed_TheProgramer
      @Ahmed_TheProgramer 10 місяців тому

      @@Clockwerk777 thank you for your comment

  • @TyyylerDurden
    @TyyylerDurden 2 місяці тому

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

  • @roryodonoghue3286
    @roryodonoghue3286 3 роки тому +1

    Excellent. Learned a lot. Thank You, Sir

  • @meksudbuser9624
    @meksudbuser9624 Рік тому

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

  • @rodelioliwag7627
    @rodelioliwag7627 2 роки тому

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

  • @McMeil
    @McMeil 3 роки тому +1

    so easy and so necessery)
    very usefull, thnx

  • @Mrcodeuniverse
    @Mrcodeuniverse 5 місяців тому

    😊😊😊

  • @furkanyldz0
    @furkanyldz0 3 місяці тому

    looks like nobody has noticed the portal reference lol

  • @joecucco4132
    @joecucco4132 3 роки тому

    How do i get the ASCII table to show up in my key? Stuck at 19:44

    • @riyamandal7667
      @riyamandal7667 2 роки тому

      I think it's showing all the chars on our keyboard

    • @GKirChhOff
      @GKirChhOff 2 роки тому +1

      You don't get to print the whole ASCII table. He only prints the printable characters from that table. (32-126). The rest are special characters used by the Operating System.

  • @kingcrashplays
    @kingcrashplays 3 роки тому +2

    You should do a GUI version

    • @aliasgharkhoyee9501
      @aliasgharkhoyee9501 3 роки тому +1

      Once you get the basics of GUI (using other videos on this channel), it's simple to convert this command line code to GUI.

    • @sacmarv8997
      @sacmarv8997 2 роки тому

      can you please post the GUI version code

  • @kurtdecena8681
    @kurtdecena8681 Рік тому

    may i ask what software you used in this video? for java ofcourse

  • @maxwong1768
    @maxwong1768 2 роки тому +2

    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 .
    // Arrays.asList(arrayName).indexOf(element)
    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++] = shuffledList.get(list.indexOf(x)) ;
    }
    System.out.println(letterArray) ; // array cannot be print directly as only string representation will be shown , except char array .
    }

  • @it04ameekanazreen59
    @it04ameekanazreen59 2 роки тому

    Thanks!! this was very helpful.

  • @Geno420
    @Geno420 2 роки тому

    my bro, thanks for this java tutorial

  • @sreeshakv5405
    @sreeshakv5405 2 роки тому

    Nice

  • @desmiles1567
    @desmiles1567 3 роки тому +1

    Another masterclass bro

  • @alhabib3031a
    @alhabib3031a Рік тому

    My friend, I want to know the basics in order, lesson by lesson, to reach a professional level in java, put it in a post or video, because I completed the data structures, but I am not at the level

  • @nehalayaaz9406
    @nehalayaaz9406 4 роки тому +2

    You are the best 🙏

  • @rent2ownnz
    @rent2ownnz Рік тому

    I cannot see the code listed in the description as mentioned in the video. I really liked this and did it (well I thought I did) but it will not launch at all. eclipse just says "The Selection cannot be launched, and there are no recent launches." I have not freakin idea what this means or how to fix it. But Thanks anyway. Actually I just fixed it. If anyone else is having this issue and a newbie like me.... select the main java tab (not the Encryption program ) tab and run the java script from there

    • @rent2ownnz
      @rent2ownnz Рік тому

      Wow - I actually got this to work in the end - took me an extra hour to debug my errors and it works well. now I just need to figure our how to convert this same theory into 1 - a GUI and 2 - instead of using ASCII as the code base I want to use the Blockchain BIP39 reference chart

  • @michaelayo2
    @michaelayo2 3 роки тому +1

    wherre's the code? Bro?

  • @murrayKorir
    @murrayKorir 3 роки тому

    very very awesome🤗

  • @mahdib9361
    @mahdib9361 4 роки тому

    Amazing
    thanks for this tuturial

  • @subhashree8887
    @subhashree8887 3 роки тому

    So good to have this video

  • @abdelkaderouhssain3243
    @abdelkaderouhssain3243 2 роки тому

    What a good tutorial thanks!!

  • @alooyeg
    @alooyeg 3 роки тому

    my name is in the key that was auto generated at 30:21

  • @pconairofficial4333
    @pconairofficial4333 2 роки тому +1

    GReat

  • @eugenezuev7349
    @eugenezuev7349 6 місяців тому

    as always the stuff is awesome

  • @erfan_rad
    @erfan_rad 3 роки тому +1

    Github link for source code please.

  • @ah1122bi
    @ah1122bi 4 роки тому +1

    can you put the source code? please

  • @zari_723
    @zari_723 2 роки тому

    have a nice day

  • @anonimanonim1223
    @anonimanonim1223 2 роки тому

    Great video.

  • @skillR-243
    @skillR-243 3 роки тому

    Thanks you

  • @nawfalnjm5699
    @nawfalnjm5699 3 роки тому

    thank you !

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 роки тому

    Thanks

  • @sewek1513
    @sewek1513 2 роки тому

    Thanks a lot!

  • @zainabfahim6170
    @zainabfahim6170 2 роки тому

    Thank youuu

  • @rke4007
    @rke4007 2 роки тому

    3:28

  • @kemann3815
    @kemann3815 3 роки тому +1

    Best of da best!

  • @pejko89
    @pejko89 2 роки тому

    I feel like a magician :P

  • @kamoshira
    @kamoshira Рік тому

    Thanks bro

  • @tonalddrump9391
    @tonalddrump9391 4 роки тому

    I wanna donate!

    • @srinicnu5283
      @srinicnu5283 3 роки тому

      mr hacker donte to whom and for what

  • @robertzdeb8103
    @robertzdeb8103 3 роки тому

    very nice!

  • @chamodmaduwantha5776
    @chamodmaduwantha5776 3 роки тому

    Thnk u bro

  • @muhammadamir9799
    @muhammadamir9799 3 роки тому

    please provide the code in comment

  • @aliasqar5379
    @aliasqar5379 Рік тому

    Hello World ;)

  • @gerhardmorozov6613
    @gerhardmorozov6613 3 роки тому

    Nice video Bro!

  • @MO-dd3cs
    @MO-dd3cs 4 роки тому

    java.lang.classnotfoundexception
    Solution, professor

    • @BroCodez
      @BroCodez  4 роки тому

      Your classpath may be broken, try (in Eclipse):
      Run -> Run configurations -> select the class that contains the main() method for your project -> Run
      This error occurs when the JVM can't find your class when it compiles

    • @MO-dd3cs
      @MO-dd3cs 4 роки тому

      He does not work with me, can I thank you?

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Рік тому

  • @signx3287
    @signx3287 2 роки тому

    Tnx Bro!

  • @gautamsharma314
    @gautamsharma314 3 роки тому +1

    Bro why you not post coding in comments 😒

    • @ottttoooo
      @ottttoooo 3 роки тому +11

      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 list;
      private ArrayList 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("********************************************");
      System.out.println("What do you want to do?");
      System.out.println("(N)ewKey,(G)etKey,(E)ncrypt,(D)ecrypt,(Q)uit");
      char response = Character.toUpperCase(scanner.nextLine().charAt(0));

      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

    • @howong8561
      @howong8561 3 роки тому

      @@ottttoooo Thanks!!

    • @rachelgreen9754
      @rachelgreen9754 Рік тому

      He has

  • @zstar8397
    @zstar8397 Рік тому

    Hey hope you are doing alright just I wanna say that
    GOD loved the world so much he sent his only begotten
    son Jesus to die a brutal death for us so that we can have eternal life and we can all accept this amazing gift this by simply trusting in Jesus, confessing that GOD raised him from the dead, turning away from your sins and forming a relationship with GOD.