You are on page 1of 24

TP CRYPTOGRAPHIE

Safae El Houicha Ikram Charef


S130241629 S130036230

Sommaire:
 Chiffrement AES
 Chiffrement DES
 Chiffrement RSA
 Chiffrement de Hill
 Chiffrement de César
 Chiffrement de Vigénère
 Chiffrement Affine

Année Universitaire : 2023/2024


Algorithme AES:
package AES;

public class AESExample {


private static final int BLOCK_SIZE = 16; // Taille du
//bloc AES en octets
private static final int NB_ROUNDS = 10; // Nombre de
rondes pour AES-128
private static final int NB_WORDS = BLOCK_SIZE / 4; //
Nombre de mots dans une clé AES

private static final byte[][] SBOX = {


// Tableau de substitution S-box
{(byte) 0x63, (byte) 0x7c, (byte) 0x77, (byte)
0x7b,(byte) 0xf2, (byte) 0x6b, (byte) 0x6f, (byte) 0xc5,
(byte) 0x30, (byte) 0x01, (byte) 0x67, (byte) 0x2b, (byte)
0xfe, (byte) 0xd7, (byte) 0xab, (byte) 0x76},
{(byte) 0xca, (byte) 0x82, (byte) 0xc9, (byte) 0x7d,
(byte) 0xfa, (byte) 0x59, (byte) 0x47, (byte) 0xf0,
(byte) 0xad, (byte) 0xd4, (byte) 0xa2, (byte) 0xaf, (byte)
0x9c, (byte) 0xa4, (byte) 0x72, (byte) 0xc0},
{(byte) 0xb7, (byte) 0xfd, (byte) 0x93, (byte) 0x26,
(byte) 0x36, (byte) 0x3f, (byte) 0xf7, (byte) 0xcc,
(byte) 0x34, (byte) 0xa5, (byte) 0xe5, (byte) 0xf1, (byte)
0x71, (byte) 0xd8, (byte) 0x31, (byte) 0x15},
{(byte) 0x04, (byte) 0xc7, (byte) 0x23, (byte) 0xc3,
(byte) 0x18, (byte) 0x96, (byte) 0x05, (byte) 0x9a,
(byte) 0x07, (byte) 0x12, (byte) 0x80, (byte) 0xe2, (byte)
0xeb, (byte) 0x27, (byte) 0xb2, (byte) 0x75},
{(byte) 0x09, (byte) 0x83, (byte) 0x2c, (byte) 0x1a,
(byte) 0x1b, (byte) 0x6e, (byte) 0x5a, (byte) 0xa0,
(byte) 0x52, (byte) 0x3b, (byte) 0xd6, (byte) 0xb3, (byte)
0x29, (byte) 0xe3, (byte) 0x2f, (byte) 0x84},
{(byte) 0x53, (byte) 0xd1, (byte) 0x00, (byte) 0xed,
(byte) 0x20, (byte) 0xfc, (byte) 0xb1, (byte) 0x5b,
(byte) 0x6a, (byte) 0xcb, (byte) 0xbe, (byte) 0x39, (byte)
0x4a, (byte) 0x4c, (byte) 0x58, (byte) 0xcf},
{(byte) 0xd0, (byte) 0xef, (byte) 0xaa, (byte) 0xfb,
(byte) 0x43, (byte) 0x4d, (byte) 0x33, (byte) 0x85,
(byte) 0x45, (byte) 0xf9, (byte) 0x02, (byte) 0x7f, (byte)
0x50, (byte) 0x3c, (byte) 0x9f, (byte) 0xa8},
{(byte) 0x51, (byte) 0xa3, (byte) 0x40, (byte) 0x8f,
(byte) 0x92, (byte) 0x9d, (byte) 0x38, (byte) 0xf5,
(byte) 0xbc, (byte) 0xb6, (byte) 0xda, (byte) 0x21, (byte)
0x10, (byte) 0xff, (byte) 0xf3, (byte) 0xd2},
{(byte) 0xcd, (byte) 0x0c, (byte) 0x13, (byte) 0xec,
(byte) 0x5f, (byte) 0x97, (byte) 0x44, (byte) 0x17,
(byte) 0xc4, (byte) 0xa7, (byte) 0x7e, (byte) 0x3d, (byte)
0x64, (byte) 0x5d, (byte) 0x19, (byte) 0x73},
{(byte) 0x60, (byte) 0x81, (byte) 0x4f, (byte) 0xdc,
(byte) 0x22, (byte) 0x2a, (byte) 0x90, (byte) 0x88,
(byte) 0x46, (byte) 0xee, (byte) 0xb8, (byte) 0x14, (byte)
0xde, (byte) 0x5e, (byte) 0x0b, (byte) 0xdb},
{(byte) 0xe0, (byte) 0x32, (byte) 0x3a, (byte) 0x0a,
(byte) 0x49, (byte) 0x06, (byte) 0x24, (byte) 0x5c,
(byte) 0xc2, (byte) 0xd3, (byte) 0xac, (byte) 0x62, (byte)
0x91, (byte) 0x95, (byte) 0xe4, (byte) 0x79},
{(byte) 0xe7, (byte) 0xc8, (byte) 0x37, (byte) 0x6d,
(byte) 0x8d, (byte) 0xd5, (byte) 0x4e, (byte) 0xa9,
(byte) 0x6c, (byte) 0x56, (byte) 0xf4, (byte) 0xea, (byte)
0x65, (byte) 0x7a, (byte) 0xae, (byte) 0x08},
{(byte) 0xba, (byte) 0x78, (byte) 0x25, (byte) 0x2e,
(byte) 0x1c, (byte) 0xa6, (byte) 0xb4, (byte) 0xc6,
(byte) 0xe8, (byte) 0xdd, (byte) 0x74, (byte) 0x1f, (byte)
0x4b, (byte) 0xbd, (byte) 0x8b, (byte) 0x8a},
{(byte) 0x70, (byte) 0x3e, (byte) 0xb5, (byte) 0x66,
(byte) 0x48, (byte) 0x03, (byte) 0xf6, (byte) 0x0e,
(byte) 0x61, (byte) 0x35, (byte) 0x57, (byte) 0xb9, (byte)
0x86, (byte) 0xc1, (byte) 0x1d, (byte) 0x9e},
{(byte) 0xe1, (byte) 0xf8, (byte) 0x98, (byte) 0x11,
(byte) 0x69, (byte) 0xd9, (byte) 0x8e, (byte) 0x94,
(byte) 0x9b, (byte) 0x1e, (byte) 0x87, (byte) 0xe9, (byte)
0xce, (byte) 0x55, (byte) 0x28, (byte) 0xdf},
{(byte) 0x8c, (byte) 0xa1, (byte) 0x89, (byte) 0x0d,
(byte) 0xbf, (byte) 0xe6, (byte) 0x42, (byte) 0x68,
(byte) 0x41, (byte) 0x99, (byte) 0x2d, (byte) 0x0f, (byte)
0xb0, (byte) 0x54, (byte) 0xbb, (byte) 0x16}
};

private static final byte[] RCON = {


// Constantes de rondes RCON
(byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte)
0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00,(byte) 0x04,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x08, (byte)
0x00, (byte) 0x00, (byte) 0x00,(byte) 0x10, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x20, (byte) 0x00, (byte)
0x00, (byte) 0x00,(byte) 0x40, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte)
0x00,(byte) 0x1b, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x36, (byte) 0x00, (byte) 0x00, (byte) 0x00};

