RSA算法加解密

代码如下,RSA算法因为其解密的难度性是在支付行业很常见的的加密算法,java自带有自己的RSA算法包来对数据进行加解密,只需要导入相应的包就可以到达相应的效果。

//解锁用户 ok
	@Test
	public void testUnlockMember(){
		try{
			System.out.println("testUnlockMember start");

			JSONObject param = new JSONObject();
			param.put("bizUserId", bizUserId);

			System.out.println("request:" + param);
			JSONObject response = client.request(soaName, "unlockMember", param);
			System.out.println("response:" + response);

			System.out.println("testUnlockMember end");
		}catch(Exception e){
			System.out.println("testUnlockMember error");
			e.printStackTrace();
		}
	}

	//RSA加密
	@Test
	public void testRsaEncrypt() throws Exception{
		try{
			System.out.println("rsaEncrypt start");

			String str = "";

			RSAUtil rsaUtil = new RSAUtil((RSAPublicKey)publicKey, (RSAPrivateKey)privateKey);
			String encryptStr = rsaUtil.encrypt(str);
			System.out.println(encryptStr);
		}catch(Exception e){
			System.out.println("rsaEncrypt error");
			e.printStackTrace();
			throw e;
		}
	}

	//RSA加密
	public String rsaEncrypt(String str) throws Exception{
		try{
			System.out.println("rsaEncrypt start");

			RSAUtil rsaUtil = new RSAUtil((RSAPublicKey)publicKey, (RSAPrivateKey)privateKey);
			String encryptStr = rsaUtil.encrypt(str);
			return encryptStr;
		}catch(Exception e){
			System.out.println("rsaEncrypt error");
			e.printStackTrace();
			throw e;
		}
	}

	//RSA解密
	@Test
	public void testRsaDecrypt() throws Exception{
		try{
			System.out.println("rsaDecrypt start");

			String signStr = "";
			RSAUtil rsaUtil = new RSAUtil((RSAPublicKey)publicKey, (RSAPrivateKey)privateKey);
			String dencryptStr = rsaUtil.dencrypt(signStr);
			System.out.println("解密:" + dencryptStr);
		}catch(Exception e){
			System.out.println("rsaDecrypt error");
			e.printStackTrace();
			throw e;
		}
	}
}

  

//解锁用户 ok@Testpublic void testUnlockMember(){try{System.out.println("testUnlockMember start");
JSONObject param = new JSONObject();param.put("bizUserId", bizUserId);
System.out.println("request:" + param);JSONObject response = client.request(soaName, "unlockMember", param);System.out.println("response:" + response);
System.out.println("testUnlockMember end");}catch(Exception e){System.out.println("testUnlockMember error");e.printStackTrace();}}
//RSA加密@Testpublic void testRsaEncrypt() throws Exception{try{System.out.println("rsaEncrypt start");
String str = "";
RSAUtil rsaUtil = new RSAUtil((RSAPublicKey)publicKey, (RSAPrivateKey)privateKey);String encryptStr = rsaUtil.encrypt(str);System.out.println(encryptStr);}catch(Exception e){System.out.println("rsaEncrypt error");e.printStackTrace();throw e;}}
//RSA加密public String rsaEncrypt(String str) throws Exception{try{System.out.println("rsaEncrypt start");
RSAUtil rsaUtil = new RSAUtil((RSAPublicKey)publicKey, (RSAPrivateKey)privateKey);String encryptStr = rsaUtil.encrypt(str);return encryptStr;}catch(Exception e){System.out.println("rsaEncrypt error");e.printStackTrace();throw e;}}
//RSA解密@Testpublic void testRsaDecrypt() throws Exception{try{System.out.println("rsaDecrypt start");
String signStr = "";RSAUtil rsaUtil = new RSAUtil((RSAPublicKey)publicKey, (RSAPrivateKey)privateKey);String dencryptStr = rsaUtil.dencrypt(signStr);System.out.println("解密:" + dencryptStr);}catch(Exception e){System.out.println("rsaDecrypt error");e.printStackTrace();throw e;}}}

猜你喜欢

转载自www.cnblogs.com/jourage/p/10319685.html