我需要实现256位AES加密,但我在网上找到的所有示例都使用"KeyGenerator"生成256位密钥,但我想使用自己的密码.如何创建自己的密钥?我已经尝试将其填充为256位,但后来我得到一个错误,说密钥太长了.我确实安装了无限管辖区补丁,所以那不是问题:)
IE浏览器.KeyGenerator看起来像这样......
// Get the KeyGenerator
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256 bits may not be available
// Generate the secret key specs.
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
Run Code Online (Sandbox Code Playgroud)
编辑
我实际上是将密码填充到256个字节,而不是位,这太长了.以下是我现在使用的一些代码,我对此有更多的经验.
byte[] key = null; // TODO
byte[] input = null; // TODO
byte[] output = null;
SecretKeySpec keySpec = null;
keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
output = cipher.doFinal(input)
Run Code Online (Sandbox Code Playgroud)
您需要自己做的"TODO"位:-)
我AES/GCM/NoPadding在Java 8中使用加密,我想知道我的代码是否存在安全漏洞.我的代码似乎有效,因为它加密和解密文本,但一些细节尚不清楚.
我的主要问题是:
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] iv = cipher.getIV(); // ?????
Run Code Online (Sandbox Code Playgroud)
IV是否满足"对于给定的密钥,IV不得重复"的要求.来自RFC 4106?
我也很感激我对相关问题的任何答案/见解(见下文),但第一个问题最让我烦恼.我不知道在哪里可以找到解决此问题的源代码或文档.
这是完整的代码,粗略.如果我在撰写这篇文章时引入了错误,我深表歉意:
class Encryptor {
Key key;
Encryptor(byte[] key) {
if (key.length != 32) throw new IllegalArgumentException();
this.key = new SecretKeySpec(key, "AES");
}
// the output is sent to users
byte[] encrypt(byte[] src) throws Exception {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] iv = cipher.getIV(); // See question #1
assert iv.length == 12; // See question …Run Code Online (Sandbox Code Playgroud) 在使用Spring Boot的项目中,我们使用application.properties,但需要根据外部配置配置其中一些属性(如日志级别的端口号).我们通过API访问配置,因此仅在运行时才知道.
有没有办法在运行时覆盖或设置一些Spring属性(例如使用bean),如果是,如何实现?
我正在使用Spring 3的属性文件.当Spring初始化它的上下文时,它会加载属性文件并将其放在所有带有@Value注释的bean中.
我希望有可能更新文件中的某些属性,并在服务器上公开JMX,将新属性重新加载到Spring - 无需重新启动服务器,并重新加载其上下文.
我可以通过使用一些Spring方法重新加载属性并将它们填充到所有bean来实现它,或者我应该自己写这样的东西吗?
我有一个简单的应用程序,我使用几个属性文件来获取其他用户编辑的内容(链接到网站等).
我加载属性的类看起来像这样:
@Configuration
@PropertySource("classpath:salestipsWhitelist.properties")
public class SalestipsWhitelist {
@Autowired
Environment env;
public Environment getEnv() {
return env;
}
public void setEnv(Environment env) {
this.env = env;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
Run Code Online (Sandbox Code Playgroud)
一些属性文件:
UPS_MPP_M_L=True
UPS_MPP_M_M=True
UPS_MPP_M_MP=True
UPS_MPP_M_S=True
Run Code Online (Sandbox Code Playgroud)
这工作正常,但如果我对属性文件进行更改,我必须重新加载应用程序以可视化所做的任何更改.
如果我将位置移动到磁盘而不是类路径,是否有可能定期或手动重新加载?我不希望在更改时自动完成此操作,因为我想控制何时完成/更新.
我有一个spring-boot申请.在run文件夹下,还有一个额外的配置文件:
dir/config/application.properties
应用程序启动时,它使用文件中的值并将它们注入:
@Value("${my.property}")
private String prop;
Run Code Online (Sandbox Code Playgroud)
问题:如何触发重新加载这些@Value属性?我希望能够application.properties在运行时更改配置,并@Value更新字段(可能通过调用/reload应用程序内的servlet来触发更新).
但是怎么样?
到目前为止,我已经看到了通过更改属性文件本身,然后重新加载文件,使用@RefreshScope注释或使用Spring Cloud Config 来更新Spring Boot应用程序的方法。
我无法直接找到任何内容(例如getEnvironment().updateProperty(key, value))。
有这种方法吗?
例如,我需要更改应用程序的日志记录类型以显示SQL。我的application.properties文件包含以下行:spring.jpa.show-sql=false。
但是,如果我想true通过REST调用在应用程序运行时将此设置为?
您能建议一种更简单的方法吗?和/或显示有关此特定属性的示例,您将自己如何做?
编辑:更改不需要反映在application.properties文件中。因此,当应用程序重新启动时,它将使用application.properties文件中最初设置的属性。
在我的项目中,我有以下bootstrap.properties文件:
spring.application.name=vault-demo
management.endpoints.web.exposure.include=*
Run Code Online (Sandbox Code Playgroud)
除此之外,我还定义了以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
配置服务器能够访问该属性,但是当我在GitHub和POST中更新该属性时,/refresh我得到了一个403: Forbidden.我是否需要在我的应用程序或bootstrap.properties中进行任何更改?
java ×6
spring ×5
properties ×3
spring-boot ×3
cryptography ×2
encryption ×2
aes ×1
aes-gcm ×1
annotations ×1
passwords ×1
rest ×1
security ×1
spring-mvc ×1