Java icon indicating copy to clipboard operation
Java copied to clipboard

Rail fence code added

Open ramanand-rv opened this issue 3 years ago • 1 comments

Describe your change:

  • [x] Add an algorithm?
  • [ ] Fix a bug or typo in an existing algorithm?
  • [ ] Documentation change?

References

#3522

Checklist:

  • [x] I have read CONTRIBUTING.md.
  • [x] This pull request is all my own work -- I have not plagiarized.
  • [x] I know that pull requests will not be merged if they fail the automated tests.
  • [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • [x] All new Java files are placed inside an existing directory.
  • [x] All filenames are in all uppercase characters with no spaces or dashes.
  • [x] All functions and variable names follow Java naming conventions.

Attaching the screenshot of the output. Request @siriak @yanglbme to review and merge. image

ramanand-rv avatar Oct 12 '22 23:10 ramanand-rv

import java.util.*; class RailFenceBasic{ int depth; String Encryption(String plainText,int depth)throws Exception { int r=depth,len=plainText.length(); int c=len/depth; char mat[][]=new char[r][c]; int k=0;

String cipherText="";

for(int i=0;i< c;i++) { for(int j=0;j< r;j++) { if(k!=len) mat[j][i]=plainText.charAt(k++); else mat[j][i]='X'; } } for(int i=0;i< r;i++) { for(int j=0;j< c;j++) { cipherText+=mat[i][j]; } } return cipherText; }

String Decryption(String cipherText,int depth)throws Exception { int r=depth,len=cipherText.length(); int c=len/depth; char mat[][]=new char[r][c]; int k=0;

String plainText="";

for(int i=0;i< r;i++) { for(int j=0;j< c;j++) { mat[i][j]=cipherText.charAt(k++); } } for(int i=0;i< c;i++) { for(int j=0;j< r;j++) { plainText+=mat[j][i]; } }

return plainText; } }

class RailFence{ public static void main(String args[])throws Exception { RailFenceBasic rf=new RailFenceBasic(); Scanner scn=new Scanner(System.in); int depth;

            String plainText,cipherText,decryptedText;
            
            System.out.println("Enter plain text:");
            plainText=scn.nextLine();
            
            System.out.println("Enter depth for Encryption:");
            depth=scn.nextInt();
           

cipherText=rf.Encryption(plainText,depth); System.out.println("Encrypted text is:\n"+cipherText);

            decryptedText=rf.Decryption(cipherText, depth);
            

System.out.println("Decrypted text is:\n"+decryptedText);

} } Screenshot (478)

bhoskarpooja avatar Oct 21 '22 10:10 bhoskarpooja

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Dec 26 '22 00:12 github-actions[bot]

Please reopen this pull request once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to seek help from our Gitter or ping one of the reviewers. Thank you for your contributions!

github-actions[bot] avatar Jan 03 '23 00:01 github-actions[bot]