Akmal Muzafarov Kriptografiya


Download 56.61 Kb.
Sana05.12.2020
Hajmi56.61 Kb.

Akmal Muzafarov


Kriptografiya

7-amali ishi






Akmal



Mavzu: RC4 shifrlash algoritmi asosida ma’lumotlarni shifrlash va deshifrlash dasturini yaratish

Ishdan maqsad: RC4 shifrlash algoritmi haqida nazariy va amaliy bilim ko’nikmalarga ega bo’lish.

Nazariy qism. RC4–uzluksiz shifrlash algoritmi bo‘lib , u SSL(Secure Sockets Layer) pratakoli va WEP(simsiz tarmoqlarda xavfsizlikni ta’minlashda) keng foydalaniladi. RC4 uzluksiz shifrlash algoritmi Ron Rivest tomonidan 1987 yilda yaratilgan va shuning uchun RC4(Rivest Cipher 4) deb nomlangan.

Ishni bajarilish tartibi va qo’yilgan vazifa: O’z familiyangizni RC4 orqali shifrlansin va qadamma–qadam izohlansin. Shuningdek Delpi,Java,C++ va C# dasturlash tizimlaridan birida dasturiy ta’minot yaratilsin.

import java.io.UnsupportedEncodingException;


public class Main{


public static void main(String[] args) throws UnsupportedEncodingException {
RC4Util rc4Util = new RC4Util();
String encryStr = rc4Util.encryRC4String ("MuzafarovXayrullayevich", "Akmal", "UTF-8");
System.out.println ("Shifrmatn:" + encryStr);
String decryStr = rc4Util.decryRC4(encryStr, "Akmal");
System.out.println ("Deshifrlangan matn:" + decryStr);
}

}
class RC4Util {

public static String encryRC4String(String data, String key,String chartSet) throws UnsupportedEncodingException {
if (data == null || key == null) {
return null;
}
return toHexString(asString(encryRC4Byte(data, key,chartSet)));
}

public static byte[] encryRC4Byte(String data, String key,String chartSet) throws UnsupportedEncodingException {


if (data == null || key == null) {
return null;
}
if(chartSet==null||chartSet.isEmpty()) {
byte bData[] = data.getBytes();
return RC4Base(bData, key);
}else {
byte bData[] = data.getBytes(chartSet);
return RC4Base(bData, key);
}

}

public static String decryRC4(String data, String key) {


if (data == null || key == null) {
return null;
}
return new String(RC4Base(HexString2Bytes(data), key));
}

public static String decryRC4(byte[] data, String key) {


if (data == null || key == null) {
return null;
}
return asString(RC4Base(data, key));
}

private static String asString(byte[] buf) {


StringBuffer strbuf = new StringBuffer(buf.length);
for (int i = 0; i < buf.length; i++) {
strbuf.append((char) buf[i]);
}
return strbuf.toString();
}

private static byte[] initKey(String aKey) {


byte[] bkey = aKey.getBytes();
byte state[] = new byte[256];

for (int i = 0; i < 256; i++) {


state[i] = (byte) i;
}
int index1 = 0;
int index2 = 0;
if (bkey == null || bkey.length == 0) {
return null;
}
for (int i = 0; i < 256; i++) {
index2 = ((bkey[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
byte tmp = state[i];
state[i] = state[index2];
state[index2] = tmp;
index1 = (index1 + 1) % bkey.length;
}
return state;
}

private static String toHexString(String s) {


String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
String s4 = Integer.toHexString(ch & 0xFF);
if (s4.length() == 1) {
s4 = '0' + s4;
}
str = str + s4;
}
return str; // 0x means hex
}

private static byte[] HexString2Bytes(String src) {


int size = src.length();
byte[] ret = new byte[size / 2];
byte[] tmp = src.getBytes();
for (int i = 0; i < size / 2; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}

private static byte uniteBytes(byte src0, byte src1) {


char _b0 = (char) Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();
_b0 = (char) (_b0 << 4);
char _b1 = (char) Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}

private static byte[] RC4Base(byte[] input, String mKkey) {


int x = 0;
int y = 0;
byte key[] = initKey(mKkey);
int xorIndex;
byte[] result = new byte[input.length];

for (int i = 0; i < input.length; i++) {


x = (x + 1) & 0xff;
y = ((key[x] & 0xff) + y) & 0xff;
byte tmp = key[x];
key[x] = key[y];
key[y] = tmp;
xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;
result[i] = (byte) (input[i] ^ key[xorIndex]);
}
return result;
}
}

Natija:


Download 56.61 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2020
ma'muriyatiga murojaat qiling