Hello! In general, it became necessary to write a Java program that will encrypt incoming messages using the DES method, specifying its own key. Now the most important thing is that the key and the message that needs to be encrypted is set in binary form , and that’s what I had problems with.
The code itself:
import java.io.IOException; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.util.ArrayList; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class Cryptography { public static String DES_ENCRYPTION_KEY; public static String INPUT; private static final String CRYPTOGRAPHY_ALGO_DES = "DES"; private static Cipher cipher = null; private static DESKeySpec keySpec = null; private static SecretKeyFactory keyFactory = null; public static String encrypt(String inputString, String commonKey) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException { String encryptedValue = null; SecretKey key = getSecretKey(commonKey); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] inputBytes = compare(inputString); byte[] outputBytes = cipher.doFinal(inputBytes); encryptedValue = new BASE64Encoder().encode(outputBytes); return encryptedValue; } public static String decrypt(String encryptedString, String commonKey) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException { String decryptedValue = ""; // When Base64Encoded strings are passed in URLs, '+' character gets converted to space and so we need to reconvert the space to '+' and since encoded string cannot have space in it so we are completely safe. encryptedString = encryptedString.replace(' ', '+'); SecretKey key = getSecretKey(commonKey); cipher.init(Cipher.DECRYPT_MODE, key); byte[] recoveredBytes = cipher.doFinal(new BASE64Decoder().decodeBuffer(encryptedString)); int[] out = new int[recoveredBytes.length]; String s = ""; for (int i = 0; i < recoveredBytes.length; i++) { out[i] = recoveredBytes[i] & 0xFF; s += Integer.toBinaryString(out[i]); } decryptedValue = new String(s); return decryptedValue; } private static SecretKey getSecretKey(String secretPassword) { SecretKey key = null; try { cipher = Cipher.getInstance(CRYPTOGRAPHY_ALGO_DES); byte[] desKeyData = compare(secretPassword); keySpec = new DESKeySpec(desKeyData); keyFactory = SecretKeyFactory.getInstance(CRYPTOGRAPHY_ALGO_DES); key = keyFactory.generateSecret(keySpec); } catch (Exception e) { e.printStackTrace(); System.out.println("Error in generating the secret Key"); } return key; } public static void main(String args[]) { try{ INPUT = "0000101111010101111101111101101110010000010011011011100111010010"; DES_ENCRYPTION_KEY = "1111101011001011101100110110111001110010000011100001001110011010"; String encrypted = Cryptography.encrypt(INPUT, DES_ENCRYPTION_KEY); System.out.println("encrypted: " + encrypted); String decrypted = Cryptography.decrypt(encrypted, DES_ENCRYPTION_KEY); System.out.println("decrypted: " + decrypted); } catch(Exception e){ } } public static byte[] compare(String value) { try { ArrayList<String> str = new ArrayList<String>(); ArrayList<Byte> inputBytes = new ArrayList<Byte>(); int countS = 0; int countE = 8; int i = 0; while (true) { try { str.add(value.substring(countS, countE)); //System.out.println(str.get(i)); inputBytes.add((byte) Integer.parseInt(str.get(i), 2)); //System.out.println(i + " " + inputBytes.get(i)); countS += 8; countE += 8; i++; } catch (Exception e2) { countE--; if(countE <= countS){ break; } } } byte[] array = new byte[inputBytes.size()]; for (int j=0; j < array.length; j++) { array[j] = inputBytes.get(j); } System.out.println(""); return array; } catch (Exception e1){ e1.printStackTrace(); return null; } } } As the name implies, the encrypt method encrypts a message, and the decrypt method decrypts it. The getSecretKey method is required to initialize a key. Now the most interesting thing is that the compare method creates an array of substrings from a string, almost each of which is 8 bits ( catch (Exception e2) is needed to reduce the substE index of the substring () method if the rest of the string is less than 8 bits) . Next, they are parsed into the 10th base form, and are fed into the array, which then goes further into the encryption / decryption methods. The problem is that if the substring starts from 0, for example, 00110101, then in this case, the first 2 characters will be ignored during the parsing, and as a result, the wrong (cropped) value will be received when decrypting. What could you advise in this case?
PS could be simply applied to the initial line getBytes (), and everything would be fine, but only in this case, each character will be interpreted separately, and as a result, a hefty message will be received that does not interest me.