java 加密解密方式


一、Java常用加密方式
Base64加密算法(編碼方式)
MD5加密(消息摘要算法,驗證信息完整性)
對稱加密算法
非對稱加密算法
數字簽名算法
數字證書

加密算法:

  • 移位、替代(古典加密)
  • 對稱加密:DES、AES
  • 非對稱加密:RSA
  • 散列函數算法(單向加密):MD5、SHA、Mac
  • 數字簽名算法:RSA、DSA

其中,前三種主要完成數據的加解密;

散列函數類主要完成驗證數據的完整性,防止消息在傳遞期間被篡改;

數字簽名類:完成驗證數據的完整性,對數據來源以及收發雙方進行驗證。

二、分類

按加密算法是否需要key被分為兩類:
不基於key的有: Base64算法、MD5
基於key的有: 對稱加密算法、非對稱加密算法、數字簽名算法、數字證書、HMAC、RC4(對稱加密)
按加密算法是否可逆被分為兩類:
單向加密算法(不可解密):MD5、SHA、HMAC
非單項加密算法(可解密):BASE64、對稱加密算法、非對稱加密算法、數字簽名算法、數字證書

四、應用場景
Base64應用場景:圖片轉碼(應用於郵件,img標簽,http加密)
MD5應用場景:密碼加密、imei加密、文件校驗
非對稱加密:電商訂單付款、銀行相關業務

1、MD5(Message Digest Algorithm)加密算法

是一種單向加密算法,只能加密不能解密,示例

/**
     * MD5簡單加密
     * @param content 加密內容
     * @return String
     */
    public static String md5Encrypt(final String content) {

        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance(ALGORITHM_MD5);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
//        md5.update(text.getBytes());
        //digest()最后返回md5 hash值,返回值為8位字符串。因為md5 hash值是16位的hex值,實際上就是8位的字符
        //BigInteger函數則將8位的字符串轉換成16位hex值,用字符串來表示;得到字符串形式的hash值
        BigInteger digest = new BigInteger(md5.digest(content.getBytes()));
        //32位
        return digest.toString(16);
    }

2、BASE64進行加密/解密

通常用作對二進制數據進行加密,示例

/**
     * base64加密
     * @param content 待加密內容
     * @return byte[]
     */
    public static byte[] base64Encrypt(final String content) {
        return Base64.getEncoder().encode(content.getBytes());
    }

    /**
     * base64解密
     * @param encoderContent 已加密內容
     * @return byte[]
     */
    public static byte[] base64Decrypt(final byte[] encoderContent) {
        return Base64.getDecoder().decode(encoderContent);
    }

3、DES(Data Encryption Standard)對稱加密/解密

數據加密標准算法,和BASE64最明顯的區別就是有一個工作密鑰,該密鑰既用於加密、也用於解密,並且要求密鑰是一個長度至少大於8位的字符串,示例

/**
     * DES加密
     * @param key 秘鑰key
     * @param content 待加密內容
     * @return byte[]
     */
    public static byte[] DESEncrypt(final String key, final String content) {
        return processCipher(content.getBytes(), getSecretKey(key), Cipher.ENCRYPT_MODE , ALGORITHM_DES);
    }

    /**
     * DES解密
     * @param key 秘鑰key
     * @param encoderContent 已加密內容
     * @return byte[]
     */
    public static byte[] DESDecrypt(final String key, final byte[] encoderContent) {
        return processCipher(encoderContent, getSecretKey(key), Cipher.DECRYPT_MODE, ALGORITHM_DES);
    }

4、RSA非對稱加密/解密

非對稱加密算法的典型代表,既能加密、又能解密。和對稱加密算法比如DES的明顯區別在於用於加密、解密的密鑰是不同的。使用RSA算法,只要密鑰足夠長(一般要求1024bit),加密的信息是不能被破解的。示例

/**
     * RSA加密
     * @param content 待加密內容
     * @return byte[]
     */
    public static byte[] RSAEncrypt(final String content) {
        return processCipher(content.getBytes(), keyPair.getPrivate(), Cipher.ENCRYPT_MODE , ALGORITHM_RSA);
    }

    /**
     * RSA解密
     * @param encoderContent 已加密內容
     * @return byte[]
     */
    public static byte[] RSADecrypt(final byte[] encoderContent) {
        return processCipher(encoderContent, keyPair.getPublic(), Cipher.DECRYPT_MODE, ALGORITHM_RSA);
    }

5、SHA(Secure Hash Algorithm,安全散列算法)

數字簽名等密碼學應用中重要的工具,被廣泛地應用於電子商務等信息安全領域,示例

/**
     * SHA加密
     * @param content 待加密內容
     * @return String
     */
    public static String SHAEncrypt(final String content) {
        try {
            MessageDigest sha = MessageDigest.getInstance(ALGORITHM_SHA);
            byte[] sha_byte = sha.digest(content.getBytes());
            StringBuffer hexValue = new StringBuffer();
            for (byte b : sha_byte) {
                //將其中的每個字節轉成十六進制字符串:byte類型的數據最高位是符號位,通過和0xff進行與操作,轉換為int類型的正整數。
                String toHexString = Integer.toHexString(b & 0xff);
                hexValue.append(toHexString.length() == 1 ? "0" + toHexString : toHexString);
            }
            return hexValue.toString();

//            StringBuffer hexValue2 = new StringBuffer();
//            for (int i = 0; i < sha_byte.length; i++) {
//                int val = ((int) sha_byte[i]) & 0xff;
//                if (val < 16) {
//                    hexValue2.append("0");
//                }
//                hexValue2.append(Integer.toHexString(val));
//            }
//            return hexValue2.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
       return "";
    }

6、HMAC(Hash Message Authentication Code,散列消息鑒別碼)

使用一個密鑰生成一個固定大小的小數據塊,即MAC,並將其加入到消息中,然后傳輸。接收方利用與發送方共享的密鑰進行鑒別認證,示例

/**
     * HMAC加密
     * @param key 給定秘鑰key
     * @param content 待加密內容
     * @return String
     */
    public static byte[] HMACEncrypt(final String key, final String content) {
        try {
            SecretKey secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM_MAC);
            Mac mac = Mac.getInstance(secretKey.getAlgorithm());
            //初始化mac
            mac.init(secretKey);
            return mac.doFinal(content.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

測試代碼:

public static void main(String[] args) {
        //md5簡單加密
        String text = "i am text";
        System.out.println(EnDecoderUtil.md5Encrypt(text));

        //base64進行加密解密,通常用作對二進制數據進行加密
        byte[] base64Encrypt = EnDecoderUtil.base64Encrypt("123456789");
        String toHexString = HexUtils.toHexString(base64Encrypt);
        System.out.println(toHexString);
        byte[] base64Decrypt = EnDecoderUtil.base64Decrypt(base64Encrypt);
        System.out.println(new String(base64Decrypt));

        //DES對稱加密/解密
        //要求key至少長度為8個字符
        String key = "123456789";
        //加密
        byte[] encode_bytes = EnDecoderUtil.DESEncrypt(key, "Hello, DES");
        System.out.println(Base64.getEncoder().encodeToString(encode_bytes));
        //解密
        byte[] decode_bytes = EnDecoderUtil.DESDecrypt(key, encode_bytes);
        System.out.println(new String(decode_bytes));

        //RSA
        //數據使用私鑰加密
        byte[] en_byte = EnDecoderUtil.RSAEncrypt("Hi, RSA");
        System.out.println(Base64.getEncoder().encodeToString(en_byte));

        //用戶使用公鑰解密
        byte[] de_byte = EnDecoderUtil.RSADecrypt(en_byte);
        System.out.println(new String(de_byte));

        //服務器根據私鑰和加密數據生成數字簽名
        byte[] sign_byte = EnDecoderUtil.getSignature(en_byte);
        System.out.println(Base64.getEncoder().encodeToString(sign_byte));

        //用戶根據公鑰、加密數據驗證數據是否被修改過
        boolean verify_result = EnDecoderUtil.verifySignature(en_byte, sign_byte);
        System.out.println(verify_result);

        //SHA
        String sha = EnDecoderUtil.SHAEncrypt("Hi, RSA");
        System.out.println(sha);

        //HMAC
        byte[] mac_bytes = EnDecoderUtil.HMACEncrypt(key, "Hi, HMAC");
        System.out.println(HexUtils.toHexString(mac_bytes));
    }

三、算法介紹
1.對稱加密

對稱加密是最快速、最簡單的一種加密方式,加密(encryption)與解密(decryption)用的是同樣的密鑰(secret key)。對稱加密有很多種算法,由於它效率很高,所以被廣泛使用在很多加密協議的核心當中。

對稱加密通常使用的是相對較小的密鑰,一般小於256 bit。因為密鑰越大,加密越強,但加密與解密的過程越慢。如果你只用1 bit來做這個密鑰,那黑客們可以先試着用0來解密,不行的話就再用1解;但如果你的密鑰有1 MB大,黑客們可能永遠也無法破解,但加密和解密的過程要花費很長的時間。密鑰的大小既要照顧到安全性,也要照顧到效率,是一個trade-off。

DES(Data Encryption Standard)和TripleDES是對稱加密的兩種實現。

DES和TripleDES基本算法一致,只是TripleDES算法提供的key位數更多,加密可靠性更高。
DES使用的密鑰key為8字節,初始向量IV也是8字節。
TripleDES使用24字節的key,初始向量IV也是8字節。
兩種算法都是以8字節為一個塊進行加密,一個數據塊一個數據塊的加密,一個8字節的明文加密后的密文也是8字節。如果明文長度不為8字節的整數倍,添加值為0的字節湊滿8字節整數倍。所以加密后的密文長度一定為8字節的整數倍

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.binary.Base64;

public class DESDemo {
// 算法名稱
public static final String KEY_ALGORITHM = "DES";
// 算法名稱/加密模式/填充方式
// DES共有四種工作模式-->>ECB:電子密碼本模式、CBC:加密分組鏈接模式、CFB:加密反饋模式、OFB:輸出反饋模式
public static final String CIPHER_ALGORITHM = "DES/ECB/NoPadding";

/**
*
* 生成密鑰key對象
*
* @param KeyStr
* 密鑰字符串
* @return 密鑰對象
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws Exception
*/
private static SecretKey keyGenerator(String keyStr) throws Exception {
byte input[] = HexString2Bytes(keyStr);
DESKeySpec desKey = new DESKeySpec(input);
// 創建一個密匙工廠,然后用它把DESKeySpec轉換成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
return securekey;
}

private static int parse(char c) {
if (c >= 'a')
return (c - 'a' + 10) & 0x0f;
if (c >= 'A')
return (c - 'A' + 10) & 0x0f;
return (c - '0') & 0x0f;
}

// 從十六進制字符串到字節數組轉換
public static byte[] HexString2Bytes(String hexstr) {
byte[] b = new byte[hexstr.length() / 2];
int j = 0;
for (int i = 0; i < b.length; i++) {
char c0 = hexstr.charAt(j++);
char c1 = hexstr.charAt(j++);
b[i] = (byte) ((parse(c0) << 4) | parse(c1));
}
return b;
}

/**
* 加密數據
*
* @param data
* 待加密數據
* @param key
* 密鑰
* @return 加密后的數據
*/
public static String encrypt(String data, String key) throws Exception {
Key deskey = keyGenerator(key);
// 實例化Cipher對象,它用於完成實際的加密操作
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
SecureRandom random = new SecureRandom();
// 初始化Cipher對象,設置為加密模式
cipher.init(Cipher.ENCRYPT_MODE, deskey, random);
byte[] results = cipher.doFinal(data.getBytes());
// 該部分是為了與加解密在線測試網站(http://tripledes.online-domain-tools.com/)的十六進制結果進行核對
for (int i = 0; i < results.length; i++) {
System.out.print(results[i] + " ");
}
System.out.println();
// 執行加密操作。加密后的結果通常都會用Base64編碼進行傳輸
return Base64.encodeBase64String(results);
}

/**
* 解密數據
*
* @param data
* 待解密數據
* @param key
* 密鑰
* @return 解密后的數據
*/
public static String decrypt(String data, String key) throws Exception {
Key deskey = keyGenerator(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
// 初始化Cipher對象,設置為解密模式
cipher.init(Cipher.DECRYPT_MODE, deskey);
// 執行解密操作
return new String(cipher.doFinal(Base64.decodeBase64(data)));
}

public static void main(String[] args) throws Exception {
String source = "helloittx";
System.out.println("原文: " + source);
String key = "A1B2C3D4E5F60708";
String encryptData = encrypt(source, key);
System.out.println("加密后: " + encryptData);
String decryptData = decrypt(encryptData, key);
System.out.println("解密后: " + decryptData);
}
}

2.非對稱加密

非對稱加密為數據的加密與解密提供了一個非常安全的方法,它使用了一對密鑰,公鑰(public key)和私鑰(private key)。私鑰只能由一方安全保管,不能外泄,而公鑰則可以發給任何請求它的人。非對稱加密使用這對密鑰中的一個進行加密,而解密則需要另一個密鑰。比如,你向銀行請求公鑰,銀行將公鑰發給你,你使用公鑰對消息加密,那么只有私鑰的持有人–銀行才能對你的消息解密。與對稱加密不同的是,銀行不需要將私鑰通過網絡發送出去,因此安全性大大提高。

目前最常用的非對稱加密算法是RSA算法,是Rivest, Shamir, 和Adleman於1978年發明,他們那時都是在MIT。請看下面的例子:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.Cipher;

import com.lxh.rsatest.HexUtil;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

public class RSAEncrypt {
/** 指定加密算法為DESede */
private static String ALGORITHM = "RSA";
/** 指定key的大小 */
private static int KEYSIZE = 1024;
/** 指定公鑰存放文件 */
private static String PUBLIC_KEY_FILE = "public.keystore";
/** 指定私鑰存放文件 */
private static String PRIVATE_KEY_FILE = "private.keystore";

/**
* 生成密鑰對
*/
private static void generateKeyPair() throws Exception {
/** RSA算法要求有一個可信任的隨機數源 */
SecureRandom sr = new SecureRandom();
/** 為RSA算法創建一個KeyPairGenerator對象 */
KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM);
/** 利用上面的隨機數據源初始化這個KeyPairGenerator對象 */
kpg.initialize(KEYSIZE, sr);
/** 生成密匙對 */
KeyPair kp = kpg.generateKeyPair();
/** 得到公鑰 */
Key publicKey = kp.getPublic();
/** 得到私鑰 */
Key privateKey = kp.getPrivate();
/** 用對象流將生成的密鑰寫入文件 */
ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream(PUBLIC_KEY_FILE));
ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(PRIVATE_KEY_FILE));
oos1.writeObject(publicKey);
oos2.writeObject(privateKey);
/** 清空緩存,關閉文件輸出流 */
oos1.close();
oos2.close();
}

/**
* 生成密鑰對字符串
*/
private static void generateKeyPairString() throws Exception {
/** RSA算法要求有一個可信任的隨機數源 */
SecureRandom sr = new SecureRandom();
/** 為RSA算法創建一個KeyPairGenerator對象 */
KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM);
/** 利用上面的隨機數據源初始化這個KeyPairGenerator對象 */
kpg.initialize(KEYSIZE, sr);
/** 生成密匙對 */
KeyPair kp = kpg.generateKeyPair();
/** 得到公鑰 */
Key publicKey = kp.getPublic();
/** 得到私鑰 */
Key privateKey = kp.getPrivate();
/** 用字符串將生成的密鑰寫入文件 */

String algorithm = publicKey.getAlgorithm(); // 獲取算法
KeyFactory keyFact = KeyFactory.getInstance(algorithm);
BigInteger prime = null;
BigInteger exponent = null;

RSAPublicKeySpec keySpec = (RSAPublicKeySpec) keyFact.getKeySpec(publicKey, RSAPublicKeySpec.class);

prime = keySpec.getModulus();
exponent = keySpec.getPublicExponent();
System.out.println("公鑰模量:" + HexUtil.bytes2Hex(prime.toByteArray()));
System.out.println("公鑰指數:" + HexUtil.bytes2Hex(exponent.toByteArray()));

System.out.println(privateKey.getAlgorithm());
RSAPrivateCrtKeySpec privateKeySpec = (RSAPrivateCrtKeySpec) keyFact.getKeySpec(privateKey,
RSAPrivateCrtKeySpec.class);
BigInteger privateModulus = privateKeySpec.getModulus();
BigInteger privateExponent = privateKeySpec.getPrivateExponent();

System.out.println("私鑰模量:" + HexUtil.bytes2Hex(privateModulus.toByteArray()));
System.out.println("私鑰指數:" + HexUtil.bytes2Hex(privateExponent.toByteArray()));

}

/**
* 加密方法 source: 源數據
*/
public static String encrypt(String source) throws Exception {
generateKeyPair();
/** 將文件中的公鑰對象讀出 */
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
Key key = (Key) ois.readObject();
ois.close();

String algorithm = key.getAlgorithm(); // 獲取算法
KeyFactory keyFact = KeyFactory.getInstance(algorithm);
BigInteger prime = null;
BigInteger exponent = null;
if ("RSA".equals(algorithm)) { // 如果是RSA加密
RSAPublicKeySpec keySpec = (RSAPublicKeySpec) keyFact.getKeySpec(key, RSAPublicKeySpec.class);
prime = keySpec.getModulus();
exponent = keySpec.getPublicExponent();

// System.out.println("公鑰模量:" + HexUtil.bytes2Hex(prime.toByteArray()));
// System.out.println("公鑰指數:" + HexUtil.bytes2Hex(exponent.toByteArray()));

}

/** 得到Cipher對象來實現對源數據的RSA加密 */
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] b = source.getBytes();
/** 執行加密操作 */
byte[] b1 = cipher.doFinal(b);
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(b1);
}

/**
* 解密算法 cryptograph:密文
*/
public static String decrypt(String cryptograph) throws Exception {
/** 將文件中的私鑰對象讀出 */
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
Key key = (Key) ois.readObject();

String algorithm = key.getAlgorithm(); // 獲取算法
KeyFactory keyFact = KeyFactory.getInstance(algorithm);
RSAPrivateCrtKeySpec privateKeySpec = (RSAPrivateCrtKeySpec) keyFact.getKeySpec(key,
RSAPrivateCrtKeySpec.class);
BigInteger privateModulus = privateKeySpec.getModulus();
BigInteger privateExponent = privateKeySpec.getPrivateExponent();

// System.out.println("私鑰模量:" + HexUtil.bytes2Hex(privateModulus.toByteArray()));
// System.out.println("私鑰指數:" + HexUtil.bytes2Hex(privateExponent.toByteArray()));

/** 得到Cipher對象對已用公鑰加密的數據進行RSA解密 */
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder decoder = new BASE64Decoder();

byte[] b1 = decoder.decodeBuffer(cryptograph);
/** 執行解密操作 */
byte[] b = cipher.doFinal(b1);
return new String(b);
}

public static void main(String[] args) throws Exception {
generateKeyPair(); //生成文件形式公鑰和私鑰
//generateKeyPairString();//生成字符串形式公鑰和私鑰

String source = "非對稱加密RSA";// 要加密的字符串

String cryptograph = encrypt(source);// 生成的密文
String hexCrypt = HexUtil.bytes2Hex(cryptograph.getBytes(), false);
System.out.println("生成的密文--->" + hexCrypt);

String target = decrypt(HexUtil.hex2String(hexCrypt));// 解密密文
System.out.println("解密密文--->" + target);

}
}

雖然非對稱加密很安全,但是和對稱加密比起來,它非常的慢,所以我們還是要用對稱加密來傳送消息,但對稱加密所使用的密鑰我們可以通過非對稱加密的方式發送出去。
(1) 對稱加密加密與解密使用的是同樣的密鑰,所以速度快,但由於需要將密鑰在網絡傳輸,所以安全性不高。
(2) 非對稱加密使用了一對密鑰,公鑰與私鑰,所以安全性高,但加密與解密速度慢。
(3) 解決的辦法是將對稱加密的密鑰使用非對稱加密的公鑰進行加密,然后發送出去,接收方使用私鑰進行解密得到對稱加密的密鑰,然后雙方可以使用對稱加密來進行溝通。 

3.Base64編碼

Base 64 Encoding有什么用?舉個簡單的例子,你使用SMTP協議 (Simple Mail Transfer Protocol 簡單郵件傳輸協議)來發送郵件。因為這個協議是基於文本的協議,所以如果郵件中包含一幅圖片,我們知道圖片的存儲格式是二進制數據(binary data),而非文本格式,我們必須將二進制的數據編碼成文本格式,這時候Base 64 Encoding就派上用場了。

public void testJDKBase64(){
String encoderStr = java.util.Base64.getEncoder().encodeToString(s.getBytes());
System.out.println("encode :"+encoderStr);

String decodeStr = new String(java.util.Base64.getDecoder().decode(encoderStr));
System.out.println("decodeStr :"+decodeStr);
}

