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

边栏推荐
- Animation curves are used every day. Can you make one by yourself? After reading this article, you will!
- 『Skywalking』. Net core fast access distributed link tracking platform
- 【测开方法论】测开平台pk心得-抉择
- [leetcode ladder] linked list · 876 find the middle node of the linked list
- dovecot 设置邮箱quota
- [fan Tan] after the arrival of Web3.0, where should testers go? (ten predictions and suggestions)
- All you want to know about interface testing is here
- How to use RS485 half duplex chip correctly
- The dragon lizard exhibition area plays a new trick this time. Let's see whose DNA moved?
- [fan Tan] in detail: lower control, upward management, upward drawing cake.
猜你喜欢

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

『SignalR』.NET使用 SignalR 进行实时通信初体验

如何实现一个App应用程序,限制用户时间使用?
![[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

2022 the latest software tests eight part essay. Whether you can offer depends on how you recite it

What should I do if I encounter the problem of verification code during automatic testing?

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

2 lines of code to generate a solid desktop background

【饭谈】那些看似为公司着想,实际却让人无法理解的事(二:面试时的软素质“眼缘”)

JMeter websocket接口测试
随机推荐
How to quickly build a picture server [easy to understand]
Automatic assembly and fuse degradation of feign
JSP nine built-in objects
在进行自动化测试,遇到验证码的问题,怎么办?
[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决
EL表达式改进JSP
[test development methodology] experience of test development platform PK - choice
3. Editors (vim)
Why does redisv6.0 introduce multithreading?
ZigBee development board (nxpzigbee Development)
如何实现一个App应用程序,限制用户时间使用?
Redis为何选择单线程?
Excuse me, how to deal with repeated consumption of MySQL data
【饭谈】软件测试薪资层次和分段(修仙)
C语言:随机生成数+冒泡排序
Ijcai2022 meeting! Microsoft and other tutorials on domain generalization
Guiding principles of information security construction
【leetcode天梯】链表 · 021 合并两个有序链表
Preliminary study on Tesseract OCR
jenkins+SVN配置