我有这个java代码,它只加密一个给定的文本,在代码中已经写好了,我怎样才能编辑这段代码,让程序让用户输入文本,然后对文本进行加密,并显示最终结果呢?我试着用("NagaSakti")代替文本("bismillah");,用(System.in);代替("bismillah");,但是它没有工作!!请帮帮我
import java.io.UnsupportedEncodingException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
public class DesEncrypter {
Cipher ecipher;
// 8-byte Salt
byte[] salt = {
(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
(byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
};
// Iteration count
int iterationCount = 19;
public static final DesEncrypter NAGASAKTI = new DesEncrypter("NagaSakti");
private DesEncrypter(String passPhrase) {
try {
// Create the key
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance(
"PBEWithMD5AndDES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
// Create the ciphers
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
} catch (java.security.InvalidAlgorithmParameterException e) {
} catch (java.security.spec.InvalidKeySpecException e) {
} catch (javax.crypto.NoSuchPaddingException e) {
} catch (java.security.NoSuchAlgorithmException e) {
} catch (java.security.InvalidKeyException e) {
}
}
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (javax.crypto.BadPaddingException e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingException e) {
}
return null;
}
public static void main(String args[]){
String encrypted = DesEncrypter.NAGASAKTI.encrypt("bismillah");
System.out.println("Enter your text");
System.out.println("encrypted text= "+ encrypted);
}
}发布于 2013-12-20 22:50:57
您可以使用Scanner类。添加此导入:
import java.util.Scanner;然后在您的主要方法中这样做:
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your text");
String textToEncrypt = keyboard.next();
String encrypted = DesEncrypter.NAGASAKTI.encrypt(textToEncrypt);
System.out.println("encrypted text= "+ encrypted);
}如果您想使用NagaSakti以外的密码,请更改以加密字符串开头的行.至
System.out.println("Enter your pass phrase");
String passPhrase = keyboard.next();
String encrypted = new DesEncrypter(passPhrase).encrypt(textToEncrypt);注意,您还必须将DesEncrypter构造函数更改为public。
发布于 2013-12-20 23:04:52
使用Console读取密码。main方法可能如下所示:
public static void main(String args[])
{
Console console = System.console();
if (console == null)
throw new IllegalStateException("console required");
char[] password = console.readPassword("Enter your text: ");
DesEncrypter encrypter = new DesEncrypter(new String(password));
String encrypted = encrypter.encrypt("bismillah");
System.out.println("encrypted text = " + encrypted);
}使用Console类的专用API有一些优点。
首先,不要将密码回显到屏幕上。这有助于保护它不受冲浪匪徒的影响。
此外,密码作为字符数组返回,以便在密码使用完成时,应用程序可以用零或随机字符填充数组。这减少了由于分页而将其写入磁盘或包含在堆转储中的可能性,等等。
最后,使用正确的高级API可以使您的代码非常清楚地了解您的代码在做什么,深入了解未来对该特性的任何改进,并简化您的应用程序。
所使用的加密还有许多其他问题,我不建议任何人按原样使用该代码,但我关注的是所提出的问题。
发布于 2013-12-20 22:50:49
试着做这样的事情:
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String textFromUser = null;
// read the text from the command-line; need to use try/catch with the
// readLine() method
try {
textFromUser = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your text!");
}https://stackoverflow.com/questions/20712738
复制相似问题