Vue与C#实现前后端RSA加密解密

前言

C#后端动态生成RSA公钥和私钥, Vue前端使用公钥对数据加密,后端使用私钥解密

vue前端

1.安装依赖

npm install jsencrypt --save

2.创建rsa.js文件 引用依赖

import { JSEncrypt } from 'jsencrypt'
 
//加密
//str 未加密字符串
export function setEncrypt(str){
  //实例化JSEncrypt
  let jse = new JSEncrypt();
  //加密公钥(由C#后端生成)
  let PUBLIC_KEY = `MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvnuBHhh9/Mwi4Z8Ea2IfVc7SJYyKkJT+4m4LBJbgxRVPa+ej1iAvJAJ++1CNfH6oVr6tw/wqEIZybTG7+iPm7SzfoW9eb/LYsLR43zLqriYWy7qAd7S/13zILiAGLUy/h9CDYwoXYaNXanVGe0iJb+NXmg7f4MAEo7rR15QHQ8zDtomPI/Fz9vz+o/xzHqsLVnxZIpn4dlCp+7QtzOePnvEda5xqWk4DNaVV2fr9gwfBFd8MyAP+CyBXLAWpmxinRU6knsLKAlqcbOLNjk2+aPNvH9EWHODKWBuMPpdDwQexD+V3/nOp8c07NzSfwXmlZuRKod9SqVY88Pg1r84WwQIDAQAB`

  jse.setPublicKey('-----BEGIN PUBLIC KEY-----' + PUBLIC_KEY + '-----END PUBLIC KEY-----');
  let  data = jse.encrypt(str.toString());  // 进行加密
  return data;
}

//解密
//params 待解密字符串
export function getDecrypt(params){
  //实例化JSEncrypt
  let jse = new JSEncrypt();
  //使用前端私钥解密(私钥由C#后端生成)
  let PRIVATE_KEY = `MIIEowIBAAKCAQEAvnuBHhh9/Mwi4Z8Ea2IfVc7SJYyKkJT+4m4LBJbgxRVPa+ej1iAvJAJ++1CNfH6oVr6tw/wqEIZybTG7+iPm7SzfoW9eb/LYsLR43zLqriYWy7qAd7S/13zILiAGLUy/h9CDYwoXYaNXanVGe0iJb+NXmg7f4MAEo7rR15QHQ8zDtomPI/Fz9vz+o/xzHqsLVnxZIpn4dlCp+7QtzOePnvEda5xqWk4DNaVV2f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7BOKqddE/Syh0dfK/FhKQ23bo0oUBH3tOipZvG/SVBdSzU5S6g+uRJf8xNOFw/c9JOnbQ6euUf2E5JYp7YN5nOuHMXVfzyF61zTjgnowKBgHqozPZH2yTU0bgQfZBsUBFB4vpUkfzB6o6qOOFQ4uiLqvW64LghE1yf7S5+XYz8ZXtIf7yb7sZcJE6XR9BD4gdYl/0KnWRL+oJQMUGDZDBzOcHiHQ8gdyACAC5hFexngJE0RNHtYcg2QjUNMfZMu/eUXJ1EjzisCT0xbwIXv0oZ`

  jse.setPrivateKey('-----BEGIN RSA PRIVATE KEY-----'+PRIVATE_KEY+'-----END RSA PRIVATE KEY-----');
  let data = jse.decrypt(params); //解密
  return data;
} 

3.创建test.vue文件使用加密和解密

<template>
    <div>
        <a-button type="primary"  @click="onTest">测试</a-button>
    </div>
</template>

<script>
    import {setEncrypt, getDecrypt} from '@/utils/rsa'
    export default {
        methods:{
            onTest() {
              let data = JSON.stringify({
                name: 'admin',
                passWord: '123456'
              })
              console.log('加密前', data)

              let encrypt = setEncrypt(data)
              console.log('加密后', encrypt)

              let decrypt = getDecrypt(encrypt)
              console.log('解密后', decrypt)
            }
        }
    }
</script>

<style lang="less" scoped>
</style>

4.结果
在这里插入图片描述

C#后端

1.生成公钥和私钥

	class Program {
		
		static void Main(string[] args) {
			Console.WriteLine("---------------------------------------------------------");
			Console.WriteLine("◆◆◆◆◆◆◆◆◆◆◆◆ RSA测试 ◆◆◆◆◆◆◆◆◆◆◆◆");
			Console.WriteLine("---------------------------------------------------------");

			CreateRsa();

			Console.WriteLine("-------------------------------------------------------------");
			Console.WriteLine("◆◆◆◆◆◆◆◆◆◆◆◆ 回车退出... ◆◆◆◆◆◆◆◆◆◆◆◆");
			Console.WriteLine();
			Console.ReadLine();
		}
		static void CreateRsa()
		{
			//指定密钥大小创建一个新的RSA
			var rsa = new RSA(1024);
			Console.WriteLine("【" + rsa.KeySize + "私钥(PKCS#1)】:");
			Console.WriteLine(rsa.ToPEM().ToPEM_PKCS1());
			Console.WriteLine();
			Console.WriteLine("【" + rsa.KeySize + "公钥(PKCS#8)】:");
			Console.WriteLine(rsa.ToPEM().ToPEM_PKCS8(true));
			Console.WriteLine();

		}
	}

生成结果
在这里插入图片描述

2.使用加密解密方法

	static void RsaTest()
	{
		string begin = "-----BEGIN RSA PRIVATE KEY-----";
		string private_key = "MIIEowIBAAKCAQEAvnuBHhh9/Mwi4Z8Ea2IfVc7SJYyKkJT+4m4LBJbgxRVPa+ej1iAvJAJ++1CNfH6oVr6tw/wqEIZybTG7+iPm7SzfoW9eb/LYsLR43zLqriYWy7qAd7S/13zILiAGLUy/h9CDYwoXYaNXanVGe0iJb+NXmg7f4MAEo7rR15QHQ8zDtomPI/Fz9vz+o/xzHqsLVnxZIpn4dlCp+7QtzOePnvEda5xqWk4DNaVV2fr9gwfBFd8MyAP+CyBXLAWpmxinRU6knsLKAlqcbOLNjk2+aPNvH9EWHODKWBuMPpdDwQexD+V3/nOp8c07NzSfwXmlZuRKod9SqVY88Pg1r84WwQIDAQABAoIBAD/Fb2vY/U0GNuNcbolY926t6FMj4zwWdeLuvoeRz7yLruRMT+CfM8kVgNe4lzXH3an0+o3zvU19BZAjJ3DOVkCvVktg0rgeEIfQU+s9y630GIuqa9onzFZjc01l8Ut3N2RL3cDpnvxUSC7fBnK9zS2Fak2sjy1ae/Mo15oZ9/itX1WYaVmDmnqopbbopfEL5UkqgwTe8KlZs7iTr5wqBqOfaTS/QU8pEXBy0m3HQwmH2KUfK9ZZOXJGPF+WZ+2OU95mFEscLNwXRZcrhO0SbcLh6dZiNZJBdu3dANsOPsPO5DUhZskLHJMeiFK3yOUcpgK6R51tTLxGYvJBHKd3nlUCgYEAxfLvsCinuORa3oUhWNaKRwa14uTAyhN1Gd6w/jGOcdM7x5vKqahD6iEMay+7B//3xNYjiPVZlyVojBxuvjDMuW9/jg3nWL7taf5WJTVQUNzE/h0vShPFQy6zskgeXYp/ceHNZzkoY3OqnW/qDrDvrLUZJLL7DM/PUXw7AJ5TH18CgYEA9lgEPuP8/h+tH6uxH8rdr4TmgsGrhProAvFw0CTSFsOlOWM2IP7U3/MYsMlWCVB8CZXgfX4z6FsWKlFT4gXwq96VaCfRyyvVQLsxZ10RyjhaZZKQS8p8n8DaRIjRh2Otqi8RJcdoNzGYGrxzZXO+H9vCOudDFRJ8skDyGQwoHd8CgYAMLzH1w+rfBJ9bRxKQZ1RHnvAfMTl0ttHkb8KxQtejxf7OAJbu7n6PsM0U4/E9QtI7eXrieUe0eAcBaNagOly/pU9SrSRqYAY5GgCvdaKl95Dgm2dHxgPEaOGCvdGwrE+/8Mg17FzCEmKUIklj8hwuo08S5Tlyxm/aci0nkpdHPQKBgQCEETXdfZ8BonvPEd34vGrTEX3/NdM9jMJb6ZLglAzp1WO7+3tmfxgjJBHlnVr5zWNiRC7BOKqddE/Syh0dfK/FhKQ23bo0oUBH3tOipZvG/SVBdSzU5S6g+uRJf8xNOFw/c9JOnbQ6euUf2E5JYp7YN5nOuHMXVfzyF61zTjgnowKBgHqozPZH2yTU0bgQfZBsUBFB4vpUkfzB6o6qOOFQ4uiLqvW64LghE1yf7S5+XYz8ZXtIf7yb7sZcJE6XR9BD4gdYl/0KnWRL+oJQMUGDZDBzOcHiHQ8gdyACAC5hFexngJE0RNHtYcg2QjUNMfZMu/eUXJ1EjzisCT0xbwIXv0oZ";
		string end = "-----END RSA PRIVATE KEY-----";
		var rsa = new RSA(begin + private_key+ end, false);
		var str = "abc内容123";
		var en = rsa.Encode(str);
		Console.WriteLine("【加密】:");
		Console.WriteLine(en);
		Console.WriteLine("【解密】:");
		Console.WriteLine(rsa.DecodeOrNull(en));
	}

