Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Caesar Cipher java source code.

843811Apr 24 2008 — edited Apr 24 2008
PLZ, what is the wrong her??


import java.io.*;
import java.util.*;
import java.util.Scanner;

public class Substitution
{
	
  public String str;
  public String k;
  
   // main function
   public static void main(String[] args) 
   {
   	
     Scanner scan = new Scanner(System.in);
 
    System.out.println("Enter character: ");
    String message=scan.nextLine();
 
     System.out.println("Enter key: ");
     String key=scan.nextLine();
  
     Substitution caes= new Substitution ();
     String encrypt = caes.translate(message,key);
     System.out.println("Encrypted: "+encrypt);
  
  

      String decrypt = caes.translate(encrypt,-1*key );
      
     System.out.println("Decrypted: "+decrypt);
     
        
   }
  
  //constractor
   public Substitution( ) { }
  
  //function of translate for encrypt and decrypt 
   public String translate (String str ,String k)
      {
         StringBuffer str1= new StringBuffer(str);
         String s = "abcdefghijklmnopqrstuvwxyz";
         String d="thesnowlayickpdfrvbg-----";
      
         for (int j=0;j<str.length();j++)
          for(int t=0;t<k.length();t++)
          for(int i=0;i<26;i++)
         
            if(str.charAt(j)==s.charAt(i) )
             if(d.charAt(t)==s.charAt(i))
             {    
              str1.setCharAt(j,s.charAt((i+t+26)%26));
              break;
             }
           return str1.toString();
     }
     
  
}

	 
Thank you
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 22 2008
Added on Apr 24 2008
1 comment
1,628 views