问题描述
我正在寻找一种方法来加密由Java程序读取的配置文件
中的密码。目前,我从文本文件中读入
密码,但是如果有人要查看配置文件,那么将密码置于
出来。
I am looking for a way to encrypt a password in a configuration filethat is being read by a Java program. Currently, I read-in thepassword from the text file, but that leaves the password sitting rightout in the open if someone were to look at the config file.
我正在考虑构建一个简单的类,用户可以在其中键入
他们所需的密码,获取密码的加密版本,然后$ b $ b将加密版本粘贴到配置文本文件中。然后
应用程序将读取加密的密码,将密码从
解密为字符串,然后继续。
I was thinking of building a simple class where user could type intheir desired password, get an encrypted version of the password, thenpaste the encrypted version into the configuration text file. Then theapplication would read encrypted password, decrypt the password backinto a string, and move on.
我遇到麻烦的字符串 - > encryptedtped bytes - > string
conversion。
I am having trouble with the string-->encrytped bytes-->stringconversions.
我正在使用内置的java安全类来实现这个代码。
这里是一些示例测试代码:
I am using the built-in java security classes to implement this code.Here is some sample test code:
// Reads password from config file
String password = ScriptConfig.getString( "password" );
// Generate Key
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
// Create Encryption cipher
Cipher cipher = Cipher.getInstance( "DES" );
cipher.init( Cipher.ENCRYPT_MODE, key );
// Encrypt password
byte[] encrypted = cipher.doFinal( password.getBytes() );
// Create decryption cipher
cipher.init( Cipher.DECRYPT_MODE, key );
byte[] decrypted = cipher.doFinal( encrypted );
// Convert byte[] to String
String decryptedString = new String(decrypted);
System.out.println("password: " + password);
System.out.println("encrypted: " + encrypted);
System.out.println("decrypted: " + decryptedString);
// Read encrypted string from config file
String encryptedPassword = ScriptConfig.getString( "encryptedPassword"
);
// Convert encryptedPassword string into byte[]
byte[] encryptedPasswordBytes = new byte[1024];
encryptedPasswordBytes = encryptedPassword.getBytes();
// Decrypt encrypted password from config file
byte[] decryptedPassword = cipher.doFinal( encryptedPasswordBytes );//error here
System.out.println("encryptedPassword: " + encryptedPassword);
System.out.println("decryptedPassword: " + decryptedPassword);
The config file has the following variables:
password=password
encryptedPassword=[B@2a4983
When I run the code, I get the following output:
password: passwd
encrypted: [B@2a4983
decrypted: passwd
javax.crypto.IllegalBlockSizeException: Input length must be multiple
of 8 when decrypting with padded cipher
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(Da shoA12275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at com.sapient.fbi.uid.TestEncryption.main(TestEncryp tion.java:4
任何关于我正在使用的错误,结构或过程的帮助一个非常简单的解决方案将是使用Base64编码,请参阅这个
将会很棒,谢谢。
Any help on the error, structure, or process I am using to do thiswould be great. Thanks.
推荐答案
代码片段如下: -
A really simple solution would be to use Base64 encoding, see the code snippet below :-
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
...
private String encode(String str) {
BASE64Encoder encoder = new BASE64Encoder();
str = new String(encoder.encodeBuffer(str.getBytes()));
return str;
}
private String decode(String str) {
BASE64Decoder decoder = new BASE64Decoder();
try {
str = new String(decoder.decodeBuffer(str));
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
...
这篇关于加密和解密java中的属性文件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!