简体   繁体   English

Java AES 128 ECB 到 AES 256 ECB 转换

[英]Java AES 128 ECB to AES 256 ECB conversion

I have an issue to upgrade an AES 128 bit encryption to AES 256 in ECB mode.我在 ECB 模式下将 AES 128 位加密升级到 AES 256 时遇到问题。 But I'm unable to find any solution for that.但我无法找到任何解决方案。 Mostly solutions are there for AES 256 CBC mode.大多数解决方案都适用于 AES 256 CBC 模式。 Any help is highly appreciated.非常感谢任何帮助。

The exception that I got was due to bad padding我得到的例外是由于填充错误

PS: I'm aware of the vulnerability of ECB mode in AES but this is what I need to implement at the moment. PS:我知道 AES 中 ECB 模式的漏洞,但这是我目前需要实现的。

import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.GeneralSecurityException;

public class Decrypt {
    public static String decryptPayload(String payload) throws Exception {
        byte[] keyBytes = {
                0x74, 0x68, 0x69, 0x73, 0x49, 0x73, 0x43, 0x53, 0x75, 0x63, 0x72, 0x65, 0x44, 0x4b, 0x55, 0x79
        };
        try {
            payload = payload.replace(' ', '+');
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            final SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            return new String(cipher.doFinal(
                    Base64.decodeBase64(payload)));
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        return null;
    }
}

The implementation of AES 256 ECB I tried was as follows:我尝试的 AES 256 ECB 的实现如下:

    public static String decrypt(String strToDecrypt, String secretKey) {
        try
        {
            IvParameterSpec ivspec = new IvParameterSpec(iv);

            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
            KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
            SecretKey tmp = factory.generateSecret(spec);
            SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");

            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
            return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
        }
        catch (Exception e) {
            System.out.println("Error while decrypting: " + e.toString());
        }
        return null;
    }

The entire solution which worked for me, thanks to @MichaelFehr感谢@MichaelFehr,对我有用的整个解决方案

public void tester() {
        System.out.println("Program starts");
        String key = "5u7x!A%D*G-KaPdSgVkYp3s6v9y/B?E(";
        String payload = "Test string to test AES 256 ECB";
        String encrypted = "";
        String decrypted = "";
        // Encryption
        try{
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            final SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            final String encryptedString = Base64.encodeBase64String(cipher.doFinal(payload.getBytes()));
            System.out.println("The encrypted string is\n"+encryptedString);
            encrypted = encryptedString;
        }
        catch(Exception e){
            e.printStackTrace();
        }


        // Decryption
        try
        {
            decrypted = encrypted.replace(' ', '+');
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            final SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(encrypted)));
            System.out.println("The decrypted string is\n"+decryptedString);
            decrypted=decryptedString;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM