当前位置:网站首页>How to implement an app application to limit users' time use?
How to implement an app application to limit users' time use?
2022-07-25 22:03:00 【Empty bad uncle】
author : Empty bad uncle
Blog :https://xuhss.com
The breakfast shop won't be open till night , The people who want to eat have already come !
How to achieve a App Applications , Limit user time usage ?

One 、 The scene that
Suppose there's a scenario like this , You took a private job , Make a software for others , The software has no networking function . When things are done , The customer hasn't paid yet , Said to try it first . You chose to trust the customer , Sent him the software . Then he pulled you black .
To avoid that , The first thing you think of , It must be writing the expiration time into the code , time out App Automatically destroy . After the other party pays , You can extend or remove the dead time . Then recompile and send it to the customer .
But the problem is , It's very troublesome to recompile the code and send it to the user every time , Is there an easier way to ? Can software always be a software , But give the user a registration code , The registration code is marked with the effective time . Wait until it expires , Just give the user a new registration code , You can continue to use .
See here , Some students will think , How to specify the validity period in the registration code ? First of all, this time must not be in clear text , Otherwise, the user will change it , Can't it be extended by itself .
But if encrypted , We must put the decryption algorithm into the software , Once the user preliminarily decompiles the program , You can get the decryption algorithm or symmetric encryption key .
therefore , We can only use asymmetric encryption . And asymmetric encryption , Encryption by public key , Decrypt with private key . If we want the software to decode the effective time from the registration code , Do you want to put the private key in the software ?
The private key cannot be disclosed , Therefore, what can only be put into the software is the public key . But can we use private key encryption , Decryption with public key ?
actually , You can really do this , But this is not called private key encryption, public key decryption , This is called private key signature (sign), Public key verification (verify). also , There is one advantage to using this method , That is, the effective time can be stored directly in plaintext , Not afraid of user modification . Because once modified , The signature doesn't match .
Two 、PyCryptodome Libraries installed
Suppose we have a string message, Use private key , You can sign this string , Get a signature string signature. And we use the public key , Can verify message Whether the signature string can be generated signature. If message Changes have taken place , perhaps signature Changes have taken place , perhaps message and signature At the same time, changes have taken place , Public key verification will fail .
Each language has a third-party library related to asymmetric encryption . We use it Python Medium PyCryptodome To demonstrate .
First , We are macOS below , Generate a pair of public and private keys :
ssh-keygen -t rsa
Just enter the storage path of the key according to the prompt , As shown in the figure below :

In the current folder , Generated private key sign And public key sign.pub.
Next , Use pip install PyCryptodome:
pip install pycryptodome
3、 ... and 、 The actual demonstration limits the user's use time
Next , Import public and private keys :
>>> from Crypto.PublicKey import RSA
>>> with open('sign') as f:
... private = f.read()
...
>>> with open('sign.pub') as f:
... public = f.read()
...
>>> private_key = RSA.import_key(private)
>>> public_key = RSA.import_key(public)
Because the key we generated before is SHA256 Algorithm , So we need to use SHA256 The algorithm generates a summary of the data that needs to be signed . This step needs to be done when signing and verifying the signature .
>>> from Crypto.Hash import SHA256
>>> digest = SHA256.new()
>>> message = 'expire: 2022-03-01'
>>> digest.update(message.encode())
Next , Sign this data :
>>> import base64
>>> from Crypto.Signature import PKCS1_v1_5
>>> signer = PKCS1_v1_5.new(private_key)
>>> code = signer.sign(digest)
>>> signature = base64.b64encode(code)
>>> print(signature.decode())
The operation effect is shown in the figure below :

Now? , You just need to put the string expire: 2022-03-01 And signature string xbelbTNpq8M... A long string ... Just send it to the customer .
The customer inputs the expiration time string and signature string into the software , The software uses the public key to verify whether the string is signed by its corresponding private key :
>>> message = 'expire: 2022-03-01'
>>> signature = 'xbelbTNpq8MCFkSxGBoTq7SwQ+oqHRAObrj5p8K2gyY+7uWs5dXGjsQ+GP2XTS5YskCtGjYIBZmAmeM5ey69lRQyk5S1m7t68pYNbUvf3o39Ym0rcmK7XGkBh3euZzVeRErs4JCl7ffTbfcqM4aAsWldDKESrZvaDNQ5DkC8VRYHPBfZfScHqPw/zcHCMRhC9Dch8j9eQlnk8/UKY0MM92jXT4map94PzZRfMLkD4vsciZTtMJm4a42UiiWDUpA6zIgQCYru2YyKspS1uZFE51atYP5DcgPWvJUVRDJS/ZjdPfi9chRjx0dS/Df1sFEreZ7myzXAJP7Y8FA6rvi7EZLlHZ1ViM9tTJp9ut/ZlKgnPAuDCp1JSyKMUk/doVqzUjTqTNHuORe+p3Hhb+xkCASyD8eUH+CyEDVLRcDkSMH5U3o/uONnOQao2o9dbkGiSYNkToElQJ2v20S3MnncPciij8H7iI2dDp1dwt8bkcZOD+E1Tf88LMvRaxB7YnhJ'
>>> digest = SHA256.new()
>>> digest.update(message.encode())
>>> reader = PKCS1_v1_5.new(public_key)
>>> reader.verify(digest, base64.b64decode(signature.encode()))
True
But if you tamper message The content of , Then the verification will fail , As shown in the figure below :

After the first verification of the software , You can save the expiration time string and the signature string to the hard disk in the form of a file , Check it every time you start the software .
It runs normally when it is found to be legal and not expired , If it is found that it is expired or illegal, the dialog box for entering the registration code will pop up again .
Four 、 summary
- In this paper, complete MFC The program only runs a single instance Simple example .
- If you think the article is useful to you , Remember
give the thumbs-upCollectionforwardA wave , Bloggers also support making exclusive dynamic wallpapers for iron fans ~
Share high-quality articles in previous periods
- C++ QT combination FFmpeg Actual development of video player -01 Environment installation and project deployment
- solve QT problem : function qmake:Project ERROR: Cannot run compiler ‘cl‘. Output:
- Resolve installation QT after MSVC2015 64bit No compiler and debugger issues with configuration
- Qt Kit tips in no complier set in kit and no debugger, The yellow exclamation mark appears and the problem is solved (MSVC2017)
- Python+selenium automation - Realize automatic import 、 Upload external files ( Don't pop up windows window )
High quality tutorial sharing
- If you don't enjoy reading the article , You can come to my other special column Take a look ~
- For example, the following columns :Python Actual wechat ordering applet 、Python Quantitative trading practice 、C++ QT Practical projects and Algorithm learning column
- You can learn more about C++/Python Relevant contents of ! Directly click on the color font below to jump !
| Learning route guidance ( Click unlock ) | Knowledge orientation | Crowd positioning |
|---|---|---|
| 🧡 Python Actual wechat ordering applet 🧡 | Progressive class | This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system . |
| Python Quantitative trading practice | beginner | Take you hand in hand to create an easy to expand 、 More secure 、 More efficient Quantitative trading System |
| ️ C++ QT combination FFmpeg Actual development of video player ️ | The difficulty is high | Sharing learning QT Finished video player source code , We need to have a solid C++ knowledge ! |
| A community of 90000 game lovers | Help each other / Blow water | 90000 game lovers community , Chat and help each other , White whoring prize |
| Python Zero basis to introduction | Python beginner | For small partners who have not been systematically studied , The core purpose is to enable us to learn quickly Python Knowledge to get started |
Data white whoring , reminder
Follow the card below to get more programming knowledge immediately , Including various language learning materials , Thousands of sets PPT Templates and various game source materials and so on . More information can be viewed by yourself !

边栏推荐
- Share | intelligent fire emergency management platform solution (PDF attached)
- Tesseract OCR初探
- 卸载npm和安装npm_使用`npm uninstall`卸载npm软件包「建议收藏」
- 2022最新软件测试八股文,能不能拿心仪Offer就看你背得怎样了
- YUV420 YUV420sp 图像格式「建议收藏」
- Open source RSS subscriber freshrss
- [51Nod1676 无向图同构]无向图哈希[通俗易懂]
- 【饭谈】如何设计好一款测试平台?
- 【饭谈】测试平台为什么有组件化?模块化?很多看不到的地方设计的很好是种浪费么?
- 在进行自动化测试,遇到验证码的问题,怎么办?
猜你喜欢

自动化测试岗花20K招人,到最后居然没一个合适的,招两个应届生都比他们强吧

Sofa weekly | open source person - Niu Xuewei, QA this week, contributor this week

五种分配方式是否会产生内部碎片、外部碎片

核电站在席卷欧洲的热浪中努力保持安全工作

The technical aspects of ByteDance are all over, but the result is still brushed. Ask HR why...

After three years of software testing at Tencent, I was ruthlessly dismissed in July, trying to wake up my brother who was paddling

信息安全建设原则指导

Shopify sellers: share some tips for social media marketing!

The automation testing post spent 20K recruiting, but in the end, there was no suitable one. Both fresh students are better than them

『SignalR』. Net using signalr for real-time communication
随机推荐
Redis master-slave architecture lock failure problem (master-slave)
JSP nine built-in objects
What should I do if I encounter the problem of verification code during automatic testing?
【饭谈】软件测试薪资层次和分段(修仙)
Redis为何选择单线程?
【饭谈】Web3.0到来后,测试人员该何去何从?(十条预言和建议)
8000 word super detailed custom structure type
Wet- a good choice for people with English difficulties - console translation
Detailed summary of C language game dual cache to solve the flash screen problem [easy to understand]
[redis underlying parsing] string type
[redis underlying parsing] linked list type
3. Editors (vim)
Unity performance optimization direction
El expression improves JSP
Redis 使用详解
『Skywalking』.NET Core快速接入分布式链路追踪平台
Guys, how can Flink SQL submit tasks in per job mode?
JMeter websocket interface test
c sqlite ... ...
卸载npm和安装npm_使用`npm uninstall`卸载npm软件包「建议收藏」