Git Product home page Git Product logo

rsa_aes's Introduction

移动端和WEB端的混合加密(RSA和AES)


流程

是先由服务器创建RSA密钥对,RSA公钥保存在安卓的so文件里面,服务器保存RSA私钥。而安卓创建AES密钥(这个密钥也是在so文件里面),并用该AES密钥加密待传送的明文数据,同时用接受的RSA公钥加密AES密钥,最后把用RSA公钥加密后的AES密钥同密文一起通过Internet传输发送到服务器。当服务器收到这个被加密的AES密钥和密文后,首先调用服务器保存的RSA私钥,并用该私钥解密加密的AES密钥,得到AES密钥。最后用该AES密钥解密密文得到明文

数据加密流程

推荐文章

Android数据加密方案

Android数据加密之RSA加密

Android数据加密之Aes加密

制作RSA公钥和私钥

  • 新建一文件夹,用终端进入到该文件夹下

效果示例

  • 打开mac自带的OpenSSL

效果示例

  • 通过如下命令生成私钥:genrsa -out rsa_private_key.pem 2048,生成了一份私钥,加密长度是2048位, 密钥长度,范围:512~2048, 内容是标准的ASCII字符

效果示例

  • 通过如下命令生成公钥:

    rsa -in rsa_private_key.pem -out rsa_public_key.pem -pubout

效果示例

  • 这样密钥就基本生成了,不过这样密钥对的私钥是无法在代码中直接使用的,要想使用它需要借助RSAPrivateKeyStructure这个类,Java是不自带的。所以为了方便使用,我们需要对私钥进行PKCS#8编码,命令如下:

pkcs8 -topk8 -in rsa_private_key.pem -out pkcs8_rsa_private_key.pem -nocrypt

效果示例

  • 所有步骤完成,最终如下图

效果示例


测试代码

	 	//得到AES加密随机生成的密钥匙
		String aesKey=  AESUtils.generateKeyString();
		System.out.println("AES秘钥为-------->>>>"+aesKey);
		System.out.println("<<<<---------------------------------------->>>>");
		//获取加密数据
		String context=initData();
		
		//AES加密生成密文
		String aesToContext=AESUtils.encrypt(context, aesKey);
		System.out.println("AES加密后密文为-------->>>>"+aesToContext);
		System.out.println("<<<<---------------------------------------->>>>");
		//获取RSA公钥
		RSAPublicKey publicKey=RSAUtils.loadPublicKey(new FileInputStream("rsa_public_key.pem文件路径"));
		//RSA公钥加密AES生成的密钥匙
		String rsaAesKey=RSAUtils.encryptByPublicKey(aesKey, publicKey);
		System.out.println("RSA加密后密钥为-------->>>>"+rsaAesKey);
		
		
		
		System.out.println("<<<<---------------------------------------->>>>");
		//获取RSA私钥路径
		RSAPrivateKey privateKey=RSAUtils.loadPrivateKey(new FileInputStream("pkcs8_rsa_private_key.pem文件路径"));
		//RSA私钥解密加密过后的AES生成的密钥匙
		String aesRKey=RSAUtils.decryptByPrivateKey(rsaAesKey, privateKey);
		System.out.println("RSA解密后密钥为-------->>>>"+aesRKey);
		String txt=AESUtils.decrypt(aesToContext, aesRKey);
		System.out.println("AES解密后密文为-------->>>>"+txt);```

rsa_aes's People

Contributors

wxmylife avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rsa_aes's Issues

android端加密,java服务端无法解密

Android端使用公钥加密,服务端私钥解密,加密后256个字节,服务端解密之后还是256个字节,按照道理来说应该是原文的长度20字节。我在安卓自己环境加解密都没问题,java端也是,但是android -> 服务端就有无法解密的问题。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.