以下是将加密用户字符串的代码:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
import java.io.*;
class Encrypter {
public synchronized String encrypt(String plainText) throws Exception {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA");
}catch(Exception exc) {
throw new Exception(exc.getMessage());
}
try {
md.update(plainText.getBytes("UTF-8"));
}catch(Exception exc) {
throw new Exception(exc.getMessage());
}
byte raw[] = md.digest();
String hash = (new BASE64Encoder()).encode(raw);
return hash;
}
public static void main(String args[]) {
try {
Encrypter encrypter = new Encrypter();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userInput = br.readLine();
String encryptedPassword = encrypter.encrypt(userInput);
System.out.println(encryptedPassword);
} catch(Exception exc) {
System.out.println(exc);
}
}
}
当我编译代码时,我得到以下警告:
Encrypter.java:4: warning: BASE64Encoder is internal proprietary API and may be removed in a future release
import sun.misc.BASE64Encoder;
^
Encrypter.java:23: warning: BASE64Encoder is internal proprietary API and may be removed in a future release
String hash = (new BASE64Encoder()).encode(raw);
^
2 warnings
在java中有没有其他加密字符串的方法?
MessageDigest
类的方法update
做了什么?也就是说,语句md.update(plainText.getBytes("UTF-8"));
做了什么?
什么是BASE64Encoder
类?我找不到是DOC
https://stackoverflow.com/questions/9399400
复制相似问题