Skip to content

aes encryption result is incorrect #29

@tfzxyinhao

Description

@tfzxyinhao

java code

public static String AESEncrypt(String plainText, SecretKey key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        if (ObjectUtils.isEmpty(plainText)) return plainText;
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, key);
        return Base64.getEncoder().encodeToString(aesCipher.doFinal(plainText.getBytes()));
    }

public static void main(String[] args) {
        String key = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=";
        String before = "James";
        try {
            String after = AESEncrypt(before, new SecretKeySpec(Base64.getDecoder().decode(key), "AES"));
            System.out.println(before);
            System.out.println(after);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
}

java code ouput

James
u9QmnI+gjMUK9LETbm2eWA==

rust code

pub fn test_aes(&self, password: String, data: String) -> String {
        let key = base64::decode(password).unwrap();
        let cipher = Aes256::new(key.as_slice());
        let mut text = data.as_bytes().clone().to_owned();
        cipher.encrypt(&mut text);
        return base64::encode(text);
    }

pub fn test(&self) {
        let password = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=".to_string();
        let data = "James".to_string();
        println!("{}\n{}", data.clone(), self.test_aes(password, data));
    }

rust code ouput

James
vWkOWA0=

base64("12345678901234567890123456789012") = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI"

aes online result
image

Is there anything wrong with how I use it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions