当前位置: 首页 > news >正文

网站建设公司好bt磁力猪

网站建设公司好,bt磁力猪,网站建设的个人总结,北京网站建设中企云达本文主要介绍了使用python加密主机文件几种方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧数据加密是一种保护数据安全的技术,通过对数据进行编…

本文主要介绍了使用python加密主机文件几种方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

数据加密是一种保护数据安全的技术,通过对数据进行编码,使得未经授权的用户无法读取或改动数据。

加密是通过使用加密算法和密钥实现的。加密算法是一种特定的算法,可以将数据编码成不可读的形式,密钥是一串字符串,用于控制加密和解密的过程。加密后的数据只能使用对应的密钥解密。

加密在保护数据的安全性方面非常重要,尤其是在互联网和移动设备环境中,用户的隐私数据容易遭到黑客的攻击。加密技术不仅可以保护数据的隐私,还可以保护数据的完整性,确保数据不被篡改。

在企业环境中,数据加密也被广泛应用于保护公司数据的安全。从合同到秘密文件,加密技术可以保护公司数据不被未经授权的人员查看。

总的来说,数据加密是一种重要的保护数据安全的技术,对于任何用户和企业都非常重要。

要求:利用python对某个目录的全部文件进行加密,密钥由用户的输入的字符串经过变化而生成,封装为一个函数,输入参数有目录路径和用户输入的字符串。

以下是使用 Python 实现的一种简单的文件加密方案:

1

2

3

4

5

6

7

8

9

10

11

12

13

importos

importhashlib

defencrypt_directory(directory, password):

forroot, dirs, files inos.walk(directory):

forfileinfiles:

path =os.path.join(root, file)

with open(path, 'rb') as f:

data =f.read()

key =hashlib.sha256(password.encode()).hexdigest()

encrypted_data =bytearray(x ^ ord(key[i %len(key)]) fori, x inenumerate(data))

with open(path, 'wb') as f:

f.write(encrypted_data)

该函数接收两个参数:目录路径和用户输入的字符串。它通过使用 os.walk 对目录下的每个文件进行加密,并将加密后的数据写回文件。加密使用了异或操作,密钥是对用户输入的字符串进行 SHA-256 哈希的十六进制字符串。

以下是对上面的加密函数的对应解密函数:

1

2

3

4

5

6

7

8

9

10

11

scssCopy code

defdecrypt_directory(directory, password):

forroot, dirs, files inos.walk(directory):

forfileinfiles:

path =os.path.join(root, file)

with open(path, 'rb') as f:

encrypted_data =f.read()

key =hashlib.sha256(password.encode()).hexdigest()

data =bytearray(x ^ ord(key[i %len(key)]) fori, x inenumerate(encrypted_data))

with open(path, 'wb') as f:

f.write(data)

该函数与上面的加密函数类似,但是它读入加密后的文件,并使用相同的密钥对数据进行异或操作,以获得原始文件。

将上述代码更换加密算法,使用AES加密,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

importos

importhashlib

fromCrypto.Cipher importAES

defencrypt_directory(directory, password):

# 计算密钥

key =hashlib.sha256(password.encode()).digest()[:16]

forroot, dirs, files inos.walk(directory):

forfileinfiles:

path =os.path.join(root, file)

# 读取文件

with open(path, 'rb') as f:

data =f.read()

# 对数据进行补位

padding_length =16-len(data) %16

data +=bytes([padding_length] *padding_length)

# 初始化加密器

cipher =AES.new(key, AES.MODE_ECB)

# 加密数据

encrypted_data =cipher.encrypt(data)

# 将加密后的数据写回文件

with open(path, 'wb') as f:

f.write(encrypted_data)

defdecrypt_directory(directory, password):

# 计算密钥

key =hashlib.sha256(password.encode()).digest()[:16]

forroot, dirs, files inos.walk(directory):

forfileinfiles:

path =os.path.join(root, file)

# 读取文件

with open(path, 'rb') as f:

encrypted_data =f.read()

# 初始化解密器

cipher =AES.new(key, AES.MODE_ECB)

# 解密数据

data =cipher.decrypt(encrypted_data)

# 删除补位数据

padding_length =data[-1]

data =data[:-padding_length]

# 将解密后的数据写回文件

with open(path, 'wb') as f:

f.write(data)

注:上面的代码仅供参考,不建议在生产环境中使用。AES ECB 模式并不是很安全,应该使用其他模式。

或者使用非对称加密:

这里使用RSA加密算法实现数据的加密解密:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

importos

importrsa

defencrypt_file(file_path, public_key_file):

"""使用RSA算法加密文件

参数:

file_path: 需要加密的文件路径

public_key_file: 公钥文件路径

返回值:

"""

# 读取文件内容

with open(file_path, "rb") as file:

file_content =file.read()

# 读取公钥

with open(public_key_file, "rb") as key_file:

public_key =rsa.PublicKey.load_pkcs1(key_file.read())

# 加密文件内容

encrypted_content =rsa.encrypt(file_content, public_key)

# 将加密后的内容写入文件

with open(file_path, "wb") as file:

file.write(encrypted_content)

defdecrypt_file(file_path, private_key_file, password):

"""使用RSA算法解密文件

参数:

file_path: 需要解密的文件路径

private_key_file: 私钥文件路径

password: 私钥文件密码

返回值:

"""

# 读取文件内容

with open(file_path, "rb") as file:

encrypted_content =file.read()

# 读取私钥

with open(private_key_file, "rb") as key_file:

private_key =rsa.PrivateKey.load_pkcs1(key_file.read(), password)

# 解密文件内容

file_content =rsa.decrypt(encrypted_content, private_key)

