Archiwa tagu: Szyfrowanie

Java Szyfrowanie DES/ECB/NoPadding

package list1;
 
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
 
public class CW1 {
	public static byte[] getKey(int bytes) throws NoSuchAlgorithmException{
		KeyGenerator kGen = KeyGenerator.getInstance("DES");
		byte[] key = new byte[bytes];
		int i = 0;
		while(i < bytes){
			kGen.init(56);		
			SecretKey sKey = kGen.generateKey();
			byte[] rawKey = sKey.getEncoded();
			for(int j = 0; (j < rawKey.length)&(j+i < key.length) ; j++){
				key[j+i] = rawKey[j];
			}
			i = i + 7;
		}
		return key;
	}
 
	public static void main(String[] args) throws NoSuchAlgorithmException,
			NoSuchPaddingException, InvalidKeyException,
			IllegalBlockSizeException, BadPaddingException {
		/*
		 * d??ugo??? wiadomo??? musi by? podzielna przez 8
		 * ??eby mo??na by??o u??y? NoPadding
		 */
		byte[] input = "WiadomoscDoZakodowania!!".getBytes();
		byte[] encrypted = null;
		byte[] decrypted = null;
 
		KeyGenerator kGen = KeyGenerator.getInstance("DES");
		SecretKey sKey = kGen.generateKey();
		byte[] keyBytes = sKey.getEncoded();
 
		Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
 
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "DES"));
		encrypted = cipher.doFinal(input);
 
		cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, "DES"));
		decrypted = cipher.doFinal(encrypted);
 
		print(input);
		print(encrypted);
		print(decrypted);
 
		System.out.println(Arrays.equals(input, decrypted));
	}
 
	public static void print(byte[] b) {
		System.out.println(new String(b));
		System.out.println("Length: " + b.length * 8);
		System.out.println("---------------");
	}
}

Java szyfrowanie AES/ECB/PKCS5Padding

package list1;
 
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
 
public class CW2 {
 
	public static void main(String[] args) throws NoSuchAlgorithmException,
			NoSuchPaddingException, InvalidKeyException,
			IllegalBlockSizeException, BadPaddingException {
		byte[] input = "Wiadomosc do zakodowania!".getBytes();
 
		KeyGenerator kGen = KeyGenerator.getInstance("AES");
		kGen.init(128);
 
		SecretKey sKey = kGen.generateKey();
 
		byte[] rawKey = sKey.getEncoded();
 
		SecretKeySpec sKeySpec = new SecretKeySpec(rawKey, "AES");
 
		// algorytm AES, tryb ECB, dope??nianie w PCKS#5
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
 
		cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
 
		byte[] encrypted = cipher.doFinal(input);
 
		cipher.init(Cipher.DECRYPT_MODE, sKeySpec);
 
		byte[] decrypted = cipher.doFinal(encrypted);
 
		print(input);
		print(encrypted);
		print(decrypted);
 
//		System.out.println(MessageDigest.isEqual(input, decrypted));
		System.out.println(Arrays.equals(input, decrypted));
	}
 
	public static void print(byte[] b){
		System.out.println(new String(b));
		System.out.println("Length: " + b.length * 8);
		System.out.println("---------------");
	}
}

Java Szyfrowanie DES/ECB/SKCS5Padding

package main;
 
import java.security.NoSuchAlgorithmException;
 
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
 
public class Main {
	public static void main(String args[])
	{
 
		try {
 
		byte[] teskJawny = "Teskt jawny".getBytes();
 
		byte[] klucz =     "klucz".getBytes();
 
		System.out.println(XORCoderToString(XORcoder(teskJawny, klucz), klucz));
 
 
 
 
		byte[] keyBytes = null;
		SecretKeySpec key = new SecretKeySpec(keyBytes,"DES");
 
 
			Cipher szyfr = Cipher.getInstance("DES/ECB/SKCS5Padding");
 
 
 
 
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
 
	}
 
	/**
	 * Metoda wykonuje operacje XOR na podanych tablicach i zwraca wynik w nowej tablicy 
	 * @param msg tablica wiadomosci
	 * @param key tablica klucza
	 * @return tablica po operacji XOR
	 */
	public static byte[] XORcoder(byte[] msg, byte[] key) {
 
	byte[] ret = new byte[msg.length];
 
 
		for(int i=0; i<msg.length; i++)
			ret[i] = (byte) (msg[i]^key[i%key.length]);
 
		return ret;
 
	}
 
	/**
	 * Metoda wykonuje operacje XOR i zwraca wynik w postaci tekstu
	 * @param msg testk do operacji XOR
	 * @param key klucza do operacji XOR
	 * @return teskt otrzymany po operacji XOR
	 */
	public static String XORCoderToString(byte[] msg, byte[] key)
	{
		StringBuilder sb= new StringBuilder();
		byte[] tmp =  XORcoder(msg, key);
		for (byte b : tmp) {
			sb.append((char)b);
		}
 
		return sb.toString();
	}
}

Szyfrowanie XOR One Time Pad

Id?? szyfrowania one time pad jest to, ??e do ka??dej wiadomo??ci generujemy jednorazowy klucz d??ugo??ci tej wiadomo??ci. Jest to spos??b na szyfrowanie bardzo bezpieczne kr??tkich wiadomo??ci.

Jedynym mankamentem samej metody jest fakt ??e przed przes??aniem zaszyfrowanej wiadomo??ci musimy przes??a? r??wnie?? klucz.

Implementacja

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
void print_string(char string[], int len) {
int i=0;
while(i<len){
 cout<<string[i];
 i++;
}
}
int main(int argc, char *){
char key[20], msg[50], crypt[50], decrypt[50];
cout<<"podaj dlugosc klucza(0 to dl. wiadomosci): ";
int key_len;
cin>>key_len;
cout<<"podaj wiadomosc: ";
cin>>msg;
if(key_len==0) {
 key_len=strlen(msg);
}
int msg_len = strlen(msg);
srand((unsigned)time(0));
for(int i=0; i<key_len; i++){
 key[i]=(char)(rand()%256);
}
cout<<"\n\n\nklucz: ";
print_string(key, key_len);
cout<<"\tdlugosc klucza: "<<strlen(key)-1<<"\tdlugosc wiadomosci: "<<strlen(msg)<<endl;
int i=0;
while(i<msg_len){
 int cur_key_char=0;
 if(i>strlen(key))
  cur_key_char = i % key_len ;
 crypt[i] = msg[i]^ key[cur_key_char];
 i++;
}
cout<<"\n\ncrypted:\n";
print_string(crypt, msg_len);
print_string(decrypt, msg_len);
int crypt_len = strlen(crypt);
cout<<"\n\nshould be:\n";
print_string(msg, msg_len);
i=0;
while(i<msg_len){
 int cur_key_char=0;
 if(i>strlen(key))
  cur_key_char = i % key_len ;
 decrypt[i] = crypt[i] ^ key[cur_key_char];
 i++;
}
cout<<"\n\ndecrypted:\n";
print_string(decrypt, msg_len);
cout<<"\n\n\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}

Do pobrania r??wnie?? wersja skompilowana plus ??r??d??o: Onetimepad XOR