Simple XOR Encryption/Decryption in C++ (And Several Other Languages)
For details on how to implement XOR encryption using Go, see this post.
If you are looking for XOR encryption for other languages, including C, C#, Dart, Go, Groovy, Java (Android Compatible), JavaScript, Objective-C, and Python, I have made them available at this GitHub repo.
XOR encryption (or Exclusive-OR encryption) is a common method of encrypting text into a format that cannot be trivially cracked by the average person. XOR encryption is great for storing things like game save data, and other data types that are stored locally on a users computer, that while not a big deal if they are tampered with, you would like to deter people from doing so. XOR encryption is also used often as a part of more complex encryption algorithms.
The idea behind it is that if you don't know the original character or the XOR encryption key, it is impossible to determine what either one is. However, the reason that it is not entirely secure is that data almost always contains patterns (JSON uses '{' and '}' characters, XML contains plenty of '<' and '>' characters, etc.) so if someone is able to determine the pattern and unlock even one character, they will have the key to unlocking everything else.
However secure or insecure XOR encryption really is, it has plenty of valid use cases. Any kind of deterrent added to data that you don't want users to tamper with but that they will have easy access to is a prime candidate, so long as security isn't paramount.
The concept is simple, you define a key character, and for every character in the string you want to encrypt, you apply the key. Once you want to unencrypt the encrypted data, you simply go through the string and apply the key again.
Here's a very simple implementation in C++, which uses the ^ character for XOR:
#include<iostream> usingnamespace std; string encryptDecrypt(stringtoEncrypt) { char key = 'K'; //Any char will work string output = toEncrypt; for (int i = 0; i < toEncrypt.size(); i++) output[i] = toEncrypt[i] ^ key; return output; } int main(intargc, constchar * argv[]) { string encrypted = encryptDecrypt("kylewbanks.com"); cout << "Encrypted:" << encrypted << "\n"; string decrypted = encryptDecrypt(encrypted); cout << "Decrypted:" << decrypted << "\n"; return 0; } |
And here's the output:
Encrypted: 2'.<)*% 8e($& Decrypted:kylewbanks.com |
As you can see, the encrypted string looks like gibberish, and would deter non-technical people from bothering to tamper with the file. However, if you run something through that algorithm with repetitive characters (JSON, XML, etc.), more tech-savvy individuals may be able to pick up on what you are doing. While you can't quite make it unbreakable, you can make it ridiculously hard to brute-force by using multiple keys in a pattern like so:
string encryptDecrypt(string toEncrypt) { char key[3] = { 'K', 'C', 'Q' }; //Any chars will work string output = toEncrypt; for (int i = 0; i < toEncrypt.size(); i++) output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))]; return output; } |
There are two differences here:
- key is now defined as a char array.
- We now use the char at index modulos the size of the key array to XOR, rather than the same key for each character to encrypt.
Now running the same string through there, we get the following output:
Encrypted: :=.43*-:8m2$. Decrypted:kylewbanks.com |
It doesn't look that much more secure, but the reason for using multiple keys rather than just one, is that for each additional key you use, you effectively double the amount of time it takes to brute force the encrypted string.
Full source in a variety of languages available on GitHub.
来自:https://kylewbanks.com/blog/Simple-XOR-Encryption-Decryption-in-Cpp
Simple XOR Encryption/Decryption in C++ (And Several Other Languages)的更多相关文章
- java Encryption&Decryption
The encryption class: package cn.com.smartcost.qy.util; import java.security.Key; import java.securi ...
- In ZeroDB, the client is responsible for the database logic. Data encryption, decryption, and compression also happen client side. Therefore, the server never has any knowledge about the data, its str
zerodb/index.rst at master · zerodb/zerodb https://github.com/zerodb/zerodb/blob/master/docs/source/ ...
- Csharp and Vbscript: Encryption/Decryption Functional
1 /// <summary> 2 /// 塗聚文 3 /// 20130621 4 /// 自定义字符串加密解密 5 /// < ...
- delphi 加密 XOR
From http://www.delphigeist.com/2009/09/text-encryption-with-xor.html Text encryption with XOR Ev ...
- Get your Windows product key from a script
The product key is located in the registry under HKLM\Software\Microsoft\Windows NT\CurrentVersion I ...
- linux loop device介绍
在Linux中,有一种特殊的块设备叫loop device,这种loop device设备是通过影射操作系统上的正常的文件而形成的虚拟块设备.因为这种设备的存在,就为我们提供了一种创建一个存在于其他文 ...
- Linux下如何创建loop device
在Linux中,有一种特殊的块设备叫loop device,这种loop device设备是通过映射操作系统上的正常的文件而形成的虚拟块设备 因为这种设备的存在,就为我们提供了一种创建一个存在于其他文 ...
- String decryption with de4dot
Introduction de4dot is a wonderful tool for deobfuscating known and unknown .NET protections. Dealin ...
- C#/PHP Compatible Encryption (AES256) ZZ
Finding a way to encrypt messages in C# and decrypting them in PHP or vice versa seems to be a " ...
随机推荐
- CentOS 7下KVM支持虚拟化/嵌套虚拟化配置
开启虚拟化: cat << EOF > /etc/modprobe.d/kvm-nested.conf options kvm-intel nested=1 options kvm- ...
- IBM BR10i阵列卡配置Raid0/Raid1(转)
说明:IBM的阵列卡无论多旧多新操作步骤都基本差不多. RAID1的步骤: 开机自检过程中出现ctrl+c提示,按ctrl+c进入LSI Logic Config Utility v6.10.02.0 ...
- IBDAP-CMSIS-DAP
IBDAP-CMSIS-DAP Armstart's CMSIS-DAP firmware implementation in gcc and makefile. http://www.armstar ...
- c# SerialPort会出现“已关闭 Safe handle”的错误
c# SerialPort使用时出现“已关闭 Safe handle”的错误我在开发SerialPort程序时出现了一个问题,在一段特殊的扫描代码的时候会出现“已关闭 Safe handle”的错误, ...
- oracle sql 高级
1 时间 如果是从当前时间到前一个月的这个时候之间的记录总条数: select count(1) from uis_md_stcustom u where firsttime betw ...
- 教程:如何手动安装Xamarin与Xamarin for VisualStudio
[2016/4/17更新:如果你下载后发现仍然需要付费才能编译Android/iOS APP,请到文章最下面更新Xamarin for VS和Xamarin Studio到最新的版本.Build201 ...
- PHP 7 来了,PHP 6 去哪儿了?
PHP7来了,那么PHP6去哪儿了呢? PHP7简介 PHP7是PHP编程语言全新的一个版本,主要在性能方面获得了极大的提升.官方的文档显示,PHP7可以达到PHP5.x版本两倍的性能.同时还 对PH ...
- AngularJS路由系列(6)-- UI-Router的嵌套State
本系列探寻AngularJS的路由机制,在WebStorm下开发.本篇主要涉及UI-Route的嵌套State. 假设一个主视图上有两个部分视图,部分视图1和部分视图2,主视图对应着一个state,两 ...
- ASIHttpRequest release 包无法发出请求
ASIHttpRequest 在 Release 模式下,Optimize 后会导致发不出请求. 解决方案: 去掉这两个文件的 Optimization:ASIFormDataRequest.m AS ...
- iOS中block简介-作用域
转:http://www.2cto.com/kf/201401/269467.html 用block可以定义任意的代码片段,将其像对象一样传入另一个方法:它是c级别的语法,和C语言中的函数指针非常相似 ...