public static void main(String[] args) {


byte[] key = {
// Clé de chiffrement (16 octets pour AES-128)
(byte) 0x2b, (byte) 0x7e, (byte) 0x15, (byte) 0x16,(byte)
0x28, (byte) 0xae, (byte) 0xd2, (byte) 0xa6,(byte) 0xab,
(byte) 0xf7, (byte) 0x15, (byte) 0x88,(byte) 0x09, (byte)
0xcf, (byte) 0x4f, (byte) 0x3c};

byte[] plaintext = {
// Texte à chiffrer (16 octets)
(byte) 0x32, (byte) 0x43, (byte) 0xf6, (byte) 0xa8,(byte)
0x88, (byte) 0x5a, (byte) 0x30, (byte) 0x8d,(byte) 0x31,
(byte) 0x31, (byte) 0x98, (byte) 0xa2,(byte) 0xe0, (byte)
0x37, (byte) 0x07, (byte) 0x34};
System.out.println("Le texte en
clair:"+byteArrayToHexString(plaintext));
byte[] ciphertext = encrypt(plaintext, key);
System.out.println("Texte chiffre : " +
byteArrayToHexString(ciphertext));
}

public static byte[] encrypt(byte[] plaintext, byte[]


key) {
byte[][] roundKeys = expandKey(key);

byte[] state = new byte[BLOCK_SIZE];


System.arraycopy(plaintext, 0, state, 0,
BLOCK_SIZE);

addRoundKey(state, roundKeys[0]);

for (int round = 1; round <= NB_ROUNDS - 1;round++) {


subBytes(state);
shiftRows(state);
mixColumns(state);
addRoundKey(state, roundKeys[round]);
}
//dernier round
subBytes(state);
shiftRows(state);
addRoundKey(state, roundKeys[NB_ROUNDS]);

return state;
}

private static byte[][] expandKey(byte[] key) {


byte[][] roundKeys = new byte[NB_ROUNDS + 1][4 *NB_WORDS];
System.arraycopy(key, 0, roundKeys[0], 0, BLOCK_SIZE);

for (int round = 1; round <= NB_ROUNDS; round++) {


byte[] previousRoundKey = roundKeys[round - 1];
byte[] newRoundKey = new byte[4 * NB_WORDS];

// Rotation de mots
byte temp = previousRoundKey[0];
for (int i = 0; i < 3; i++) {
previousRoundKey[i] = previousRoundKey[i + 1];
}
previousRoundKey[3] = temp;

// Substitution de bytes
for (int i = 0; i < NB_WORDS; i++) {
previousRoundKey[i] = subByte(previousRoundKey[i]);
}

// XOR avec le RCON de la ronde


previousRoundKey[0] ^= RCON[round];

// Calcul des nouveaux mots de la ronde


for (int i = 0; i < NB_WORDS; i++) {
for (int j = 0; j < 4; j++) {
newRoundKey[4 * i + j] = (byte)
(previousRoundKey[4 * i + j] ^ roundKeys[round - 1][4 * i
+ j]);
}
}
roundKeys[round] = newRoundKey;
}

return roundKeys;
}

private static void subBytes(byte[] state) {


for (int i = 0; i < BLOCK_SIZE; i++) {
state[i] = subByte(state[i]);
}
}

private static byte subByte(byte b) {


int row = (b >> 4) & 0x0F;
int column = b & 0x0F;
return SBOX[row][column];
}

private static void shiftRows(byte[] state) {


byte[] temp = new byte[BLOCK_SIZE];

// Décalage des rangées


temp[0] = state[0];
temp[1] = state[5];
temp[2] = state[10];
temp[3] = state[15];

temp[4] = state[4];
temp[5] = state[9];
temp[6] = state[14];
temp[7] = state[3];

temp[8] = state[8];
temp[9] = state[13];
temp[10] = state[2];
temp[11] = state[7];

temp[12] = state[12];
temp[13] = state[1];
temp[14] = state[6];
temp[15] = state[11];
System.arraycopy(temp, 0, state, 0, BLOCK_SIZE);
}

private static void mixColumns(byte[] state) {


for (int i = 0; i < 4; i++) {
byte[] column = new byte[4];
for (int j = 0; j < 4; j++) {
column[j] = state[4 * j + i];
}
mixColumn(column);
for (int j = 0; j < 4; j++) {
state[4 * j + i] = column[j];
}
}
}

private static void mixColumn(byte[] column) {


byte[] temp = new byte[4];
temp[0] = (byte) (mul2(column[0]) ^
mul3(column[1]) ^ column[2] ^ column[3]);
temp[1] = (byte) (column[0] ^ mul2(column[1]) ^
mul3(column[2]) ^ column[3]);
temp[2] = (byte) (column[0] ^ column[1] ^
mul2(column[2]) ^ mul3(column[3]));
temp[3] = (byte) (mul3(column[0]) ^ column[1] ^
column[2] ^ mul2(column[3]));
System.arraycopy(temp, 0, column, 0, 4);
}

private static byte mul2(byte b) {


int value = b & 0xFF;
int result = (value << 1);
if (result > 0xFF) {
result ^= 0x1B;
}
return (byte) (result & 0xFF);
}

private static byte mul3(byte b) {


return (byte) (mul2(b) ^ b);
}
private static void addRoundKey(byte[] state, byte[]
roundKey) {
for (int i = 0; i < BLOCK_SIZE; i++) {
state[i] ^= roundKey[i];
}
}

private static String byteArrayToHexString(byte[]


array) {
StringBuilder sb = new StringBuilder();
for (byte b : array) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}
Algorithme DES :
package DES;

import java.util.Arrays;

public class DESExample {

private static final int BLOCK_SIZE = 64;


private static final int KEY_SIZE = 64;

private static final int[] IP = {


58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};

private static final int[] IP_INV = {


40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};

private static final int[] E = {


32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1
};

private static final int[] P = {


16, 7, 20, 21, 29, 12, 28, 17,
1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9,
19, 13, 30, 6, 22, 11, 4, 25
};

private static final int[] PC1 = {


57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};

private static final int[] PC2 = {


14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};

private static final int NUM_ROUNDS = 16;

private static final int[][] SBOX = {


{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0,
7,0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13},
{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5,
10,3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 13,
8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9},
{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2,
8,13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,13,
6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,1, 10,
13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12},
{7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4,
15,13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14,
9,10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14},
{2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0,
14, 9,14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8,
6,4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,11,
8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3},
{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7,
5, 11,10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3,
8,9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13},
{4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10,
6, 1,13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12},
{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0,
12, 7,1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9,
2,7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 1}
};

private static final int[] SHIFT = {


1, 1, 2, 2, 2, 2, 2, 2,
1, 2, 2, 2, 2, 2, 2, 1
};

public static void main(String[] args) {


String plaintext = "0123456789ABCDEF";
String key = "133457799BBCDFF1";

int[] plaintextBits = stringToBits(plaintext);


int[] keyBits = stringToBits(key);

int[] encryptedBits = encrypt(plaintextBits, keyBits);


String encryptedText = bitsToString(encryptedBits);

System.out.println("Plaintext: " + plaintext);


System.out.println("Key: " + key);
System.out.println("Encrypted text: " + encryptedText);
}

private static int[] encrypt(int[] plaintextBits,


int[] keyBits) {
// Apply initial permutation (IP)
int[] permutedBits = permute(plaintextBits, IP);

// Split the permuted bits into left and right


halves
int[] left = Arrays.copyOfRange(permutedBits, 0,
BLOCK_SIZE / 2);
int[] right = Arrays.copyOfRange(permutedBits,
BLOCK_SIZE / 2, BLOCK_SIZE);

// Generate round keys


int[][] roundKeys = generateRoundKeys(keyBits);

// Perform 16 rounds of encryption


for (int i = 0; i < NUM_ROUNDS; i++) {
int[] previousLeft = left.clone();
left = right;
right = xor(previousLeft, feistelFunction(right,
roundKeys[i]));
}

// Combine the final left and right halves


int[] combined = concatenate(right, left);

// Apply final permutation (IP inverse)


return permute(combined, IP_INV);
}

private static int[] permute(int[] input, int[]


permutationTable) {
int[] output = new int[permutationTable.length];
for (int i = 0; i < permutationTable.length; i++)
{
output[i] = input[permutationTable[i] - 1];
}
return output;
}

private static int[] feistelFunction(int[] right,


int[] roundKey) {
// Expand the right half to match the size of the
round key
int[] expandedRight = permute(right, E);

// XOR the expanded right half with the round key


int[] xored = xor(expandedRight, roundKey);

// Apply S-box substitution


int[] substituted = substitute(xored);

// Permute the substituted bits


return permute(substituted, P);
}

private static int[][] generateRoundKeys(int[] key) {


int[][] roundKeys = new int[NUM_ROUNDS][KEY_SIZE / 2];

// Apply permuted choice 1 (PC1) to the key


int[] permutedKey = permute(key, PC1);

// Split the permuted key into left and right


halves
int[]left=Arrays.copyOfRange(permutedKey,0,KEY_SIZE /2);
int[]right=Arrays.copyOfRange(permutedKey,KEY_SIZE/2,
KEY_SIZE);

// Generate round keys


for (int i = 0; i < NUM_ROUNDS; i++) {
// Perform circular left shifts on both halves
left = circularLeftShift(left, SHIFT[i]);
right = circularLeftShift(right, SHIFT[i]);

// Combine the left and right halves


int[] combined = concatenate(left, right);

// Apply permuted choice 2 (PC2) to generate


the round key
roundKeys[i] = permute(combined, PC2);
}

return roundKeys;
}

private static int[] substitute(int[] input) {


int[] output = new int[input.length / 6 * 4];
for (int i = 0; i < input.length; i += 6) {
int row = input[i] * 2 + input[i + 5];
int column = input[i + 1] * 8 + input[i + 2]*4
+ input[i + 3] * 2 + input[i + 4];
int value = SBOX[i / 6][row * 16 + column];
output[i / 6 * 4] = (value >> 3) & 1;
output[i / 6 * 4 + 1] = (value >> 2) & 1;
output[i / 6 * 4 + 2] = (value >> 1) & 1;
output[i / 6 * 4 + 3] = value & 1;
}
return output;
}

private static int[] xor(int[] a, int[] b) {


int[] result = new int[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] ^ b[i];
}
return result;
}

private static int[] circularLeftShift(int[] input,


int numShifts) {
int[] output = new int[input.length];
for (int i = 0; i < input.length; i++) {
output[i] = input[(i + numShifts) %input.length];
}
return output;
}

private static int[] concatenate(int[] a, int[] b) {


int[] result = new int[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}

private static int[] stringToBits(String input) {


int[] bits = new int[input.length() * 4];
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
int value = Character.digit(c, 16);
for (int j = 0; j < 4; j++) {
bits[i * 4 + j] = (value >> (3 - j)) & 1;
}
}
return bits;
}

private static String bitsToString(int[] bits) {


StringBuilder sb = new StringBuilder(bits.length / 4);
for (int i = 0; i < bits.length; i += 4) {
int value = 8 * bits[i] + 4 * bits[i + 1] + 2
* bits[i + 2] + bits[i + 3];
sb.append(Integer.toHexString(value));
}
return sb.toString().toUpperCase();
}
}
Chiffrement de RSA:
package RSA;

//Java Program to Implement the RSA Algorithm


import java.math.*;
import java.util.*;

class RSA {
public static void main(String args[])
{
int p, q, n, z, d = 0, e, i;

// The number to be encrypted and decrypted


int msg = 12;
double c;
BigInteger msgback;
System.out.println("Le message en clair :"+msg);
// 1st prime number p
p = 3;

// 2nd prime number q


q = 11;
n = p * q;
z = (p - 1) * (q - 1);
System.out.println("the value of z = " + z);

for (e = 2; e < z; e++) {

// e is for public key exponent


if (gcd(e, z) == 1) {
break;
}
}
System.out.println("the value of e = " + e);
for (i = 0; i <= 9; i++) {
int x = 1 + (i * z);

// d is for private key exponent


if (x % e == 0) {
d = x / e;
break;
}
}
System.out.println("the value of d = " + d);
c = (Math.pow(msg, e)) % n;
System.out.println("Encrypted message is : " + c);

// converting int value of n to BigInteger


BigInteger N = BigInteger.valueOf(n);

// converting float value of c to BigInteger


BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
System.out.println("Decrypted message is : "
+ msgback);
}

static int gcd(int e, int z)


{
if (e == 0)
return z;
else
return gcd(z % e, e);
}
}

Chiffrement Affine :
package Affine;

import java.util.Scanner;

public class AffineCipher {

private static final int a = 5; // coefficient a


private static final int b = 7; // coefficient b
private static final int m = 26; // taille de
l'alphabet

// Fonction pour calculer l'inverse modulaire de a


private static int modInverse(int a, int m) {
a = a % m;
for (int x = 1; x < m; x++) {
if ((a * x) % m == 1) {
return x;
}
}
return -1;
}

// Fonction pour encrypter un message


public static String encrypt(String message) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (Character.isLetter(c)) {
int charIndex = c - 'a';
int encryptedIndex = (a * charIndex + b) %
m;
char encryptedChar = (char)
(encryptedIndex + 'a');
result.append(encryptedChar);
} else {
result.append(c);
}
}
return result.toString();
}

// Fonction pour décrypter un message


public static String decrypt(String message) {
StringBuilder result = new StringBuilder();
int aInverse = modInverse(a, m);
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (Character.isLetter(c)) {
int charIndex = c - 'a';
int decryptedIndex=(aInverse*(charIndex-b+m))% m;
char decryptedChar = (char)(decryptedIndex+ 'a');
result.append(decryptedChar);
} else {
result.append(c);
}
}
return result.toString();
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Entrez le message à chiffrer : ");
String message = sc.nextLine();
String encryptedMessage = encrypt(message);
System.out.println("Message chiffré:"+
encryptedMessage);
String decryptedMessage = decrypt(encryptedMessage);
System.out.println("Message déchiffré:"+
decryptedMessage);
sc.close();
}
}
Chiffrement de Hill :
package Hill;

