⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encryptutils.java

📁 Ftp服务1.0
💻 JAVA
字号:
package ranab.util;

import java.security.*;

/**
 * String encryption utility methods.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */

public
class EncryptUtils {
    
    /**
     * Encrypt byte array.
     */
    public static byte[] encrypt(byte[] source, String algorithm) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        md.reset();
        md.update(source);
        return md.digest();
    }
    
    /**
     * Encrypt string
     */
    public static String encrypt(String source, String algorithm) throws NoSuchAlgorithmException {
        byte[] resByteArray = encrypt(source.getBytes(), algorithm);
        return StringUtils.toHexString(resByteArray);
    }
    
    /**
     * Encrypt string using MD5 algorithm
     */
    public static String encryptMD5(String source) {
        if (source == null) {            source = "";        }
    
        String result = "";
        try {
            result = encrypt(source, "MD5");
        }
        catch(NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return result;
    }
    
    /**
     * Encrypt string using SHA algorithm
     */
    public static String encryptSHA(String source) {
        if (source == null) {            source = "";        }
    
        String result = "";
        try {
            result = encrypt(source, "SHA");
        }
        catch(NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return result;
    }
     
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -