查看“︁异或密码”︁的源代码
←
异或密码
跳转到导航
跳转到搜索
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{Unreferenced|time=2013-05-06T16:34:39+00:00 }} {{noteTA |G1=Electronics |G2=IT |G3=Communication }} '''简单异或密码'''({{lang-en|simple XOR cipher}})是[[密码学]]中一种简单的[[加密算法]],它按照如下原则进行运算: :A <math>\oplus</math> 0 = A :A <math>\oplus</math> A = 0 :(A <math>\oplus</math> B) <math>\oplus</math> C = A <math>\oplus</math> (B <math>\oplus</math> C) :(B <math>\oplus</math> A) <math>\oplus</math> A = B <math>\oplus</math> 0 = B 其中<math>\oplus</math>为[[逻辑异或]](XOR)运算的符号。按这种逻辑,文本序列的每个字符可以通过与给定的密钥进行按位异或运算来加密。如果要解密,只需要将加密後的结果与密钥再次进行按位异或运算即可。 例如,字符串“Wiki”(8位[[ASCII]]:01010111 01101001 01101011 01101001) 可以按如下的方式用密钥11110011进行加密: :{| | || <tt>01010111 01101001 01101011 01101001</tt> |- | <math>\oplus</math> || <tt>11110011 11110011 11110011 11110011</tt> |- | = || style="border-top: 1px solid black" | <tt>10100100 10011010 10011000 10011010</tt> |} 此種加密方法類似[[對稱加密]],故解密的方式如下: :{| | || <tt>10100100 10011010 10011000 10011010</tt> |- | <math>\oplus</math> || <tt>11110011 11110011 11110011 11110011</tt> |- | = || style="border-top: 1px solid black" | <tt>01010111 01101001 01101011 01101001</tt> |} 异或运算符常作为更为复杂的加密算法的组成部分。对於其本身来说,如果使用不断重复的密钥,利用[[频率分析]]就可以破解这种简单的异或密码。如果消息的内容被猜出或知道,密钥就会泄露。异或密码值得使用的原因主要是其易於实现,而且计算成本小。简单重复异或加密有时用於不需要特别安全的情况下来隐藏信息。 如果密钥是随机的(不重复),而且与消息长度相同,异或密码就会更为安全。当密钥流由[[伪随机数发生器]]生成时,结果就是[[流密码]]。若密钥是[[硬件随机序列发生器|真正随机的]],结果就是[[一次性密碼本]],這種密码在理论上是不可破解的。 在这些密码的任何部分中,密钥运算符在[[已知明文攻击]]下都是脆弱的,这是因为''明文'' <math>\oplus</math> ''密文'' = ''密钥''。 == 程式碼範例 == ===C語言=== 加密: <syntaxhighlight lang="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++; } </syntaxhighlight> 解密: <syntaxhighlight lang="C "> 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++; } </syntaxhighlight> ===golang=== <syntaxhighlight lang="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 } </syntaxhighlight> ===Python=== <syntaxhighlight lang="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') </syntaxhighlight> == 参见 == * [[吉尔伯特·维尔南|维尔南密码]] * [[维吉尼亚密码]] == 外部链接 == * {{en}}[https://web.archive.org/web/20091116202513/http://chris.dod.net/xor/xor.php Solving the Basic XOR Cipher] [[Category:流密码]] [[Category:密码學]]
该页面使用的模板:
Template:En
(
查看源代码
)
Template:Lang-en
(
查看源代码
)
Template:NoteTA
(
查看源代码
)
Template:Unreferenced
(
查看源代码
)
返回
异或密码
。
导航菜单
个人工具
登录
命名空间
页面
讨论
不转换
查看
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
特殊页面
工具
链入页面
相关更改
页面信息