import java.util.Scanner;

public class HillCipher {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Demander à l'utilisateur de saisir la clé


de chiffrement sous forme de matrice 2x2
int[][] key = new int[2][2];
System.out.println("Entrez la cle de
chiffrement sous forme de matrice 2x2 :");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
key[i][j] = scanner.nextInt();
}
}

// Demander à l'utilisateur de saisir le texte


à chiffrer
System.out.println("Entrez le texte a chiffrer :");
scanner.nextLine();
String plaintext = scanner.nextLine().toUpperCase();

// Ajouter des lettres fictives pour que la


longueur du texte soit un multiple de 2
while (plaintext.length() % 2 != 0) {
plaintext += "X";
}

// Chiffrer le texte par blocs de deux lettres


String ciphertext = "";
for (int i = 0; i < plaintext.length(); i += 2) {
int[] block = {plaintext.charAt(i) - 'A',
plaintext.charAt(i+1) - 'A'};
int[] encryptedBlock = multiplyMatrix(key, block);
ciphertext += (char) (encryptedBlock[0] + 'A');
ciphertext += (char) (encryptedBlock[1] + 'A');}
// Afficher le texte chiffré
System.out.println("Texte chiffre : " + ciphertext);
}

// Fonction pour multiplier une matrice 2x2 avec