public void testCodecBase64(){
String encoderStr = org.apache.commons.codec.binary.Base64.encodeBase64String(s.getBytes());
System.out.println("encode :"+encoderStr);

String decodeStr = new String(org.apache.commons.codec.binary.Base64.decodeBase64(encoderStr));
System.out.println("decodeStr :"+decodeStr);
}

4.MD5加密

Message Digest Algorithm MD5(中文名為消息摘要算法第五版)為計算機安全領域廣泛使用的一種散列函數,用以提供消息的完整性保護。該算法的文件號為RFC 1321(R.Rivest,MIT Laboratory for Computer Science and RSA Data Security Inc. April 1992).

MD5的全稱是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest開發出來,經MD2、MD3和MD4發展而來。

MD5用於確保信息傳輸完整一致。是計算機廣泛使用的雜湊算法之一(又譯摘要算法、哈希算法),主流編程語言普遍已有MD5實現。將數據(如漢字)運算為另一固定長度值,是雜湊算法的基礎原理,MD5的前身有MD2、MD3和MD4。

MD5的作用是讓大容量信息在用數字簽名軟件簽署私人密鑰前被”壓縮”成一種保密的格式(就是把一個任意長度的字節串變換成一定長的十六進制數字串)。

 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* Java消息摘要算法 MD5 工具類,其實其他摘要算法的實現也類似
