🔒 Closed Cipher sample 01

Status
Not open for further replies.

GieLou

Leecher
Hello po Bago pa po ako dito, share ko lang po yung basic program (own version of Cipher Decryption with 3 shifts) ko sana makatulong :)

copy paste lang po ang code sa favorite ninyong IDE :)

Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package practice;

/**
*
* @author Frank
*/
public class Cipher {

     char[] array = {'a','b','c',
                     'd','e','f',
                     'g','h','i',
                     'j','k','l',
                     'm','n','o',
                     'p','q','r',
                     's','t','u',
                     'v','w','x',
                     'y','z'};
     // a word
     private String decrypted = "";
     // a code
     private String incrypted = "";
  
     public Cipher(){
         this("","");
     }
     public Cipher(String incrypted){
         this(incrypted, "");
     }
     public Cipher(String incrypted, String decrypted){
         this.incrypted = incrypted.toLowerCase();
         this.decrypted = decrypted.toLowerCase();
     }
     public void setDecrypted(String decrypted){
         this.decrypted = decrypted;
     }
     public String getDecrypted(){
         return this.decrypted;
     }
     public void setIncrypted(String incrypted){
         this.incrypted = incrypted;
     }
     public String getIncrypted(){
         return this.incrypted;
     }
  
     public String getDecryptedString(){
      
         for (int i = 0; i < decrypted.length(); i++) {
          
         }
      
         return "";
     }
     public String getEncryptedString(){
      
         for (int i = 0; i < incrypted.length(); i++) {
             // we're going to determine if the character is a letter.
             if (getCharacterIndex(incrypted.charAt(i)) != -1){
                // if si character index - 3 is negative
                 if (getCharacterIndex(incrypted.charAt(i)) - 3 < 0){
                     int itsIndex = (array.length ) + (getCharacterIndex(incrypted.charAt(i)) - 3);
                  
                     char real = array[itsIndex];
                  
                     decrypted+=real;
                 } else {
                     int itsIndex = getCharacterIndex(incrypted.charAt(i))- 3;
                  
                     decrypted+= array[itsIndex];
                 }
             } else {
                 decrypted+="-";
             }
         }
      
      
         return decrypted;
     }
     private int getCharacterIndex(char i){
      
         for (int j = 0; j < array.length; j++) {
             if (array[j]==i){
                 return j;
             }
         }
      
         return -1;
     }
  
}

Code:
// create lang po nang main class then add the data //sa contructor

// example

public class Sapmlpe_Class {

    public static void main(String[] args){
   
Scanner inp = new Scanner(System.in);

System.out.print("Enter any word: " );
Cipher cip = new Cipher(inp.nextLine());

System.out.println("Decrypted Cipher = " + cip.getEncryptedString());
 


   }
}
 
Status
Not open for further replies.

Similar threads

About this Thread

  • 0
    Replies
  • 1K
    Views
  • 1
    Participants
Last reply from:
GieLou

Trending Topics

Online now

Members online
1,217
Guests online
2,191
Total visitors
3,408

Forum statistics

Threads
2,285,566
Posts
29,031,301
Members
1,218,514
Latest member
trebskie
Back
Top