DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Please help with enscryption program

1. Registered User
Join Date
Apr 2008
Posts
1

## Please help with enscryption program

The idea of this program is to read input from the keyboard and convert it into ciphertext using P^3 mod 33. First though CAP letters only must correspond to their numeric value (A=1, B = 2, C = 3...). For example if I input ANNE
output 1 5 5 26
Is the try catch in the right place or do I even need it?

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

public class RSA {

public static void main (String[] args)
{
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream);

String message = bufRead.readLine();

String encryptedString = "";
// convert str to a char array so I can access
// digit by digit

char[] digit = message.toCharArray();
// loop through the digit and print the position of each letter

for(int i = 0; i < digit.length; i++)
{
// init rank to 0
int rank = 0;
// test if letter from a to z
if(digit[i] >= 'a' && digit[i] <= 'z')
{
rank = digit[i] - 'a' + 1;
}
// test if letter from A to Z
if(digit[i] >= 'A' && digit[i] <= 'Z')
{
rank = digit[i] - 'A' + 1;
}
if(rank != 0) {
// raise to power of 3
int power = rank * rank * rank;
// modulo 33
power %= 33;
encryptedString = encryptedString + " " + power;
}

System.out.println("Please enter the message you wish to send: ");
System.out.println();
System.out.println ("You entered: " + message + "");
System.out.println ();
System.out.println ("Therefore plaintext is: " + message + "");
System.out.println ("Encrypted string : " + encryptedString + "");
}

{
System.out.println("Error reading line");
}
}
}

Thanks in advance

2. Senior Member
Join Date
Dec 2004
Location
San Bernardino County, California
Posts
1,468
I don't see a try ... catch in the code you've posted.

You need some organization to your code. Are you going to define methods in your class [stuff like a constructor, getUserInput, encrypt, decrypt], have some data fields, provide some structure other than trying to put all your code in a method main?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.