*/
public class MD5Util {
/**
* 對文本執行 md5 摘要加密, 此算法與 mysql,JavaScript生成的md5摘要進行過一致性對比.
* @param plainText
* @return 返回值中的字母為小寫
*/
public static String md5(String plainText) {
if (null == plainText) {
plainText = "";
}
String MD5Str = "";
try {
// JDK 6 支持以下6種消息摘要算法,不區分大小寫
// md5,sha(sha-1),md2,sha-256,sha-384,sha-512
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();

int i;

StringBuilder builder = new StringBuilder(32);
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
builder.append("0");
builder.append(Integer.toHexString(i));
}
MD5Str = builder.toString();
// LogUtil.println("result: " + buf.toString());// 32位的加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return MD5Str;
}
// 一個簡版測試
public static void main(String[] args) {
String m1 = md5("1");
String m2 = md5(m1);
/* 輸出為
* m1=c4ca4238a0b923820dcc509a6f75849b
* m2=28c8edde3d61a0411511d3b1866f0636
*/
System.out.println("m1="+m1);
System.out.println("m2="+m2);
}
}

通常我們不直接使用上述MD5加密。通常將MD5產生的字節數組交給Base64再加密一把,得到相應的字符串。

5.數字簽名算法

簽名:就有安全性,抗否認性
數字簽名:帶有密鑰(公鑰,私鑰)的消息摘要算法
作用:
1. 驗證數據的完整性
2. 認證數據來源
3. 抗否認

數字簽名遵循:私鑰簽名,公鑰驗證
常用的數字簽名算法:RSA,DSA,ECDSA

RSA介紹:

是經典算法,是目前為止使用最廣泛的數字簽名算法。

RSA數字簽名算法的密鑰實現與RSA的加密算法是一樣的,算法的名稱都叫RSA。密鑰的產生和轉換都是一樣的。

RSA數字簽名算法主要包括MD和SHA兩類。

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import org.apache.commons.codec.binary.Hex;

public class RSATest {
public static final String src = "hello world";

public static void main(String[] args) {
jdkRSA();

}

/**
* 說明: 用java的jdk里面相關方法實現rsa的簽名及簽名驗證
*/
public static void jdkRSA() {
try {
// 1.初始化密鑰
KeyPairGenerator keyPairGenerator = KeyPairGenerator
.getInstance("RSA");
//設置KEY的長度
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
//得到公鑰
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
//得到私鑰
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();

// 2.進行簽名
//用私鑰進行簽名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
rsaPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//構造一個privateKey
PrivateKey privateKey = keyFactory
.generatePrivate(pkcs8EncodedKeySpec);
//聲明簽名的對象
Signature signature = Signature.getInstance("MD5withRSA");
signature.initSign(privateKey);
signature.update(src.getBytes());
//進行簽名
byte[] result = signature.sign();
System.out.println("jdk rsa sign:" + Hex.encodeHexString(result));

// 3.驗證簽名
//用公鑰進行驗證簽名
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
rsaPublicKey.getEncoded());
keyFactory = KeyFactory.getInstance("RSA");
//構造一個publicKey
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
//聲明簽名對象
signature = Signature.getInstance("MD5withRSA");
signature.initVerify(publicKey);
signature.update(src.getBytes());
//驗證簽名
boolean bool = signature.verify(result);
System.out.println("jdk rsa verify:" + bool);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM