xor

How to decrypt simple XOR encryption

痴心易碎 提交于 2019-11-27 19:54:37
I found the following XOR encryption function on the internet: void xor_encrypt(char *key, char *string) { int i, string_length = strlen(string); for(i=0; i<string_length; i++) { string[i]=string[i]^key[i]; printf("%i", string[i]); } } It works perfect, but I would like to decrypt the string also. For example: void xor_decrypt(char *key, char *encrypted_string) { //decrypt method goes here } So basically after I encrypt the string, I would use the same encryption key to decrypt the previously encrypted string. I'm pretty new to programming and I would just like to know how to decrypt the

XOR of three values

▼魔方 西西 提交于 2019-11-27 19:19:00
What is the simplest way to do a three-way exclusive OR? In other words, I have three values, and I want a statement that evaluates to true IFF only one of the three values is true. So far, this is what I've come up with: ((a ^ b) && (a ^ c) && !(b && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b)) Is there something simpler to do the same thing? Here's the proof that the above accomplishes the task: a = true; b = true; c = true ((a ^ b) && (a ^ c) && !(b && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b)) => false a = true; b = true;

Why is there no ^^ operator in C/C++?

 ̄綄美尐妖づ 提交于 2019-11-27 17:31:06
问题 & has && . | has || . Why doesn't ^ have ^^ ? I understand that it wouldn't be short-circuiting, but it would have different semantics. In C, true is really any non-zero value. Bitwise XOR is not always the same thing as logical XOR: int a=strcmp(str1,str2);// evaluates to 1, which is "true" int b=strcmp(str1,str3);// evaluates to 2, which is also "true" int c=a ^^ b; // this would be false, since true ^ true = false int d=a ^ b; //oops, this is true again, it is 3 (^ is bitwise) Since you

What is XOR Encryption?

落爺英雄遲暮 提交于 2019-11-27 13:04:25
问题 I have heard about people starting encryption and thought it may be something I would like, so I checked XOR and can't make any sense of it. So can someone explain to me what XOR is ? 回答1: XOR is a logical operation, pronounced exclusive or . It can be used to cipher messages simply and fast. You can see a truth table for this operation here: http://mathworld.wolfram.com/XOR.html quasi-pseudo code implementation (via http://www.evanfosmark.com/2008/06/xor-encryption-with-python/): #!/usr/bin

Why are XOR often used in java hashCode() but another bitwise operators are used rarely?

风格不统一 提交于 2019-11-27 11:33:09
I often see code like int hashCode(){ return a^b; } Why XOR? Of all bit-operations XOR has the best bit shuffling properties. This truth-table explains why: A B AND 0 0 0 0 1 0 1 0 0 1 1 1 A B OR 0 0 0 0 1 1 1 0 1 1 1 1 A B XOR 0 0 0 0 1 1 1 0 1 1 1 0 As you can see for AND and OR do a poor job at mixing bits. OR will on average produce 3/4 one-bits. AND on the other hand will produce on average 3/4 null-bits. Only XOR has an even one-bit vs. null-bit distribution. That makes it so valuable for hash-code generation. Remember that for a hash-code you want to use as much information of the key

Why does swapping values with XOR fail when using this compound form?

余生长醉 提交于 2019-11-27 11:24:38
I found this code to swap two numbers without using a third variable, using the XOR ^ operator. Code: int i = 25; int j = 36; j ^= i; i ^= j; j ^= i; Console.WriteLine("i:" + i + " j:" + j); //numbers Swapped correctly //Output: i:36 j:25 Now I changed the above code to this equivalent code. My Code: int i = 25; int j = 36; j ^= i ^= j ^= i; // I have changed to this equivalent (???). Console.WriteLine("i:" + i + " j:" + j); //Not Swapped correctly //Output: i:36 j:0 Now, I want to know, Why does my code give incorrect output? EDIT: Okay, got it. The first point to make is that obviously you

T-SQL XOR Operator

别等时光非礼了梦想. 提交于 2019-11-27 11:12:17
Is there an XOR operator or equivalent function in SQL Server (T-SQL)? There is a bitwise XOR operator - the caret (^), i.e. for: SELECT 170 ^ 75 The result is 225. For logical XOR, use the ANY keyword and NOT ALL, i.e. WHERE 5 > ANY (SELECT foo) AND NOT (5 > ALL (SELECT foo)) Using boolean algebra, it is easy to show that: A xor B = (not A and B) or (A and not B) A B | f = notA and B | g = A and notB | f or g | A xor B ----+----------------+----------------+--------+-------- 0 0 | 0 | 0 | 0 | 0 0 1 | 1 | 0 | 1 | 1 1 0 | 0 | 1 | 1 | 1 1 1 | 0 | 0 | 0 | 0 As clarified in your comment,

bitwise XOR of hex numbers in python

假如想象 提交于 2019-11-27 10:57:09
how can we XOR hex numbers in python eg. I want to xor 'ABCD' to '12EF'. answer should be B922. i used below code but it is returning garbage value def strxor(a, b): # xor two strings of different lengths if len(a) > len(b): return "".join(["%s" % (ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)]) else: return "".join(["%s" % (ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])]) key ='12ef' m1='abcd' print strxor(key,m1) Whoa. You're really over-complicating it by a very long distance. Try: >>> print hex(0x12ef ^ 0xabcd) 0xb922 You seem to be ignoring these handy facts, at least: Python has

What is the meaning of XOR in x86 assembly?

社会主义新天地 提交于 2019-11-27 10:46:30
问题 I'm getting into assembly and I keep running into xor, for example: xor ax, ax Does it just clear the register's value? 回答1: A XOR B in english would be translated as "are A and B not equal". So xor ax, ax will set ax to zero since ax is always equal to itself. A B | A XOR B 0 0 | 0 1 0 | 1 0 1 | 1 1 1 | 0 回答2: xor reg, reg is often used to clear register. It can be an alternative to mov reg, 0 AFAIR, it was faster (or shorter) in some cases. And of course, XOR itself is eXclusive OR (a.k.a.:

What's wrong with XOR encryption?

一个人想着一个人 提交于 2019-11-27 10:39:33
I wrote a short C++ program to do XOR encryption on a file, which I may use for some personal files (if it gets cracked it's no big deal - I'm just protecting against casual viewers). Basically, I take an ASCII password and repeatedly XOR the password with the data in the file. Now I'm curious, though: if someone wanted to crack this, how would they go about it? Would it take a long time? Does it depend on the length of the password (i.e., what's the big-O)? The problem with XOR encryption is that for long runs of the same characters, it is very easy to see the password. Such long runs are