[英]AES Encryption and Decryption (JAVA)
I want to check my result of encryption and decryption with this website https://8gwifi.org/CipherFunctions.jsp How do I input in the secret key on the website?我想在这个网站上查看我的加解密结果https://8gwifi.org/CipherFunctions.jsp我如何在网站上输入秘钥?
This is my java code.这是我的java代码。
public static void main(String[] args) throws Exception {
final String secretKey = "PASSWORD";
String originalString = "Java Web Developer";
String encryptedString = AESUtils.encrypt(originalString, secretKey) ;
String decryptedString = AESUtils.decrypt(encryptedString, secretKey) ;
System.out.println("Original : " + originalString); //Original : Java Web Developer
System.out.println("Encryption : " + encryptedString); //Encryption : 43Ak2VHK368eandGGYWX2WvbcimGGAEsPdCSjEatjR0=
System.out.println("Decryption : " + decryptedString); //Decryption : Java Web Developer
}
public class AESUtils {
private static SecretKeySpec secretKey;
private static byte[] key;
public static void setKey(String myKey)
{
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
byte encoded[] = secretKey.getEncoded();
/*
* Encodes the specified byte array into a String using Base64 encoding
* scheme
*/
String encodedKey = Base64.getEncoder().encodeToString(encoded);
System.out.println("SecretKey: " + encodedKey); //SecretKey: ESu3kTBHkd3PaS4p/VzxSQ==
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String encrypt(String strToEncrypt, String secret)
{
try
{
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
}
catch (Exception e)
{
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
public static String decrypt(String strToDecrypt, String secret)
{
try
{
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
}
catch (Exception e)
{
System.out.println("Error while decrypting: " + e.toString());
}
return null;
}
}
I tried inputting the secret key with "ESu3kTBHkd3PaS4p/VzxSQ==", But the result of encryption does not match the result of encryption from the java program.我尝试用“ESu3kTBHkd3PaS4p/VzxSQ==”输入密钥,但是加密的结果与java程序的加密结果不匹配。
My Java program retured encryption to be 43Ak2VHK368eandGGYWX2WvbcimGGAEsPdCSjEatjR0=我的 Java 程序返回加密为 43Ak2VHK368eandGGYWX2WvbcimGGAEsPdCSjEatjR0=
AES with ECB mode and PKCS5Padding,带有 ECB 模式和 PKCS5Padding 的 AES,
public static String encryptAES(String toEncrypt, final String key1, final String key2) throws Exception {
try {
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec keySpec = new PBEKeySpec(key1.toCharArray(), key2.getBytes(), 65536, 256);
SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return Base64.getEncoder().encodeToString(cipher.doFinal(toEncrypt.getBytes(StandardCharsets.UTF_8)));
} catch (Exception ex) {
throw new Exception(ex);
}
}
public static String decryptAES(String toDecrypt, final String key1, final String key2) throws Exception {
try {
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec keySpec = new PBEKeySpec(key1.toCharArray(), key2.getBytes(), 65536, 256);
SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return new String(cipher.doFinal(Base64.getDecoder().decode(toDecrypt)));
} catch (Exception ex) {
throw new Exception(ex);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.