El-Gamal algoritmida maʼlumotni shifrlovchi dasturiy vositani ishlab chiqish



Download 1,52 Mb.
Sana15.06.2022
Hajmi1,52 Mb.
#672465
Bog'liq
Eshmamatov Obidjon kripto 4 - amal




O‘ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI
TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI

Kriptografiya 2”


fanidan


amaliy ish 4


El-Gamal algoritmida maʼlumotni shifrlovchi dasturiy vositani ishlab chiqish
Bajardi: Axborot xavfsizligi ta’lim yo‘nalishi
714-19-guruh
Eshmamatov Obidjon
talabaning F.I.Sh.

Qabul qildi: Mardiyev U




Toshkent 2021



package com.epam.esm;

import java.util.*;


import java.math.BigInteger;

public class ElGamal {

public static void main(String[] args) {

Scanner stdin = new Scanner(System.in);


Random r = new Random();

// Get user input for p.


System.out.println("Enter the approximate value of the prime number for your El Gamal key.");
BigInteger p = getNextPrime(stdin.next());

// Calculate a generator.


BigInteger g = getGenerator(p, r);

// We found a generator, so let's do the rest of it.


if (g != null) {

// Pick a secret a, not 0 or 1.


BigInteger a = new BigInteger(p.bitLength()+1, r);
a = a.mod(p);
if (a.equals(BigInteger.ZERO) || a.equals(BigInteger.ONE))
a = a.add(new BigInteger("2"));

// Calculate the corresponding public b.


BigInteger b = g.modPow(a, p);

// Print out our public keys.


System.out.println("Post p = "+p+" g = "+g+" b = "+b);

// When we send a message, the sender picks a random k.


// I make sure it's never 0 or 1.
BigInteger k = new BigInteger(p.bitLength()+1, r);
k = k.mod(p);
if (k.equals(BigInteger.ZERO) || k.equals(BigInteger.ONE))
k = k.add(new BigInteger("2"));

// Here, the sender starts calculating parts of the ciphertext that


// don't involve the actual message.
BigInteger c1 = g.modPow(k, p);
BigInteger c2 = b.modPow(k, p);

// Here we get the message from the user.


System.out.println("Please enter your message. It should be in between 1 and "+p);
BigInteger m = new BigInteger(stdin.next());

// Now, we can calculate the rest of the second ciphertext.


c2 = c2.multiply(m);
c2 = c2.mod(p);

// Print out the two ciphertexts.


System.out.println("The corresponding cipher texts are c1 = "+c1+" c2 = "+c2);

// First, determine the inverse of c1 raised to the a power mod p.


BigInteger temp = c1.modPow(a,p);
temp = temp.modInverse(p);

// Print this out.


System.out.println("Here is c1^ -a = "+temp);

// Now, just multiply this by the second ciphertext


BigInteger recover = temp.multiply(c2);
recover = recover.mod(p);

// And this will give us our original message back!


System.out.println("The original message = "+recover);
}

// My sorry message!


else
System.out.println("Sorry, a generator for your prime couldn't be found.");

}


// Incrementally tries each BigInteger starting at the value passed
// in as a parameter until one of them is tests as being prime.
public static BigInteger getNextPrime(String ans) {
BigInteger one = new BigInteger("1");
BigInteger test = new BigInteger(ans);
while (!test.isProbablePrime(99))
test = test.add(one);
return test;
}

// Precondition - p is prime and it's reasonably small, say, no more than


// 5,000,000. If it's larger, this method will be quite
// time-consuming.
// Postcondition - if a generator for p can be found, then it is returned
// if no generator is found after 1000 tries, null is
// returned.
public static BigInteger getGenerator(BigInteger p, Random r) {

int numtries = 0;

// Try finding a generator at random 100 times.
while (numtries < 1000) {

// Here's what we're trying as the generator this time.


BigInteger rand = new BigInteger(p.bitLength()+1,r);
rand = rand.mod(p);

BigInteger exp = BigInteger.ONE;


BigInteger next = rand;

// These are bad choices!


if (next.equals(BigInteger.ZERO) || next.equals(BigInteger.ONE))
continue;

// We exponentiate our generator until we get 1 mod p.


while (!next.equals(BigInteger.ONE)) {
next = (next.multiply(rand)).mod(p);
exp = exp.add(BigInteger.ONE);
}

// If the first time we hit 1 is the exponent p-1, then we have


// a generator.
if (exp.equals(p.subtract(BigInteger.ONE)))
return rand;

numtries++;


}

// None of the 1000 values we tried was a generator.


return null;

}

}










Download 1,52 Mb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish