JAVA加密方法总结

	/**
	 * MD5加密,适用于通用的加密规则
	 * @param id
	 * @return
	 */
	public String sign(String content) {
		String contentKey = content + privateKey;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.reset();
			md.update(contentKey.getBytes("UTF-8"));
			byte[] result = md.digest();

			return HttpHelper.bytesToHexString(result);
		} catch (Exception e) {
			System.out.println(Arrays.toString(e.getStackTrace()));
		}
    	return null;
    }
	
    /**
     * MD5withRSA私钥加密
     * @param content
     * @return
     * @throws Exception
     */
    public String MD5withRSASign(String content) throws Exception {
        byte[] contentBytes = content.getBytes("utf-8");
     
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initSign(getPrivateKey(privateKey));
        signature.update(contentBytes);
        byte[] signs = signature.sign();
        
        return Base64.encodeBase64String(signs);
    }
    
    /**
     * SHA1withRSA私钥加密
     * @param content
     * @param privateKey
     * @return
     * @throws Exception
     */
    public String getSha1Sign(String content) throws Exception {
        byte[] contentBytes = content.getBytes("utf-8");
        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initSign(getPrivateKey(privateKey));
        signature.update(contentBytes);
        byte[] signs = signature.sign();
        return Base64.encodeBase64String(signs);
    }
    
    /**
     * RSA公钥加密
     * @param content
     * @return
     * @throws Exception
     */
    public String RSASign(String content) throws Exception{  
    	byte[] contentBytes = content.getBytes("utf-8");
    	
        Cipher cipher=Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));  
        return Base64.encodeBase64String(cipher.doFinal(contentBytes));  
    }  
      
    
    /**
     * 公钥转换
     * @param key
     * @return
     * @throws Exception
     */
    private static PublicKey getPublicKey(String key) throws Exception {
        byte[] keyBytes;
        keyBytes = (new BASE64Decoder()).decodeBuffer(key);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return publicKey;
  }
    /**
     * 私钥转换
     * @param key
     * @return
     * @throws Exception
     */
    private static PrivateKey getPrivateKey(String key) throws Exception {
	    byte[] keyBytes;
	    keyBytes = (new BASE64Decoder()).decodeBuffer(key);
	    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
	    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
	    PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
	    return privateKey;
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值