un vecteur 2x1
public static int[] multiplyMatrix(int[][] matrix,
int[] vector) {
int[] result = new int[2];
result[0] = (matrix[0][0] * vector[0] +
matrix[0][1] * vector[1]) % 26;
result[1] = (matrix[1][0] * vector[0] +
matrix[1][1] * vector[1]) % 26;
return result;
}
}

Chiffrement César:
package Cezar;

public class CesarCrypt {

public static void main(String[] args) {


String message = "crypto";
int clef = 3; // nombre de positions à décaler

String messageChiffre = chiffrementCesar(message, clef);


System.out.println("Message chiffré: " +
messageChiffre);
String messageDechiffre =
dechiffrementCesar(messageChiffre, clef);
System.out.println("Message déchiffré: " +
messageDechiffre);
}

// Fonction de chiffrement de César


public static String chiffrementCesar(String message,
int clef) {
String messageChiffre = "";

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


char c = (char)(message.charAt(i) + clef);

if (c > 'z') {
messageChiffre +=
(char)(message.charAt(i) - (26 - clef));
} else {
messageChiffre += c;
}
}

return messageChiffre;
}

// Fonction de déchiffrement de César


public static String dechiffrementCesar(String
messageChiffre, int clef) {
String messageDechiffre = "";

for (int i = 0; i < messageChiffre.length();


i++) {
char c = (char)(messageChiffre.charAt(i) -
clef);

if (c < 'a') {
messageDechiffre +=
(char)(messageChiffre.charAt(i) + (26 - clef));
} else {
messageDechiffre += c;
}
}

return messageDechiffre;
}

Chiffrement de Vigénère:
package Vigenere;

public class VigenereCipher {


private String key;

public VigenereCipher(String key) {


this.key = key;
}

public String encrypt(String plainText) {


StringBuilder cipherText = new StringBuilder();
int keyIndex = 0;
for (int i = 0; i < plainText.length(); i++) {
char c = plainText.charAt(i);
if (Character.isLetter(c)) {
int shift = key.charAt(keyIndex) - 'a';
c = (char) ((c - 'a' + shift) % 26 + 'a');
keyIndex = (keyIndex + 1) % key.length();
}
cipherText.append(c);
}
return cipherText.toString();
}

public String decrypt(String cipherText) {


StringBuilder plainText = new StringBuilder();
int keyIndex = 0;
for (int i = 0; i < cipherText.length(); i++) {
char c = cipherText.charAt(i);
if (Character.isLetter(c)) {
int shift = key.charAt(keyIndex) - 'a';
c = (char)((c - 'a' - shift + 26) % 26 + 'a');
keyIndex = (keyIndex + 1) % key.length();
}
plainText.append(c);
}
return plainText.toString();
}

public static void main(String[] args) {


String key = "secret";
VigenereCipher cipher = new VigenereCipher(key);
String plainText = "hello world";
System.out.println("Le texte en clair:"+plainText);
String cipherText = cipher.encrypt(plainText);
System.out.println("Cipher Text: " + cipherText);
String decryptedText = cipher.decrypt(cipherText);
System.out.println("Decrypted Text:"+
decryptedText);
}
}

You might also like