|
import java.security.MessageDigest; |
|
import java.util.Arrays; |
|
import java.util.Base64; |
|
import java.nio.charset.StandardCharsets; |
|
import javax.crypto.KeyGenerator; |
|
import javax.crypto.SecretKey; |
|
import javax.crypto.spec.SecretKeySpec; |
|
import javax.crypto.spec.IvParameterSpec; |
|
|
|
import javax.crypto.Cipher; |
|
import javax.crypto.spec.IvParameterSpec; |
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
public class AES { |
|
static byte[] CIPHER_KEY = "0123456789abcdef0123456789abcdef".getBytes(StandardCharsets.UTF_8); |
|
static byte[] IV = "1234567890ABCDEF".getBytes(StandardCharsets.UTF_8); |
|
static char PADDING_CHAR = '\034'; |
|
|
|
public static String encrypt(String text) throws Exception { |
|
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE"); |
|
SecretKeySpec key = new SecretKeySpec(CIPHER_KEY, "AES"); |
|
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV)); |
|
int paddingSize = 16 - text.length() % 16; |
|
String padding = String.format("%0" + paddingSize + "d", 0).replace('0', PADDING_CHAR); |
|
String padded = text + padding; |
|
byte[] encrypted = cipher.doFinal(padded.getBytes(StandardCharsets.UTF_8)); |
|
return Base64.getEncoder().encodeToString(encrypted); |
|
} |
|
|
|
public static String decrypt(String data) throws Exception { |
|
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE"); |
|
SecretKeySpec key = new SecretKeySpec(CIPHER_KEY, "AES"); |
|
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV)); |
|
byte[] encrypted = Base64.getDecoder().decode(data); |
|
String padded = new String(cipher.doFinal(encrypted), StandardCharsets.UTF_8); |
|
return padded.replaceAll(PADDING_CHAR + "+$", ""); |
|
} |
|
|
|
public static void main(String[] args) { |
|
try { |
|
String message = "this information is confidential, for your eyes only"; |
|
System.out.println("secret message: " + message); |
|
String encrypted_message = encrypt(message); |
|
System.out.println(" encrypted > " + encrypted_message); |
|
String decrypted_message = decrypt(encrypted_message); |
|
System.out.println(" decrypted > " + decrypted_message); |
|
if (message.equals(decrypted_message)) { |
|
System.out.println("SUCCESS"); |
|
} else { |
|
System.out.println("FAILED"); |
|
} |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
} |
|
} |
Why does your Java example not work anymore if I modify the 'message' in line 41 to:
message = "this information is cönfidential, for your eyes only";
?