A few days ago, a Java proxy was used in Domino to write a simple function of encrypting and validating strings using MD5.
Reference articles:
1. Java encryption algorithm Implementation use case: Http://www-128.ibm.com/developerworks/cn/java/l-security
The implementation process is as follows:
1. Encrypt the password string:
First, the generation of a MessageDigest class: MessageDigest alg= messagedigest.getinstance ("MD5");
Add information to calculate the summary: Alg.update (Password.getbytes ()); Where password is the password string
Calculate the summary: byte[] Digest=alga.digest ();
Then convert the digest into 16: Byte2hex (Digest)
/*
* Binary conversion to hexadecimal string
*
* @param b-binary array @return hexadecimal string
*/
private static String Byte2hex (byte[] bytes) {
String hs = "";
String stmp = "";
for (int i = 0; i < bytes.length; i++) {
Stmp = (java.lang.Integer.toHexString (bytes[i] & 0XFF));
if (stmp.length () = = 1)
HS = HS + "0" + stmp;
Else
HS = HS + stmp;
}
return Hs.touppercase ();
Saves the generated 16 feed summary to the database.
2. Verification:
Gets the authentication password entered: password=doc.getitemvaluestring ("f_passwordvalidate");
Get the relevant summary in the database and convert the digest:
Byte[] Digest = tobytes (curdoc.getitemvaluestring ("F_password"));
/**
* Convert string to byte[]
* @param hex the string to be converted
* @return converted byte[]
*/
public static byte[] Tobytes (String hex) {
byte[] bytes = new BYTE[16];
for (int i = 0,j=0; I
BYTES[J] = integer.valueof (hex.substring (i,i+2), Bytevalue ();
}
return bytes;
}
Finally, verify that the comparison summary is the same:
result = Isvalidate (digest, password);
if (result = = True)
Retmsg = "Success". ";
Else
Retmsg = "failed." ";
public boolean isvalidate (byte[] Digest, String password) {
try {
MessageDigest alga = messagedigest.getinstance ("MD5");
Alga.update (Password.getbytes ());
if (Messagedigest.isequal (Digest, Alga.digest ()))
return true;
Else
return false;
catch (Exception ex) {
SYSTEM.OUT.PRINTLN ("algorithm error.");
return false;
}
}
Full Java code:
/*
* Created on 2005-9-23
*
*/
Package security;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
/**
* @author stunet
*
* MD5 encryption and validation examples using Java for strings
*
*/
public class Md5digestdemo {
public static void Main (string[] args) {
try {
Make string encryption
String password = "NET";
System.out.println ("Encrypted string:" + md5digestdemo.md5digest (password));
Perform string validation
String validate = md5digestdemo.md5digest (password);
String passwordvalidate = "NET";
Boolean issuccess = Md5digestdemo.isvalidate (tobytes (Validate),
Passwordvalidate);
if (issuccess)
SYSTEM.OUT.PRINTLN ("Validation successful!");
Else
System.out.println ("Validation failed!");
catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
}
}
/**
*
* @param password
* Strings that need to be encrypted
* @return The encrypted string