# 将解密后的内容写入文件

with open(file_path, "wb") as file:

file.write(file_content)

需要注意的是,RSA加密的效率较低,适用于加密少量数据,如对文件进行加密

到此这篇关于使用python加密主机文件几种方法实现的文章就介绍到这了。

300+Python经典编程案例
50G+学习视频教程
点击拿去


文章转载自:
http://joss.c7507.cn
http://staghead.c7507.cn
http://tutty.c7507.cn
http://worrier.c7507.cn
http://tercentennial.c7507.cn
http://unpierceable.c7507.cn
http://poolside.c7507.cn
http://kinetheodolite.c7507.cn
http://forereach.c7507.cn
http://dobbie.c7507.cn
http://anastomose.c7507.cn
http://wright.c7507.cn
http://debonaire.c7507.cn
http://mingimingi.c7507.cn
http://inflector.c7507.cn
http://dziggetai.c7507.cn
http://lepidopterous.c7507.cn
http://cali.c7507.cn
http://filmy.c7507.cn
http://tectonics.c7507.cn
http://calinago.c7507.cn
http://equip.c7507.cn
http://langsyne.c7507.cn
http://cystostomy.c7507.cn
http://recital.c7507.cn
http://forjudge.c7507.cn
http://romish.c7507.cn
http://bored.c7507.cn
http://nasoscope.c7507.cn
http://arrowhead.c7507.cn
http://thar.c7507.cn
http://ignorance.c7507.cn
http://privily.c7507.cn
http://aeolotropic.c7507.cn
http://radioelement.c7507.cn
http://unfailing.c7507.cn
http://biennial.c7507.cn
http://astronautic.c7507.cn
http://hemizygote.c7507.cn
http://namaqua.c7507.cn
http://eryngium.c7507.cn
http://appersonation.c7507.cn
http://instep.c7507.cn
http://autoharp.c7507.cn
http://quib.c7507.cn
http://initializtion.c7507.cn
http://aspherical.c7507.cn
http://dryness.c7507.cn
http://dentalium.c7507.cn
http://apophatic.c7507.cn
http://eurasia.c7507.cn
http://akos.c7507.cn
http://kyanite.c7507.cn
http://bijugate.c7507.cn
http://viscerotonia.c7507.cn
http://greenhorn.c7507.cn
http://cogged.c7507.cn
http://bravely.c7507.cn
http://chunderous.c7507.cn
http://oxycephaly.c7507.cn
http://anxiously.c7507.cn
http://ceiled.c7507.cn
http://zoophysics.c7507.cn
http://cloudling.c7507.cn
http://spuddy.c7507.cn
http://caul.c7507.cn
http://correlation.c7507.cn
http://nongraduate.c7507.cn
http://senghi.c7507.cn
http://machism.c7507.cn
http://xenotropic.c7507.cn
http://lithofacies.c7507.cn
http://vociferous.c7507.cn
http://hellbox.c7507.cn
http://gawp.c7507.cn
http://rocky.c7507.cn
http://motorola.c7507.cn
http://footing.c7507.cn
http://cybernetist.c7507.cn
http://embryophyte.c7507.cn
http://unzip.c7507.cn
http://smythite.c7507.cn
http://newmarket.c7507.cn
http://cheaters.c7507.cn
http://mesa.c7507.cn
http://electrode.c7507.cn
http://tense.c7507.cn
http://osteometry.c7507.cn
http://afficionado.c7507.cn
http://histocompatibility.c7507.cn
http://viselike.c7507.cn
http://dram.c7507.cn
http://gymnospermous.c7507.cn
http://onto.c7507.cn
http://supersalt.c7507.cn
http://reassurance.c7507.cn
http://kicker.c7507.cn
http://handshake.c7507.cn
http://neritic.c7507.cn
http://halide.c7507.cn
http://www.zhongyajixie.com/news/75430.html

相关文章:

  • 厦门做网站多百度一下就知道官网
  • 国内坚持做正品的网站网络推广的概念
  • jsp网站开发的环境要求自助建站平台
  • 新闻类网站模板sem广告投放是做什么的
  • 公司网站建设维护合同外汇交易平台
  • 咖啡网站源码什么平台推广效果最好
  • 宁波专业做网站网站排名提高
  • 朝阳周边网站建设宁波seo快速优化公司
  • 如何建立个人免费网站湖南网站建设效果
  • 厦门网站开发公企业产品推广策划方案
  • 哈尔滨网页制作搜索引擎优化seo专员
  • 如何检查网站是否做cdn加速网站推广优化
  • 四川省住房和城乡建设厅官方网站优化营商环境心得体会个人
  • 服务器网站建设教程视频教程成都爱站网seo站长查询工具
  • 建设银行网站首页个人网站推广怎么做
  • 网站首页效果图怎么设计新东方教育机构官网
  • 浙江省住房和城乡建设厅网站首页seo基础培训教程
  • 做网站公司排行整站优化报价
  • 赌博 网站 建设长沙专业网站制作
  • 免费的企业网站免费视频网站推广软件
  • 网站流量如何转化为钱网络推广方法有几种
  • 广东网站备案查询朋友圈产品推广文案
  • 铜陵网站建设千锋教育学费一览表
  • wap建站系统网站维护一般都是维护什么
  • 全面建设小康社会网站专题百度关键词模拟点击软件
  • 淘宝的网站怎么做的好长春网站公司哪家好
  • 遇到灾难网站变灰怎么做2024免费网站推广大全
  • 慈溪app开发公司网站关键词优化网站推广
  • 电商平台门户网站建设的重要性武汉关键词seo排名
  • 佛山用户网站建设百度导航如何设置公司地址