Security3DES
  C5bk9rm9TFUo 2023年11月02日 43 0


public class Security3DES {
	private static final String Algorithm = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish
	
	/**
	 * @Title: encryptData
	 * @Description: 加密
	 * @param cryptKey 密钥
	 * @param src
	 * @return
	 */
    public static byte[] encryptData(String cryptKey, byte[] src) {
       try {
            //添加新安全算法,如果用JCE就要把它添加进去
            //Security.addProvider(new com.sun.crypto.provider.SunJCE());
           
            SecretKey deskey = new SecretKeySpec(build3DesKey(cryptKey), Algorithm);
            Cipher cip = Cipher.getInstance(Algorithm);
            cip.init(Cipher.ENCRYPT_MODE, deskey);
            return cip.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }
    
    /**
     * @Title: decryptData
     * @Description: 解密
     * @param cryptKey 密钥
     * @param src
     * @return
     */
	public static byte[] decryptData(String cryptKey, byte[] src) {
		try {
			SecretKey deskey = new SecretKeySpec(build3DesKey(cryptKey),Algorithm);

			Cipher cip = Cipher.getInstance(Algorithm);
			cip.init(Cipher.DECRYPT_MODE, deskey);
			return cip.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}
    
	/**
	 * @Title: build3DesKey
	 * @Description: 将key转为24位byte
	 * @param keyStr
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException {
		byte[] key = new byte[24];
		byte[] temp = keyStr.getBytes("UTF-8");
		if (key.length > temp.length) {
			System.arraycopy(temp, 0, key, 0, temp.length);
		} else {
			System.arraycopy(temp, 0, key, 0, key.length);
		}
		return key;
	}
	
    public static void main(String[] args){
		try {
			String keyBytes = "012345678901234567890123";

			String szSrc = "This is a 3DES test. 测试";

			System.out.println("加密前的字符串:" + szSrc);

			byte[] encoded = encryptData(keyBytes, szSrc.getBytes());
			BASE64Encoder enc = new BASE64Encoder();
			String cipherString = enc.encode(encoded);
			System.out.println("加密后的字符串:" + cipherString);

			BASE64Decoder base64 = new BASE64Decoder();

			byte[] byStr = base64.decodeBuffer(cipherString);
			byte[] srcBytes = decryptData(keyBytes, byStr);
			System.out.println("解密后的字符串:" + (new String(srcBytes)));
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   53   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   109   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
C5bk9rm9TFUo