Java 256-bit AES Password-Based Encryption

后端 未结 9 1396
名媛妹妹
名媛妹妹 2020-11-21 05:22

I need to implement 256 bit AES encryption, but all the examples I have found online use a \"KeyGenerator\" to generate a 256 bit key, but I would like to use my own passkey

9条回答
  •  感情败类
    2020-11-21 05:45

    Consider using the Spring Security Crypto Module

    The Spring Security Crypto module provides support for symmetric encryption, key generation, and password encoding. The code is distributed as part of the core module but has no dependencies on any other Spring Security (or Spring) code.

    It's provides a simple abstraction for encryption and seems to match what's required here,

    The "standard" encryption method is 256-bit AES using PKCS #5's PBKDF2 (Password-Based Key Derivation Function #2). This method requires Java 6. The password used to generate the SecretKey should be kept in a secure place and not be shared. The salt is used to prevent dictionary attacks against the key in the event your encrypted data is compromised. A 16-byte random initialization vector is also applied so each encrypted message is unique.

    A look at the internals reveals a structure similar to erickson's answer.

    As noted in the question, this also requires the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy (else you'll encounter InvalidKeyException: Illegal Key Size). It's downloadable for Java 6, Java 7 and Java 8.

    Example usage

    import org.springframework.security.crypto.encrypt.Encryptors;
    import org.springframework.security.crypto.encrypt.TextEncryptor;
    import org.springframework.security.crypto.keygen.KeyGenerators;
    
    public class CryptoExample {
        public static void main(String[] args) {
            final String password = "I AM SHERLOCKED";  
            final String salt = KeyGenerators.string().generateKey();
            
            TextEncryptor encryptor = Encryptors.text(password, salt);      
            System.out.println("Salt: \"" + salt + "\"");
            
            String textToEncrypt = "*royal secrets*";
            System.out.println("Original text: \"" + textToEncrypt + "\"");
            
            String encryptedText = encryptor.encrypt(textToEncrypt);
            System.out.println("Encrypted text: \"" + encryptedText + "\"");
            
            // Could reuse encryptor but wanted to show reconstructing TextEncryptor
            TextEncryptor decryptor = Encryptors.text(password, salt);
            String decryptedText = decryptor.decrypt(encryptedText);
            System.out.println("Decrypted text: \"" + decryptedText + "\"");
            
            if(textToEncrypt.equals(decryptedText)) {
                System.out.println("Success: decrypted text matches");
            } else {
                System.out.println("Failed: decrypted text does not match");
            }       
        }
    }
    

    And sample output,

    Salt: "feacbc02a3a697b0"
    Original text: "*royal secrets*"
    Encrypted text: "7c73c5a83fa580b5d6f8208768adc931ef3123291ac8bc335a1277a39d256d9a" 
    Decrypted text: "*royal secrets*"
    Success: decrypted text matches
    

提交回复
热议问题