加密结果
在这里插入图片描述

3.代码地址

https://github.com/xiangyuecn/RSA-csharp

前后端结果解密

1.vue解密 C#加密结果

<template>
    <div>
        <a-button type="primary"  @click="onDecrypt">解密</a-button>
    </div>
</template>

<script>
    import {setEncrypt, getDecrypt} from '@/utils/rsa'
    export default {
        methods:{
            onDecrypt() {
              let encrypt = 'Mtiax9E0dkY/yQZ3WAOpFDyiN3thkKWJHdj+HdbDalO5QY2cBbWmlC0dAeO+af2XSLXkSg1ORD5uaZXmqq6FY00YM627j9rQHMdpN3XU2YE2XtBO+Lvyv0BFAMdbsQejqig4qYhQqZ3Bv792AMkfuavYCWgAWXZd8Ag1umXgAA3triYabzEHHOEHjEqbU2Ew7WC2m/UEdE4DLqZDXSSukYWxcpvnrN+xjRDMTJte+S4p1MrgjkhS3HkNEyHIa+UKG5dpd3Yqg8fo2IGjHyyU4z86EJFu/itD4uj5e1DgyKsAM/ijqClJ9hk4F3kdiDvcX1KhGKv209xeaTAg0SYPFQ=='
              let decrypt = getDecrypt(encrypt)
              console.log('解密结果', decrypt)
            }
        }
    }
</script>

<style lang="less" scoped>
</style>

解密结果
在这里插入图片描述

2.C#解密 vue加密结果

	static void Decrypt()
		{
			string begin = "-----BEGIN RSA PRIVATE KEY-----";
			string private_key = "MIIEowIBAAKCAQEAvnuBHhh9/Mwi4Z8Ea2IfVc7SJYyKkJT+4m4LBJbgxRVPa+ej1iAvJAJ++1CNfH6oVr6tw/wqEIZybTG7+iPm7SzfoW9eb/LYsLR43zLqriYWy7qAd7S/13zILiAGLUy/h9CDYwoXYaNXanVGe0iJb+NXmg7f4MAEo7rR15QHQ8zDtomPI/Fz9vz+o/xzHqsLVnxZIpn4dlCp+7QtzOePnvEda5xqWk4DNaVV2fr9gwfBFd8MyAP+CyBXLAWpmxinRU6knsLKAlqcbOLNjk2+aPNvH9EWHODKWBuMPpdDwQexD+V3/nOp8c07NzSfwXmlZuRKod9SqVY88Pg1r84WwQIDAQABAoIBAD/Fb2vY/U0GNuNcbolY926t6FMj4zwWdeLuvoeRz7yLruRMT+CfM8kVgNe4lzXH3an0+o3zvU19BZAjJ3DOVkCvVktg0rgeEIfQU+s9y630GIuqa9onzFZjc01l8Ut3N2RL3cDpnvxUSC7fBnK9zS2Fak2sjy1ae/Mo15oZ9/itX1WYaVmDmnqopbbopfEL5UkqgwTe8KlZs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begin + private_key + end, false);

			//前端加密生成的字符串
			string str = "VjnaqXArMcDfKF+at4eW0NiG7R6FBNEua9XYpa6DaexLWuQGkp0WopH0YGBiHjnlt16j/eDTJceXSBgGcbuEr8qBFeXHnnkw8cTBmVCQZtSunvr1rMWXWfqD51hY08CIK4LqAVxwPrQFPGdD7Hj1Grf6pZOb/VrzaWzy6gxnO5tg4W7bdBHTV9jOEgAzmhSUCSqDBjI0HACzXombHq3erzlmLsugUKmTl8l70wIz5avasOlZ6akL8RBW/Jhkr6O819TAiaV1Opr9oa1FbtkcRq9z6BGGelucj4Q7b9HgIkKpM6SZ5AMb2POVq9vRUxwY/EqRjGQYxGpBTNUIp0rPiA==";
			
			Console.WriteLine("【解密】:");
			Console.WriteLine(rsa.DecodeOrNull(str));
		}

解密结果
在这里插入图片描述

Logo

前往低代码交流专区

更多推荐