我需要实现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"位:-)
Java .class文件可以很容易地反编译.如果我必须在代码中使用登录数据,我该如何保护我的数据库?
假设我试图从使用基本身份验证/基本证书的RESTful api中提取,那么在我的程序中存储该用户名和密码的最佳方法是什么?现在它只是以明文坐在那里.
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("myName@myserver","myPassword1234");
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以做到这一点更安全?
谢谢
我已经实现了一个例程,当用户提交表单时,会向管理员发送一封电子邮件。为此,我使用了 Java Mail API。我在 Microsoft Outlook 上设置了一个虚拟帐户来发送电子邮件。在代码中,我对密码进行了硬编码。我担心这会成为我托管网页时的安全问题。
这是我的代码。
我写了一个私有函数:
private void getSession(){
this.session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication
getPasswordAuthentication() {
return new PasswordAuthentication("xxxxxxxxx@outlook.com", "xxxxx_password_xxx");
}
});
}
Run Code Online (Sandbox Code Playgroud)
在我的公共execute()方法中,我调用该getSession()方法并生成消息。
public String execute() throws Exception {
getSession();
Message message = new MimeMessage(this.session);
message.setFrom(new InternetAddress("xxxxxxxxxxxxx@outlook.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("admins.email@xxxxx.com"));
message.setSubject("Form submit notification");
//...
}
Run Code Online (Sandbox Code Playgroud)
当我托管网页时,在会话方法中硬编码密码是否安全?
如果没有,那么一些实现替代方案的指针。
谢谢!
java ×4
security ×3
passwords ×2
aes ×1
cryptography ×1
decompiling ×1
encryption ×1
jakarta-mail ×1
mysql ×1