Skip to content

Commit 794719c

Browse files
authored
Fix formatting in Ciphers package (TheAlgorithms#2756)
1 parent 0503c46 commit 794719c

11 files changed

+1230
-1341
lines changed

Ciphers/AES.java

Lines changed: 549 additions & 547 deletions
Large diffs are not rendered by default.

Ciphers/AESEncryption.java

Lines changed: 74 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package Ciphers;
22

3+
import javax.crypto.*;
34
import java.security.InvalidKeyException;
45
import java.security.NoSuchAlgorithmException;
5-
import javax.crypto.BadPaddingException;
6-
import javax.crypto.Cipher;
7-
import javax.crypto.IllegalBlockSizeException;
8-
import javax.crypto.KeyGenerator;
9-
import javax.crypto.NoSuchPaddingException;
10-
import javax.crypto.SecretKey;
116

127
/**
138
* This example program shows how AES encryption and decryption can be done in Java. Please note
@@ -16,82 +11,83 @@
1611
*/
1712
public class AESEncryption {
1813

19-
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
20-
/**
21-
* 1. Generate a plain text for encryption 2. Get a secret key (printed in hexadecimal form). In
22-
* actual use this must by encrypted and kept safe. The same key is required for decryption.
23-
*/
24-
public static void main(String[] args) throws Exception {
25-
String plainText = "Hello World";
26-
SecretKey secKey = getSecretEncryptionKey();
27-
byte[] cipherText = encryptText(plainText, secKey);
28-
String decryptedText = decryptText(cipherText, secKey);
14+
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
2915

30-
System.out.println("Original Text:" + plainText);
31-
System.out.println("AES Key (Hex Form):" + bytesToHex(secKey.getEncoded()));
32-
System.out.println("Encrypted Text (Hex Form):" + bytesToHex(cipherText));
33-
System.out.println("Descrypted Text:" + decryptedText);
34-
}
16+
/**
17+
* 1. Generate a plain text for encryption 2. Get a secret key (printed in hexadecimal form). In
18+
* actual use this must by encrypted and kept safe. The same key is required for decryption.
19+
*/
20+
public static void main(String[] args) throws Exception {
21+
String plainText = "Hello World";
22+
SecretKey secKey = getSecretEncryptionKey();
23+
byte[] cipherText = encryptText(plainText, secKey);
24+
String decryptedText = decryptText(cipherText, secKey);
3525

36-
/**
37-
* gets the AES encryption key. In your actual programs, this should be safely stored.
38-
*
39-
* @return secKey (Secret key that we encrypt using it)
40-
* @throws NoSuchAlgorithmException (from KeyGenrator)
41-
*/
42-
public static SecretKey getSecretEncryptionKey() throws NoSuchAlgorithmException {
43-
KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("AES");
44-
aesKeyGenerator.init(128); // The AES key size in number of bits
45-
return aesKeyGenerator.generateKey();
46-
}
26+
System.out.println("Original Text:" + plainText);
27+
System.out.println("AES Key (Hex Form):" + bytesToHex(secKey.getEncoded()));
28+
System.out.println("Encrypted Text (Hex Form):" + bytesToHex(cipherText));
29+
System.out.println("Descrypted Text:" + decryptedText);
30+
}
4731

48-
/**
49-
* Encrypts plainText in AES using the secret key
50-
*
51-
* @return byteCipherText (The encrypted text)
52-
* @throws NoSuchPaddingException (from Cipher)
53-
* @throws NoSuchAlgorithmException (from Cipher)
54-
* @throws InvalidKeyException (from Cipher)
55-
* @throws BadPaddingException (from Cipher)
56-
* @throws IllegalBlockSizeException (from Cipher)
57-
*/
58-
public static byte[] encryptText(String plainText, SecretKey secKey)
59-
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
60-
IllegalBlockSizeException, BadPaddingException {
61-
// AES defaults to AES/ECB/PKCS5Padding in Java 7
62-
Cipher aesCipher = Cipher.getInstance("AES");
63-
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
64-
return aesCipher.doFinal(plainText.getBytes());
65-
}
32+
/**
33+
* gets the AES encryption key. In your actual programs, this should be safely stored.
34+
*
35+
* @return secKey (Secret key that we encrypt using it)
36+
* @throws NoSuchAlgorithmException (from KeyGenrator)
37+
*/
38+
public static SecretKey getSecretEncryptionKey() throws NoSuchAlgorithmException {
39+
KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("AES");
40+
aesKeyGenerator.init(128); // The AES key size in number of bits
41+
return aesKeyGenerator.generateKey();
42+
}
6643

67-
/**
68-
* Decrypts encrypted byte array using the key used for encryption.
69-
*
70-
* @return plainText
71-
*/
72-
public static String decryptText(byte[] byteCipherText, SecretKey secKey)
73-
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
74-
IllegalBlockSizeException, BadPaddingException {
75-
// AES defaults to AES/ECB/PKCS5Padding in Java 7
76-
Cipher aesCipher = Cipher.getInstance("AES");
77-
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
78-
byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
79-
return new String(bytePlainText);
80-
}
44+
/**
45+
* Encrypts plainText in AES using the secret key
46+
*
47+
* @return byteCipherText (The encrypted text)
48+
* @throws NoSuchPaddingException (from Cipher)
49+
* @throws NoSuchAlgorithmException (from Cipher)
50+
* @throws InvalidKeyException (from Cipher)
51+
* @throws BadPaddingException (from Cipher)
52+
* @throws IllegalBlockSizeException (from Cipher)
53+
*/
54+
public static byte[] encryptText(String plainText, SecretKey secKey)
55+
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
56+
IllegalBlockSizeException, BadPaddingException {
57+
// AES defaults to AES/ECB/PKCS5Padding in Java 7
58+
Cipher aesCipher = Cipher.getInstance("AES");
59+
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
60+
return aesCipher.doFinal(plainText.getBytes());
61+
}
62+
63+
/**
64+
* Decrypts encrypted byte array using the key used for encryption.
65+
*
66+
* @return plainText
67+
*/
68+
public static String decryptText(byte[] byteCipherText, SecretKey secKey)
69+
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
70+
IllegalBlockSizeException, BadPaddingException {
71+
// AES defaults to AES/ECB/PKCS5Padding in Java 7
72+
Cipher aesCipher = Cipher.getInstance("AES");
73+
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
74+
byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
75+
return new String(bytePlainText);
76+
}
8177

82-
/**
83-
* Convert a binary byte array into readable hex form Old library is deprecated on OpenJdk 11 and
84-
* this is faster regarding other solution is using StringBuilder
85-
*
86-
* @return hexHash
87-
*/
88-
public static String bytesToHex(byte[] bytes) {
89-
char[] hexChars = new char[bytes.length * 2];
90-
for (int j = 0; j < bytes.length; j++) {
91-
int v = bytes[j] & 0xFF;
92-
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
93-
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
78+
/**
79+
* Convert a binary byte array into readable hex form Old library is deprecated on OpenJdk 11 and
80+
* this is faster regarding other solution is using StringBuilder
81+
*
82+
* @return hexHash
83+
*/
84+
public static String bytesToHex(byte[] bytes) {
85+
char[] hexChars = new char[bytes.length * 2];
86+
for (int j = 0; j < bytes.length; j++) {
87+
int v = bytes[j] & 0xFF;
88+
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
89+
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
90+
}
91+
return new String(hexChars);
9492
}
95-
return new String(hexChars);
96-
}
9793
}

Ciphers/AffineCipher.java

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,54 @@
1-
//The ‘key’ for the Affine cipher consists of 2 numbers, we’ll call them a and b.
2-
// The following discussion assumes the use of a 26 character alphabet (m = 26).
3-
// a should be chosen to be relatively prime to m (i.e. a should have no factors in common with m).
4-
51
package Ciphers;
62

7-
import java.util.Scanner;
3+
class AffineCipher {
84

9-
class AffineCipher
10-
{
11-
static Scanner in = new Scanner(System.in);
5+
// Key values of a and b
6+
static int a = 17;
7+
static int b = 20;
128

13-
static String encryptMessage(char[] msg)
14-
{
15-
System.out.println("Enter key value a for encryption : ");
16-
int a = in.nextInt();
17-
18-
System.out.println("Enter key value b for encryption : ");
19-
int b = in.nextInt();
20-
21-
/// Initially empty cipher String
9+
static String encryptMessage(char[] msg) {
10+
/// Cipher Text initially empty
2211
String cipher = "";
23-
for (int i = 0; i < msg.length; i++)
24-
{
12+
for (int i = 0; i < msg.length; i++) {
2513
// Avoid space to be encrypted
2614
/* applying encryption formula ( a x + b ) mod m
2715
{here x is msg[i] and m is 26} and added 'A' to
2816
bring it in range of ascii alphabet[ 65-90 | A-Z ] */
29-
if (msg[i] != ' ')
30-
{
17+
if (msg[i] != ' ') {
3118
cipher = cipher
3219
+ (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
33-
} else // append space character
20+
} else // else simply append space character
3421
{
3522
cipher += msg[i];
3623
}
3724
}
3825
return cipher;
3926
}
4027

41-
static String decryptCipher(String cipher)
42-
{
43-
System.out.println("Enter key value a for decryption : ");
44-
int a = in.nextInt();
45-
46-
System.out.println("Enter key value b for decryption : ");
47-
int b = in.nextInt();
48-
28+
static String decryptCipher(String cipher) {
4929
String msg = "";
5030
int a_inv = 0;
5131
int flag = 0;
5232

5333
//Find a^-1 (the multiplicative inverse of a
5434
//in the group of integers modulo m.)
55-
for (int i = 0; i < 26; i++)
56-
{
35+
for (int i = 0; i < 26; i++) {
5736
flag = (a * i) % 26;
5837

5938
// Check if (a*i)%26 == 1,
60-
// if so, then i will be the multiplicative inverse of a
61-
if (flag == 1)
62-
{
39+
// then i will be the multiplicative inverse of a
40+
if (flag == 1) {
6341
a_inv = i;
6442
}
6543
}
66-
for (int i = 0; i < cipher.length(); i++)
67-
{
44+
for (int i = 0; i < cipher.length(); i++) {
6845
/*Applying decryption formula a^-1 ( x - b ) mod m
6946
{here x is cipher[i] and m is 26} and added 'A'
7047
to bring it in range of ASCII alphabet[ 65-90 | A-Z ] */
71-
if (cipher.charAt(i) != ' ')
72-
{
48+
if (cipher.charAt(i) != ' ') {
7349
msg = msg + (char) (((a_inv *
7450
((cipher.charAt(i) + 'A' - b)) % 26)) + 'A');
75-
}
76-
else // append space character
51+
} else //else simply append space character
7752
{
7853
msg += cipher.charAt(i);
7954
}
@@ -82,17 +57,17 @@ static String decryptCipher(String cipher)
8257
return msg;
8358
}
8459

85-
// Main method
86-
public static void main(String[] args)
87-
{
60+
// Driver code
61+
public static void main(String[] args) {
8862
String msg = "AFFINE CIPHER";
8963

90-
// Encrypting message
64+
// Calling encryption function
9165
String cipherText = encryptMessage(msg.toCharArray());
9266
System.out.println("Encrypted Message is : " + cipherText);
9367

94-
// Decrypting message
68+
// Calling Decryption function
9569
System.out.println("Decrypted Message is: " + decryptCipher(cipherText));
9670

9771
}
9872
}
73+

0 commit comments

Comments
 (0)