GeneratorUndModul gM= new GeneratorUndModul();
DiffieHellmanKlasse alice, bob;
public void bGActionPerformed(ActionEvent evt) {
tG.setText(gM.newGenerator().toString());
}
public void bMActionPerformed(ActionEvent evt) {
tP.setText(gM.newModul().toString());
}
public void gUMActionPerformed(ActionEvent evt) {
gM.setModul(tP.getText());
gM.setGenerator(tG.getText());
}
public void bAActionPerformed(ActionEvent evt) {
tA.setText(""+ (int) (Math.random()*10000));
}
public void bAPublickeyActionPerformed(ActionEvent evt) {
alice =new DiffieHellmanKlasse(gM.getModul(), gM.getGenerator(), Integer.parseInt(tA.getText()));
tOA.setText(alice.getÖffentlichenSchlüssel().toString());
}
public void bBActionPerformed(ActionEvent evt) {
tB.setText(""+ (int) (Math.random()*10000));
}
public void bBPublicKeyActionPerformed(ActionEvent evt) {
bob =new DiffieHellmanKlasse(gM.getModul(), gM.getGenerator(), Integer.parseInt(tB.getText()));
tOB.setText(bob.getÖffentlichenSchlüssel().toString());
}
public void bSActionPerformed(ActionEvent evt) {
bob.setÖffentlichenSchlüssel(alice.getÖffentlichenSchlüssel());
alice.setÖffentlichenSchlüssel(bob.getÖffentlichenSchlüssel());
tSA.setText(alice.getSymmetrischenSchlüssel().toString());
tSB.setText(bob.getSymmetrischenSchlüssel().toString());
}
import java.math.BigInteger;
import java.util.*;
public class GeneratorUndModul {
BigInteger p, g;
public GeneratorUndModul(){
p=randomPrime(128);
g=randomPrime(96);
}
public BigInteger randomPrime(int bits){
BigInteger pr= new BigInteger(bits,10000,new Random());
return pr;
}
public void setModul(String m){
p= new BigInteger(m);
}
public void setGenerator(String g){
this.g= new BigInteger(g);
}
public BigInteger getModul(){
return p;
}
public String getModulAlsString(){
return p.toString();
}
public String getGeneratorAlsString(){
return g.toString();
}
public BigInteger getGenerator(){
return g;
}
public BigInteger newModul(){
p=randomPrime(96);
return p;
}
public BigInteger newModul(int bits){
p=randomPrime(bits);
return p;
}
public BigInteger newGenerator(int bits){
g=randomPrime(bits);
return p;
}
public BigInteger newGenerator(){
p=randomPrime(128);
return p;
}
}
import java.math.BigInteger;
public class DiffieHellmanKlasse {
BigInteger p, g, A, B, S;
int a;
public DiffieHellmanKlasse(BigInteger p, BigInteger g, int a){
this.p=p; this.g=g; this.a=a;
}
public DiffieHellmanKlasse(){
}
public BigInteger getÖffentlichenSchlüssel(){
A=g.pow(a); A=A.mod(p);
return A;
}
public void setModul(BigInteger p){
this.p=p;
}
public void setGenerator(BigInteger g){
this.g=g;
}
public void setGeheimzahl(int a){
this.a=a;
}
public void setÖffentlichenSchlüssel(BigInteger B){
this.B= B;
}
public BigInteger getSymmetrischenSchlüssel (){
S=B.pow(a);
S=S.mod(p);
return S;
}
}