fbpx
维基百科

异或密码

简单异或密码(英語:simple XOR cipher)是密码学中一种简单的加密算法,它按照如下原则进行运算:

A 0 = A
A A = 0
(A B) C = A (B C)
(B A) A = B 0 = B

其中逻辑异或(XOR)运算的符号。按这种逻辑,文本序列的每个字符可以通过与给定的密钥进行按位异或运算来加密。如果要解密,只需要将加密後的结果与密钥再次进行按位异或运算即可。

例如,字符串“Wiki”(8位ASCII:01010111 01101001 01101011 01101001) 可以按如下的方式用密钥11110011进行加密:

01010111 01101001 01101011 01101001
11110011 11110011 11110011 11110011
= 10100100 10011010 10011000 10011010

此種加密方法類似對稱加密,故解密的方式如下:

10100100 10011010 10011000 10011010
11110011 11110011 11110011 11110011
= 01010111 01101001 01101011 01101001

异或运算符常作为更为复杂的加密算法的组成部分。对於其本身来说,如果使用不断重复的密钥,利用频率分析就可以破解这种简单的异或密码。如果消息的内容被猜出或知道,密钥就会泄露。异或密码值得使用的原因主要是其易於实现,而且计算成本小。简单重复异或加密有时用於不需要特别安全的情况下来隐藏信息。

如果密钥是随机的(不重复),而且与消息长度相同,异或密码就会更为安全。当密钥流由伪随机数发生器生成时,结果就是流密码。若密钥是真正随机的,结果就是一次性密碼本,這種密码在理论上是不可破解的。

在这些密码的任何部分中,密钥运算符在已知明文攻击下都是脆弱的,这是因为明文 密文 = 密钥

程式碼範例

C語言

加密:

 while (done < len) {  tmp_ch = *buffer;  for(int i = 0; i < key_len; i++)  tmp_ch ^= key[i];  *crypted = tmp_ch;  crypted++; buffer++; done++;  } 

解密:

 while (done <= len) {  tmp_ch = *buffer;  for(int i = key_len-1; i >= 0; i--)  tmp_ch ^= key[i];  *decrypted = tmp_ch;  decrypted++; buffer++; done++;  } 

golang

func XOR(input []byte, key []byte) []byte { //解密時僅需將原本的output改到input,key不變即可  output := make([]byte, len(input))  for i := range input {  output[i] = input[i] ^ key[i%len(key)] //當input比key長時會不斷使用key對每一個byte加密  }  return output } 

Python

#!/usr/bin/env python from __future__ import print_function from os import urandom def o(x): # Python 2 and 3 compatibility with bytes if isinstance(x, str): return ord(x) else: return x def genkey(length): """Generate key""" return urandom(length) def xor_strings(s,t): """xor two strings together""" return "".join(chr(o(a)^o(b)) for a,b in zip(s,t)) message = 'This is a secret message' print('message:', message) key = genkey(len(message)) print('key:', key) cipherText = xor_strings(message, key) print('cipherText:', cipherText) print('decrypted:', xor_strings(cipherText,key)) # verify if xor_strings(cipherText, key) == message: print('Unit test passed') else: print('Unit test failed') 

参见

外部链接

  • (英文)

异或密码, 此條目没有列出任何参考或来源, 2013年5月6日, 維基百科所有的內容都應該可供查證, 请协助補充可靠来源以改善这篇条目, 无法查证的內容可能會因為異議提出而移除, 简单, 英語, simple, cipher, 是密码学中一种简单的加密算法, 它按照如下原则进行运算, displaystyle, oplus, displaystyle, oplus, displaystyle, oplus, displaystyle, oplus, displaystyle, oplus, displaystyle. 此條目没有列出任何参考或来源 2013年5月6日 維基百科所有的內容都應該可供查證 请协助補充可靠来源以改善这篇条目 无法查证的內容可能會因為異議提出而移除 简单异或密码 英語 simple XOR cipher 是密码学中一种简单的加密算法 它按照如下原则进行运算 A displaystyle oplus 0 AA displaystyle oplus A 0 A displaystyle oplus B displaystyle oplus C A displaystyle oplus B displaystyle oplus C B displaystyle oplus A displaystyle oplus A B displaystyle oplus 0 B其中 displaystyle oplus 为逻辑异或 XOR 运算的符号 按这种逻辑 文本序列的每个字符可以通过与给定的密钥进行按位异或运算来加密 如果要解密 只需要将加密後的结果与密钥再次进行按位异或运算即可 例如 字符串 Wiki 8位ASCII 01010111 01101001 01101011 01101001 可以按如下的方式用密钥11110011进行加密 01010111 01101001 01101011 01101001 displaystyle oplus 11110011 11110011 11110011 11110011 10100100 10011010 10011000 10011010此種加密方法類似對稱加密 故解密的方式如下 10100100 10011010 10011000 10011010 displaystyle oplus 11110011 11110011 11110011 11110011 01010111 01101001 01101011 01101001异或运算符常作为更为复杂的加密算法的组成部分 对於其本身来说 如果使用不断重复的密钥 利用频率分析就可以破解这种简单的异或密码 如果消息的内容被猜出或知道 密钥就会泄露 异或密码值得使用的原因主要是其易於实现 而且计算成本小 简单重复异或加密有时用於不需要特别安全的情况下来隐藏信息 如果密钥是随机的 不重复 而且与消息长度相同 异或密码就会更为安全 当密钥流由伪随机数发生器 生成时 结果就是流密码 若密钥是真正随机的 结果就是一次性密碼本 這種密码在理论上是不可破解的 在这些密码的任何部分中 密钥运算符在已知明文攻击下都是脆弱的 这是因为明文 displaystyle oplus 密文 密钥 目录 1 程式碼範例 1 1 C語言 1 2 golang 1 3 Python 2 参见 3 外部链接程式碼範例 编辑C語言 编辑 加密 while done lt len tmp ch buffer for int i 0 i lt key len i tmp ch key i crypted tmp ch crypted buffer done 解密 while done lt len tmp ch buffer for int i key len 1 i gt 0 i tmp ch key i decrypted tmp ch decrypted buffer done golang 编辑 func XOR input byte key byte byte 解密時僅需將原本的output改到input key不變即可 output make byte len input for i range input output i input i key i len key 當input比key長時會不斷使用key對每一個byte加密 return output Python 编辑 usr bin env python from future import print function from os import urandom def o x Python 2 and 3 compatibility with bytes if isinstance x str return ord x else return x def genkey length Generate key return urandom length def xor strings s t xor two strings together return join chr o a o b for a b in zip s t message This is a secret message print message message key genkey len message print key key cipherText xor strings message key print cipherText cipherText print decrypted xor strings cipherText key verify if xor strings cipherText key message print Unit test passed else print Unit test failed 参见 编辑维尔南密码 维吉尼亚密码外部链接 编辑 英文 Solving the Basic XOR Cipher 取自 https zh wikipedia org w index php title 异或密码 amp oldid 68318719, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。