当前位置:网站首页>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 !

边栏推荐
- Redis基础2(笔记)
- Detailed summary of C language game dual cache to solve the flash screen problem [easy to understand]
- dovecot 设置邮箱quota
- Come again
- JSP nine built-in objects
- JMeter websocket接口测试
- Performance debugging -- chrome performance
- 少儿编程 电子学会图形化编程等级考试Scratch一级真题解析(判断题)2022年6月
- [redis underlying parsing] linked list type
- Whether the five distribution methods will produce internal fragments and external fragments
猜你喜欢

【GO基础02】第一个程序
![[Fantan] how to design a test platform?](/img/54/5aca54c0e66f8a7c1c3215b8f06613.png)
[Fantan] how to design a test platform?

Nuclear power plants strive to maintain safety in the heat wave sweeping Europe

JMeter websocket接口测试

『Skywalking』. Net core fast access distributed link tracking platform

The second short contact of gamecloud 1608

开源的RSS订阅器FreshRSS

Why do independent sellers like to do e-mail marketing? The original conversion rate can be improved so much!
![[MAIXPY]kpu: load error:2005, ERR_ READ_ File: read file failed problem solving](/img/0b/da67b5a361a2cdfaf81568d34cf5f7.png)
[MAIXPY]kpu: load error:2005, ERR_ READ_ File: read file failed problem solving

PE format: analyze and implement IATHOOK
随机推荐
Ijcai2022 meeting! Microsoft and other tutorials on domain generalization
Animation curves are used every day. Can you make one by yourself? After reading this article, you will!
How to use RS485 half duplex chip correctly
Ability to choose
Collation of SQL statement exercises
Unity performance optimization direction
[assembly language 01] basic knowledge
Detailed summary of C language game dual cache to solve the flash screen problem [easy to understand]
字节跳动技术面都过了,结果还是被刷了,问HR原因竟是。。。
All you want to know about interface testing is here
自动化测试岗花20K招人,到最后居然没一个合适的,招两个应届生都比他们强吧
JS timer and swiper plug-in
手机端微信发朋友圈功能测试点总结
New maixhub deployment (v831 and k210)
PE format: analyze and implement IATHOOK
开户就可以购买收益在百分之六以上的理财产品了吗
[Fantan] how to design a test platform?
Tesseract OCR初探
Redis usage details
Nuclear power plants strive to maintain safety in the heat wave sweeping Europe