Click to See Complete Forum and Search --> : Encrypt


merxais
03-16-2005, 12:42 AM
/*
A company who wants to transmit data over the telephone, but they are concerned that their phones are trapped. All of their data transmitted as four digit integers. They have asked you to write a program that will encrypt their data so that it may be transmitted more securely. Your application should read a four digit integer entered by the user in an input dialog and encrypt it as follows: Replace each digit by the sum of that digit plus 7 modulus 10. The swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer.
*/

import javax.swing.*;

class encrypt
{
public static void main(String args[])
{
int x;

int n = Integer.parseInt(JOptionPane.showInputDialog("Enter 4 digit password:"));
while(n>0)
{
x=n%10;
n=n/10;

int a[] = new int[x];
int sum;
for(int i=0; i<=3;i++)
{
sum=(a[i]+7)%10;
for(int j=i+2;j<=3;j++)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
JOptionPane.showMessageDialog(null,sum);
System.exit(0);
}
}

i cant make it :( anyone help pls

merxais
03-16-2005, 06:04 AM
help~~

Khalid Ali
03-16-2005, 08:46 AM
Here is how it can work, but you must try to write it your self

boolean isGood = false;
int password=0;
int LIMIT = 9999; //make sure password is not more then 4 digits
String message = "Enter 4 digit password";
String strPassword = "";
strPassword = JOptionPane.showInputDialog(message);
//now convert numbers to an integer array
int[] passArr = new int[strPassword.length()];

//initialize int array
for(int x=0;x<passArr.length;x++){
passArr[x] = 0;
}
for(int n=0;n<passArr.length;n++){
int xx = Integer.parseInt(String.valueOf(strPassword.charAt(n)));

System.out.println("int = "+xx);
//now perform the encryption
//Replace each digit by the sum of that digit plus 7 modulus 10
//xx = (xx + 7)%10

}
//now swap the first digit with the third
//and swap the second digit with the fourth
int firstDigit = passArr[0];
int secnddDigit = passArr[1];
int thirdDigit = passArr[2];
int forthDigit = passArr[3];
//convert to string again for printing
String temp = "";
for (int z=0;z<passArr.length;z++){
temp+=passArr[z];
}
//now print the actual password that user entered and the encrypted one
System.out.println("\nOriginal Password Entered = "+strPassword);
System.out.println("Encrypted Password = "+temp );

merxais
03-16-2005, 12:34 PM
import javax.swing.*;

class encrypt
{
public static void main(String args[])
{
int a[]=new int[4];
int b[]=new int[4];
int n=Integer.parseInt(JOptionPane.showInputDialog("Enter 4 digit integers:"));
int i=0;
String s=" ";

a[i]=n/1000;
a[i+1]=(n-(a[i]*1000))/100;
a[i+2]=(n-(a[i]*1000)-(a[i+1]*100))/10;
a[i+3]=n-(a[i]*1000)-(a[i+1]*100)-(a[i+2]*10);

int x=a[i];
a[i]=a[i+2];
a[i+2]=x;

int y=a[i+1];
a[i+1]=a[i+3];
a[i+3]=y;

for(int j=0;j<=3;j++)
{
b[j]=(a[j]+7)%10;
s=s+b[j];
}

JOptionPane.showMessageDialog(null,s);
System.exit(0);
}
}