java DES加密,ECB模式,PKCS7扩展规则

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java DES加密,ECB模式,PKCS7扩展规则相关的知识,希望对你有一定的参考价值。



import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
 * DES加密,ECB模式,PKCS7扩展规则
 * @author wangbiao
 *
 */
public class DESecbpkcs7 {
	
	private Cipher cipher = null;
	// 密钥
	private String key = "";
	
	
	public DESecbpkcs7(String key, int mode){
		this.key = key;
		cipher = initCipher(mode);
	}

	private final Cipher initCipher(int mode) {
		try {
			// 添加新安全算法:PKCS7
			Security.addProvider(new BouncyCastleProvider());
			String algorithm = "DESede/ECB/PKCS7Padding";
			SecretKey desKey = new SecretKeySpec(Base64.decode(this.key, Base64.DEFAULT), algorithm);
			Cipher tcipher = Cipher.getInstance(algorithm);
			tcipher.init(mode, desKey);
			return tcipher;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


    /**
     * 字符串转换成十六进制值
     * @param bin String 我们看到的要转换成十六进制的字符串
     * @return 
     */
	private static String bin2hex(String bin) {
        char[] digital = "0123456789abcdef".toCharArray();
        StringBuffer sb = new StringBuffer("");
        byte[] bs = bin.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(digital[bit]);
            bit = bs[i] & 0x0f;
            sb.append(digital[bit]);
        }
        return sb.toString();
    }
 
    /**
     * 十六进制转换字符串
     * @param hex String 十六进制
     * @return String 转换后的字符串
     */
	private static String hex2bin(String hex) {
        String digital = "0123456789abcdef";
        char[] hex2char = hex.toCharArray();
        byte[] bytes = new byte[hex.length() / 2];
        int temp;
        for (int i = 0; i < bytes.length; i++) {
            temp = digital.indexOf(hex2char[2 * i]) * 16;
            temp += digital.indexOf(hex2char[2 * i + 1]);
            bytes[i] = (byte) (temp & 0xff);
        }
        return new String(bytes);
    }
    
	/**
	 * 加密以charset编码做为密文
	 * 
	 * @param src 明文
	 * @return
	 */
	public String encryptDesEcbPKCS7Padding(String src) {
		cipher = initCipher(Cipher.ENCRYPT_MODE);
		try {
			byte[] er = cipher.doFinal(src.getBytes());
			byte[] bt = Base64.encode(er, Base64.DEFAULT);
			String str = new String(bt,"ASCII");
			str = str.replaceAll("\r", "");  
	        str = str.replaceAll("\n", "");
	        str = bin2hex(str);
			return str;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	/**
	 * 解密 
	 * @param src  密文
	 * @return
	 * @throws Exception
	 */
	public String decryptDesEcbPKCS7Padding(String src) throws Exception {
		src = hex2bin(src);
		byte[] bt = Base64.decode(src, Base64.DEFAULT);
		byte[] dd = cipher.doFinal(bt);
		return new String(dd);
	}

	
	public static void main(String[] args) throws Exception {
		DESecbpkcs7 cWebService3DES = new DESecbpkcs7("MjMyZTJjNjM1MzQ0MzIzMA==", Cipher.ENCRYPT_MODE);
		String s = cWebService3DES.encryptDesEcbPKCS7Padding("371321199002017433");
		System.out.println(s);
		s = cWebService3DES.decryptDesEcbPKCS7Padding(s);
		System.out.println(s);
	}

}

以上是关于java DES加密,ECB模式,PKCS7扩展规则的主要内容,如果未能解决你的问题,请参考以下文章

如何用Java进行3DES加密解密

常用的加密算法

常用的加密算法

php aes ecb pkcs7padding 加密

DES加密模式详解

JAVA和.NET使用DES对称加密的区别