Encryption Classes

Encryption Classes

Are there any standard classes for performing one-way encryption ofstrings (e.g., passwords) in Java, similar to crypt() on Unix systems?

The Java core APIs do not include an implementation of the DESencryption used by the Unix crypt() function. However, one-wayencryption functionality, in the form of message digests, such as MD5,is provided. In fact, MD5 is the standard password encryptionalgorithm on some of the free BSD distributions and the option ofusing MD5 is available on most Linux distributions.

The java.securitypackage contains the MessageDigest class, which can be used to performone-way encryption. MessageDigests cannot be directly instantiated andare instead created by calling the getInstance() factory method.Encryption is performed by passing as an argument to the digest()method the raw bytes of a string. The accompanying programdemonstrates how to do this. It prints out the MD5 digest of each ofits arguments on a separate line.

import*;/** * Prints to standard output the MD5 digest of its arguments, each on * a separate line. */public class MD5Digest {  private MessageDigest __md5;  private StringBuffer __digestBuffer;  public MD5Digest() throws NoSuchAlgorithmException {    __md5 = MessageDigest.getInstance("MD5");    __digestBuffer = new StringBuffer();  }  public String md5crypt(String password) {    int index;    byte[] digest;    __digestBuffer.setLength(0);    digest = __md5.digest(password.getBytes());    for(index = 0; index 


Share the Post: