Невозможно печатать зашифрованные данныеJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Невозможно печатать зашифрованные данные

Сообщение Anonymous »

Код: Выделить всё

public class EncryptionUtil {
private static final String ALGORITHM = "AES";

public static byte[] encrypt(String data, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(data.getBytes());
}

public static String decrypt(byte[] encryptedData, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(encryptedData);
return new String(decryptedBytes);
}

public static SecretKey generateSecretKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(256);
return keyGenerator.generateKey();
}

public static SecretKey manualSecretKey() throws Exception {
String keyString = "7c4a8d09ca3762af61e59520943dc264";
byte[] keyBytes = keyString.getBytes();
return new SecretKeySpec(keyBytes, "AES");
}

public static void main(String[] args) throws Exception {
SecretKey secretKey = manualSecretKey(); // Change to generateSecretKey
String originalData = "ABHISHEK";

byte[] encryptedData = encrypt(originalData, secretKey);
System.out.println("Encrypted data: " + new String(encryptedData)); // This line doesn't work well when using the manualSecretKey()

String decryptedData = decrypt(encryptedData, secretKey);
System.out.println("Decrypted data: " + decryptedData);

Base64.Encoder encoder = Base64.getEncoder();
System.out.println(encoder.encodeToString(encryptedData));
}

}
Если я изменю на GenerateseCretKey () Для генерации объекта SecretKey, заявление SYSOUT работает хорошо. Но когда я использую hanualSecretKey () , он не печатает зашифрованные данные »:« и значение, добавленное к нему.

Подробнее здесь: https://stackoverflow.com/questions/793 ... ypted-data
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»