RES加密解密长文本中文JAVA与JS方法,前后端通用

 更新:RSAUtils.java中

return Base64.encodeBase64URLSafeString(dataReturn);

替换为

return Base64.encodeBase64String(dataReturn);

否则java加密的内容JS解密不了

 

 

 

 

引用 jsencrypt.js (修改过的jsencrypt)
引用 Base64.js 


//测试  testText为需要加密的字符串  JS加密会有一定几率不能解密,所以可以把加密后的内容解密一次,如果解密后跟加密前的内容完全相等,则加密成功,可以把加密后的内容发给后端,如果不想等则重新加密一次再次判断
//运行10000次结果  成功次数:9456    失败次数:544

var publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDjrMVG5VOqCPw89aOrBTw7LYtP" +
	"yaKenSRufzWjp0wVAGKdHmgIBuw0YTO28myEW3j2BhEs6k6MsUyIl4bwtpkAKMU5" +
	"rG5X/KywSIw43mhmimPI9fZ/Ojf9pZou+6j8820Ay66xVFbhkg1BVt3wMthdnOql" +
	"AyQU8QtyI2/N+6ezWwIDAQAB";
var privateKeyStr = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAOOsxUblU6oI/Dz1" +
	"o6sFPDsti0/Jop6dJG5/NaOnTBUAYp0eaAgG7DRhM7bybIRbePYGESzqToyxTIiX" +
	"hvC2mQAoxTmsblf8rLBIjDjeaGaKY8j19n86N/2lmi77qPzzbQDLrrFUVuGSDUFW" +
	"3fAy2F2c6qUDJBTxC3Ijb837p7NbAgMBAAECgYAi8P8N1GA0tlBP4rsNoi4OPv0k" +
	"b6OfiCpxMuC+jwxkJLz9tA7vPHC/0TMonqf84DT+PQwhwovvl75RMEArbTqpu32U" +
	"AiHmEbV+7nErpDQ3dyquf8lHkztfPJPEKydREGZSkIeXA0oZrv/OVejjOABt1C9V" +
	"nF7XfpmZAaChFQoNkQJBAPGH8MSGpIE9d41inO187RnGAfB1viiY7+V9rh9/Me22" +
	"0jx5N7CBDQgd2BH0mD1ph5QV1vgzwWyEhH8+++TDsP0CQQDxUFX+6pj4hLzurPD3" +
	"y6rVTVs+hZs7zBetm33iVfkMJCmZlusinFWA4xvzvPoRVnT+kd7MQ3UG99hU/f/p" +
	"gXE3AkBSHGz14vA6H7NtFYFUG4xfnrk6Dk0/b/7efgOcIZnPvr9qX6tXDghoIf06" +
	"3XSELEQzS9lxN+ciBUsmFzt7YivhAkBZLjNZziFSVtvr6QX0zdQImFoMNRw1cSuf" +
	"AUP4jYVIs2ScWmXzmIsQ4t3FqiVQmv8JxabtHlD1jkroviYBidDpAkEAzbFyZN/i" +
	"NOaXvtuoOrX4LmbpNvu681lLVCIignHtCV3lz6gz4RmOiatqHuToWcVbjPXlA9Od" +
	"K0KYiCbQOkrLPg==";

var testText = "测试一下,可以放长字符串测试";

var x = 0;
var y = 0;
for (var i = 0; i < 10000; i++) {
	var enData = encrypt(testText, publicKeyStr)

	var deData = decryp(enData, privateKeyStr)

	if (deData == testText) {
		x++;
	} else {
		y++;
	}
	console.log("成功次数:" + x + "    失败次数:" + y);
}


//加密
function encrypt(text, key) {
	var base64 = new Base64();
	var encrypt = new JSEncrypt();
	encrypt.setKey(key);
	var data = encrypt.encryptUnicodeLong(base64.encode(text));
	return data;
}

//解密
function decryp(data, key) {
	var base64 = new Base64();
	var decryp = new JSEncrypt();
	decryp.setKey(key);
	var text = base64.decode(decryp.decryptUnicodeLong(data));
	return text;
}

 

java 用法:
引用 RSAUtils.java  (第5886行到6099行)

//测试java加密解密10000次,未发现失败 
//成功次数:10000    失败次数:0

String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDjrMVG5VOqCPw89aOrBTw7LYtP\n" +
        "yaKenSRufzWjp0wVAGKdHmgIBuw0YTO28myEW3j2BhEs6k6MsUyIl4bwtpkAKMU5\n" +
        "rG5X/KywSIw43mhmimPI9fZ/Ojf9pZou+6j8820Ay66xVFbhkg1BVt3wMthdnOql\n" +
        "AyQU8QtyI2/N+6ezWwIDAQAB";
String privateKeyStr = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAOOsxUblU6oI/Dz1\n" +
        "o6sFPDsti0/Jop6dJG5/NaOnTBUAYp0eaAgG7DRhM7bybIRbePYGESzqToyxTIiX\n" +
        "hvC2mQAoxTmsblf8rLBIjDjeaGaKY8j19n86N/2lmi77qPzzbQDLrrFUVuGSDUFW\n" +
        "3fAy2F2c6qUDJBTxC3Ijb837p7NbAgMBAAECgYAi8P8N1GA0tlBP4rsNoi4OPv0k\n" +
        "b6OfiCpxMuC+jwxkJLz9tA7vPHC/0TMonqf84DT+PQwhwovvl75RMEArbTqpu32U\n" +
        "AiHmEbV+7nErpDQ3dyquf8lHkztfPJPEKydREGZSkIeXA0oZrv/OVejjOABt1C9V\n" +
        "nF7XfpmZAaChFQoNkQJBAPGH8MSGpIE9d41inO187RnGAfB1viiY7+V9rh9/Me22\n" +
        "0jx5N7CBDQgd2BH0mD1ph5QV1vgzwWyEhH8+++TDsP0CQQDxUFX+6pj4hLzurPD3\n" +
        "y6rVTVs+hZs7zBetm33iVfkMJCmZlusinFWA4xvzvPoRVnT+kd7MQ3UG99hU/f/p\n" +
        "gXE3AkBSHGz14vA6H7NtFYFUG4xfnrk6Dk0/b/7efgOcIZnPvr9qX6tXDghoIf06\n" +
        "3XSELEQzS9lxN+ciBUsmFzt7YivhAkBZLjNZziFSVtvr6QX0zdQImFoMNRw1cSuf\n" +
        "AUP4jYVIs2ScWmXzmIsQ4t3FqiVQmv8JxabtHlD1jkroviYBidDpAkEAzbFyZN/i\n" +
        "NOaXvtuoOrX4LmbpNvu681lLVCIignHtCV3lz6gz4RmOiatqHuToWcVbjPXlA9Od\n" +
        "K0KYiCbQOkrLPg==";

String testText = "测试一下,可以放长字符串测试";
int x = 0;
int y =0;
for (int i = 0; i <10000 ; i++) {
     publicEncryptStr = encrypt(testText, publicKeyStr);
    if(decrypt(publicEncryptStr,privateKeyStr).equals(testText)){
        x++;
    }else{
        y++;
    }
    System.out.println("成功次数:"+x  +"    失败次数:"+y);
}

秘钥生成:http://web.chacuo.net/netrsakeypair

JS文本太长,5000多行,这里放不了,下载地址:

https://download.csdn.net/download/ink_s/12797510

你可能感兴趣的:(RES加密解密长文本中文JAVA与JS方法,前后端通用)