加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

常用加解密工具类(MD5、SHA、DES、AES、RSA)

发布时间:2020-12-15 03:13:50 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 加解密工具类,实现了常用的加解密类。包括单向加密:MD5、SHA;对称加密:DES、AES;非对称加密:RSA 完整代码见:https://git.oschina.net/bayern.c

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

加解密工具类,实现了常用的加解密类。包括单向加密:MD5、SHA;对称加密:DES、AES;非对称加密:RSA
完整代码见:https://git.oschina.net/bayern.com/SecureUtils.git??同时提供ant打包脚本。

下面展示部分关键代码????

MD5 单向加密:
	/**
	 * 返回MD5单向加密后的十六进制字符串
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public String getEncryptForHex(byte[] data) throws Exception
	{
		byte[] digestData = encrypt(data);
		StringBuffer hex = new StringBuffer();
		for(int i = 0; i < digestData.length; i++)
		{
			int h = ((int)digestData[i]) & 0XFF;
			if(h < 16)
			{
				hex.append("0");
			}
			hex.append(Integer.toHexString(h));
		}
		
		return hex.toString();
	}


DES 对称加密类:
	@Override
	public byte[] encrypt(byte[] data) throws Exception 
	{
		if(secretKey == null || "".equals(secretKey))
		{
			throw new Exception("scretKey need to exists");
		}
		
		SecretKey md5Key = getKey(secretKey);
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		cipher.init(Cipher.ENCRYPT_MODE,md5Key);
		return cipher.doFinal(data);
	}

	@Override
	public byte[] decrypt(byte[] data) throws Exception 
	{
		if(secretKey == null || "".equals(secretKey))
		{
			throw new Exception("scretKey need to exists");
		}
		
		SecretKey md5Key = getKey(secretKey);
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		cipher.init(Cipher.DECRYPT_MODE,md5Key);
		return cipher.doFinal(data);
	}



RSA 非对称加密。私钥加密 & 私钥解密 & 私钥签名
	@Override
	public byte[] encrypt(byte[] data) throws Exception 
	{
		PrivateKey rsaPrivateKey = getRSAPrivateKey();
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		cipher.init(Cipher.ENCRYPT_MODE,rsaPrivateKey);
		return cipher.doFinal(data);
	}

	@Override
	public byte[] decrypt(byte[] data) throws Exception 
	{
		PrivateKey rsaPrivateKey = getRSAPrivateKey();
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		cipher.init(Cipher.DECRYPT_MODE,rsaPrivateKey);
		return cipher.update(data);
	}

        /**
	 * 使用私钥 对数据进行签名
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public String sign(byte[] data) throws Exception
	{
		PrivateKey rsaPrivateKey = getRSAPrivateKey();
		Signature signature = Signature.getInstance(SIGN_ALGORITHM);
		signature.initSign(rsaPrivateKey);
		signature.update(data);
		return encoder(signature.sign());
	}

RSA 非对称加密。公钥加密 & 公钥解密 & 公钥校验签名
	@Override
	public byte[] encrypt(byte[] data) throws Exception
	{
		if(publicKey == null || "".equals(publicKey))
		{
			throw new Exception("publicKey is need exists");
		}
		
		PublicKey rsaPublicKey = getRSAPublicKey(publicKey);
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		cipher.init(Cipher.ENCRYPT_MODE,rsaPublicKey);
		return cipher.doFinal(data);
	}

	@Override
	public byte[] decrypt(byte[] data) throws Exception
	{
		if(publicKey == null || "".equals(publicKey))
		{
			throw new Exception("publicKey is need exists");
		}
		
		PublicKey rsaPublicKey = getRSAPublicKey(publicKey);
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		cipher.init(Cipher.DECRYPT_MODE,rsaPublicKey);
		return cipher.doFinal(data);
	}

	/**
	 * 使用公钥校验签名
	 * @param data
	 * @param sign
	 * @return
	 * @throws Exception
	 */
	public boolean verifySign(byte[] data,String sign) throws Exception
	{
		if(publicKey == null || "".equals(publicKey))
		{
			throw new Exception("publicKey is need exists");
		}
		
		PublicKey rsaPublicKey = getRSAPublicKey(publicKey);
		Signature signature = Signature.getInstance(SIGN_ALGORITHM);
		signature.initVerify(rsaPublicKey);
		signature.update(data);
		return signature.verify(decoder(sign));
	}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读