|
plz give me decryption code for following progrm.....
import java.io.*;
import java.util.Scanner;
import java.util.Random;
import java.lang.Math.*;
class Diffie_Hellman{
public static void main(String[] args) {
Random rand = new Random();
Scanner in = new Scanner(System.in);
System.out.println("Enter any prime number");
int n = in.nextInt();
int g1, g2;
int pb1,pb2,pa1,pa2;
int e1, e2;
int i;
int bk1, bk2, ak1, ak2;
System.out.println("Enter valu of g");
g1= in.nextInt();
g2= in.nextInt();
System.out.println("Enter valu of e");
e1= in.nextInt();
e2= in.nextInt();
int na = rand.nextInt(5)+2;
if(na%2!=0)
na=na+1;
int nb = rand.nextInt(5)+2;
if(nb%2!=0)
nb=nb+1;
System.out.println("Private key of Alice: "+ na + " \n Private key Bob " +nb + " ");
//int pa = (na * g)
// int pb = (nb * g)
int p = na/2;
int g11= g1;
int g22= g2;
for(i=1; i<= p ; i++)
{
double m1=(3*g11*g11+ e1);
double m2= 2*g22;
double inverse=0;
inverse = (Math.pow(m2,-1))%n;
double m =(na*(m1*inverse))%n;
double x = (m*m +(n-(2*g11)))%n;
double y= (m*(x+(n-g11))+g22)%n;
g11=(int)x;
g22=(int)y;
}
if(na%2 == 0)
{
pa1=g11;
pa2=g22;
System.out.println("Alice's Public key is :" +pa1+ " " +pa2 );
}
else
{
double m = (g2-g22)/(g1-g11);
double x3 = (m*m+(n-g1)+(n-g11))%n;
double y3 = (m*(x3+(n-g11)) + g22)%n;
pa1= g11+(int)x3;
pa2= g22+(int)y3;
System.out.println("Alice's Public key is :" +pa1+ " " +pa2 );
}
p = nb/2;
g11= g1;
g22= g2;
for(i=1; i<= p ; i++)
{
double m1=(3*g11*g11+ e1);
double m2= 2*g22;
double inverse=0;
inverse = (Math.pow(m2,-1))%n;
double m =(na*(m1*inverse))%n;
double x = (m*m +(n-(2*g11)))%n;
double y= (m*(x+(n-g11))+g22)%n;
g11=(int)x;
g22=(int)y;
}
if(nb%2 == 0)
{
pb1=g11;
pb2=g22;
System.out.println("Bob's Public key is :" +pb1+ " " +pb2 );
}
else
{
double m = (g2-g22)/(g1-g11);
double x3 = (m*m+(n-g1)+(n-g11))%n;
double y3 = (m*(x3+(n-g11)) + g22)%n;
pb1= g11+(int)x3;
pb2= g22+(int)y3;
System.out.println("Bob's Public key is :" +pb1+ " " +pb2 );
}
System.out.println("******Generating shared key for Alice *******");
p = na/2;
g11= pb1;
g22= pb2;
for(i=1; i<= p ; i++)
{
double m1=(3*g11*g11+ e1);
double m2= 2*g22;
double inverse=0;
inverse = (Math.pow(m2,-1))%n;
double m =(na*(m1*inverse))%n;
double x = (m*m +(n-(2*g11)))%n;
double y= (m*(x+(n-g11))+g22)%n;
g11=(int)x;
g22=(int)y;
}
if(na%2 == 0)
{
ak1=g11;
ak2=g22;
System.out.println("Alice's shared key is :" +ak1+ " " +ak2 );
}
else
{
double m = (g2-g22)/(g1-g11);
double x3 = (m*m+(n-g1)+(n-g11))%n;
double y3 = (m*(x3+(n-g11)) + g22)%n;
ak1= g11+(int)x3;
ak2= g22+(int)y3;
System.out.println("Alice's shared key is :" +ak1+ " " +ak2 );
}
System.out.println("*****Generation of shared key for Bob*****");
p = nb/2;
g11= pa1;
g22= pa2;
for(i=1; i<= p ; i++)
{
double m1=(3*g11*g11+ e1);
double m2= 2*g22;
double inverse=0;
inverse = (Math.pow(m2,-1))%n;
double m =(na*(m1*inverse))%n;
double x = (m*m +(n-(2*g11)))%n;
double y= (m*(x+(n-g11))+g22)%n;
g11=(int)x;
g22=(int)y;
}
if(nb%2 == 0)
{
bk1=g11;
bk2=g22;
System.out.println("Bob's shared key is :" +bk1+ " " +bk2 );
}
else
{
double m = (g2-g22)/(g1-g11);
double x3 = (m*m+(n-g1)+(n-g11))%n;
double y3 = (m*(x3+(n-g11)) + g22)%n;
bk1= g11+(int)x3;
bk2= g22+(int)y3;
System.out.println("Bob's shared key is :" +bk1+ " " +bk2 );
}
System.out.println("Enter Your Messgae");
int msg1 = in.nextInt();
int msg2= in.nextInt();
int k= (bk1*bk2)%n;
System.out.println("Value of K is : " +k);
p = k/2;
if(p<0)
p=p*(-1);
g11= msg1;
g22= msg2;
for(i=1; i<=p ; i++)
{
double m1=(3*g11*g11+ e1);
double m2= 2*g22;
double inverse=0;
inverse = (Math.pow(m2,-1))%n;
double m =(na*(m1*inverse))%n;
double x = (m*m +(n-(2*g11)))%n;
double y= (m*(x+(n-g11))+g22)%n;
g11=(int)x;
g22=(int)y;
}
if(na%2 == 0)
{
pa1=g11;
pa2=g22;
System.out.println("C :" +pa1+ " " +pa2 );
}
else
{
double m = (g2-g22)/(g1-g11);
double x3 = (m*m+(n-g1)+(n-g11))%n;
double y3 = (m*(x3+(n-g11)) + g22)%n;
int c1= g11+(int)x3;
int c2=g22+(int)y3;
pa1= (int)c1;
pa2= (int)c2;
System.out.println("C:" +pa1+ " " +pa2 );
}
//decryption code
}
}
|
|
|
|
|
|
Please learn something about finite fields before using them, otherwise fundamentally broken code (as you wrote) results.
|
|
|
|
|
I am trying to decrypt a text that is encrypted using a substitution cipher. It is simple substituion, so a one-to-one mapping. there are no spaces, and only lower caps letters. I am using the jakobsen's algorithm. I followed the algorithm, and I implemented both arrays in which we will compute the distance. However, the code is providing erroneous answers, I don't know why. The Digrams generated from the input text are correctly places. This is the link to the paper that explains the algorithm and provides the digram frequency
http://www.cs.sjsu.edu/~stamp/RUA/homophonic.pdf[^]
|
|
|
|
|
|
Creating the thread inside the constructor is it good practice or not?
If not hen why??
|
|
|
|
|
Not a good practice. Constructors should be used to instantiate an object of a class, not to control the overall application.
|
|
|
|
|
It's fine as long you know what you doing
|
|
|
|
|
Hi everyone. I'm new to this and could use a bit of help. Is it possible to call the "timesTable" method from inside the "main" as its coded so far? I've tried a few things inside the main like timesTable t = new timesTable, and even adding a class name and using "class".timesTable, and so on, but nothing has worked. The code I have so far is:
package timestableapp;
/**
*
* @author
*/
import java.util.Scanner;
public class TimesTableApp {
/**
* @param args the command line arguments
*/
static void timesTable(int x)
{
System.out.println("Multiplication Table of "+ 4);
for(x=0; x<=12; x++)
{
System.out.println(x+" * "+4+" = "+(4*x));
}
}
public static void main(String args[])
{
int n, c;
System.out.println("Enter an integer to print it's multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");
for ( c = 0 ; c <= 12 ; c++ )
System.out.println(n+"*"+c+" = "+(n*c));
}
}
|
|
|
|
|
|
Thanks. I try to break my assignment down in separate steps and I'm a stickler for making every attempt to figure it out on my own. But this time (though each step worked) I just couldn't seem to connect them.
|
|
|
|
|
It's one of those things that catches most people out when they start writing code based on classes.
|
|
|
|
|
I'm having problem adding an image to my applet. Can someone tell me what I did wrong??
Errors in Second Class
URL Cannot Be resolved to a type. Line 10/14/15
First Class
import java.applet.*;
import java.awt.*;
public class MainClass extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
Thread thread = new Thread(this);
boolean running=true;
Player p;
Image dbImage;
Graphics dbg;
public void init()
{
setSize(800,800);
p = new Player(this);
}
public void start(){thread.start();}
public void destroy(){running=false;}
public void stop(){running=false;}
public void run()
{
while(running)
{
repaint();
p.update(this);
try
{
Thread.sleep(5);
}
catch(InterruptedException e)
{
System.out.println("ERROR HAS OCCURED");
}
}
}
public void update(Graphics g){
dbImage = createImage(400,400);
dbg = dbImage.getGraphics();
paint(dbg);
g.drawImage(dbImage,0,0,this);
}
public void paint(Graphics g){
p.paint(g, this);
}
}
Second Class
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Player implements KeyListener {
private int x=200;
private int y=200;
private int radius=20;
private int velX=0;
private int velY=0;
private URL url;
private Image player;
public Player(MainClass mc){
url = mc.getDocumentBase();
player = mc.getImage(url,"mario.jpg");
}
public void update(MainClass mc){
mc.addKeyListener(this);
x+=velX;
y+=velY;
}
public void paint(Graphics g, MainClass mc){
g.drawImage(player, x, y, mc);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_RIGHT:{
velX=5;
break;
}
case KeyEvent.VK_LEFT:{
velX=-5;
break;
}
case KeyEvent.VK_UP:{
velY=-5;
break;
}
case KeyEvent.VK_DOWN:{
velY=5;
break;
}
}
}
@Override
public void keyReleased(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_RIGHT:{
velX=0;
break;
}
case KeyEvent.VK_LEFT:{
velX=-0;
break;
}
case KeyEvent.VK_UP:{
velY=-0;
break;
}
case KeyEvent.VK_DOWN:{
velY=0;
break;
}
}
}
}
|
|
|
|
|
|
I think you need to make object in main class also "URL url "and in init to call it.. maybe I dont know try . GoodLuck
|
|
|
|
|
y
modified 7-Mar-15 16:12pm.
|
|
|
|
|
VietAnh162 wrote: Can someone tell me what I did wrong? Yes, you forgot to explain the full details of the problem and where in your code it is occurring.
|
|
|
|
|
How can used mac theme in java ui manager?? please help.
|
|
|
|
|
|
Hello I'm Den,
i want to create a simple software architekture with Design Pattern, but i don't Know how i can beginn, how i can do that. Please can Anbody help me Please...
Thank u
Den
|
|
|
|
|
Question is phrased poorly or you need to start learning how to code before you attempt to even understand what patterns and architectures are.
But if you really want an answer then the FIRST step is to gather the business requirements for the enterprise. THEN you create an architecture. THEN you create designs (plural since architecture represent complex functionality) and SOME of those might explicitly refer to one or more patterns.
|
|
|
|
|
can you send the program for very fast decision tree algorithm
|
|
|
|
|
|
package exercise12;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Exercise12 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String ticket="";
String temp;
int Ticket, Remainder;
char[] numb=new char[6];
System.out.print("Please enter the ticket number : ");
temp= String.valueOf(input.nextInt());
Boolean answer;
for(int i=0; i<6;i++){
numb[i]=temp.charAt(i);
if (i != 5){ticket=ticket+numb[i]; }
}
Ticket=Integer.parseInt(ticket);
temp=String.valueOf(numb[5]);
Remainder=Integer.parseInt(temp);
if (Ticket%7==Remainder){answer=true;
}
else{answer=false;
}
JOptionPane.showMessageDialog(null,answer.toString().toUpperCase());
}
}
modified 5-Mar-15 3:08am.
|
|
|
|
|
This looks more complex than it needs to be. If I understand correctly you want to verify that the last digit equals the remainder when the 5-digit value is divided by 7, which could be something like:
int ticket = input.nextInt();
int remainder = ticket % 10;
ticket /= 10;
int modulo = ticket % 7;
if (modulo == remainder)
|
|
|
|