Детерминированный TextEncryptorJAVA

Программисты JAVA общаются здесь
Anonymous
Детерминированный TextEncryptor

Сообщение Anonymous »

Я хочу, чтобы этот шифратор возвращал одно и то же значение для одного и того же ввода. Но этого не происходит.
Как это сделать?

Код: Выделить всё

import com.example.em_card_service.data.properties.EncryptionProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;

import java.util.List;

@Configuration
@RequiredArgsConstructor
public class SecurityConfig {

private final EncryptionProperties encryptionProperties;

// more beans

@Bean
public TextEncryptor textEncryptor() {
return Encryptors.text(encryptionProperties.getPassword(), encryptionProperties.getSalt());
}
}
Клод говорит, что не существует внеплановых способов сделать это, и предложил реализовать это самому:

Код: Выделить всё

    @Bean
public TextEncryptor textEncryptor() {
AesBytesEncryptor bytesEncryptor = new AesBytesEncryptor(
encryptionProperties.getPassword(),
encryptionProperties.getSalt(),
KeyGenerators.shared(16)
);

return new TextEncryptor() {
@Override
public String encrypt(String text) {
byte[] encrypted = bytesEncryptor.encrypt(text.getBytes(StandardCharsets.UTF_8));
return HexFormat.of().formatHex(encrypted);
}

@Override
public String decrypt(String encryptedText) {
byte[] decrypted = bytesEncryptor.decrypt(HexFormat.of().parseHex(encryptedText));
return new String(decrypted, StandardCharsets.UTF_8);
}
};
}
Spring Boot 4.

Вернуться в «JAVA»