Amaliy kriptografiya fanidan 1-amaliy mashg’ulot
Klassik shifrlash algoritmlarini Java dasturlash tilidan foydalanib yaratilgan dasturlari:
Sezar dastur kodi:
package Entity;
import java.util.Locale;
import java.util.Scanner;
public class Sezar {
public static final String ALIFBO= "abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args) {
Scanner str=new Scanner(System.in);
Scanner num=new Scanner(System.in);
String ochiqMatn;
int kalit;
String shifrMatn="";
System.out.print("Ochiq matnni kiriting: ");
ochiqMatn=str.nextLine();
System.out.print("Kalit uzunligini kiriting: ");
kalit=num.nextInt();
for (int i = 0; i < ochiqMatn.length(); i++) {
ochiqMatn=ochiqMatn.toLowerCase();
int i1 = ALIFBO.indexOf(ochiqMatn.charAt(i));
int i2 = (kalit + i1)%26;
shifrMatn+=ALIFBO.charAt(i2);
}
System.out.println();
System.out.println("shifrMatn = " + shifrMatn);
}
}
Dastur natijasi:
Vernam dastur kodi:
package Entity;
import java.util.Locale;
import java.util.Scanner;
public class Vernam {
public static void main(String[] args) {
String ochiqMatn, kalit;
String shifrmatn = "";
Scanner scanner=new Scanner(System.in);
System.out.print("ochiq matnni kiriting: ");
ochiqMatn=scanner.nextLine();
System.out.print("kalitni kiriting: ");
kalit=scanner.nextLine();
ochiqMatn=ochiqMatn.toUpperCase();
kalit=kalit.toUpperCase();
int shifr[] = new int[kalit.length()];
for (int i = 0; i < kalit.length(); i++) {
shifr[i] = ochiqMatn.charAt(i) - 'A'
+ kalit.charAt(i)
- 'A';
}
for (int i = 0; i < kalit.length(); i++) {
if (shifr[i] > 25) {
shifr[i] = shifr[i] - 26;
}
}
for (int i = 0; i < kalit.length(); i++) {
int x = shifr[i] + 'A';
shifrmatn += (char)x;
}
System.out.println("shifrMatn= "+ shifrmatn);
}
}
Dastur natijasi:
Venjiner usuli:
def Tekshirish(string, key):
key = list(key)
if len(string) == len(key):
return (key)
else:
for i in range(len(string) - len(key)):
key.append(key[i % len(key)])
return ("".join(key))
def Shifrlash(string, key):
Shifr = []
for i in range(len(string)):
x = (ord(string[i]) + ord(key[i])) % 26
x += ord('A')
Shifr.append(chr(x))
return ("".join(Shifr))
if __name__ == "__main__":
string = input("Enter the message: ")
keyword = input("Enter the keyword: ")
key = Tekshirish(string, keyword)
Shifr = Shifrlash(string, key)
print("SHifrlangan matn:", Shifr)
Dastur natijasi:
Dostları ilə paylaş: |