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:

  1. key is now defined as a char array.
  2. 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)的更多相关文章

  1. java Encryption&Decryption

    The encryption class: package cn.com.smartcost.qy.util; import java.security.Key; import java.securi ...

  2. 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/ ...

  3. Csharp and Vbscript: Encryption/Decryption Functional

      1 /// <summary>   2     /// 塗聚文   3     /// 20130621   4     /// 自定义字符串加密解密   5     /// < ...

  4. delphi 加密 XOR

    From  http://www.delphigeist.com/2009/09/text-encryption-with-xor.html Text encryption with XOR   Ev ...

  5. Get your Windows product key from a script

    The product key is located in the registry under HKLM\Software\Microsoft\Windows NT\CurrentVersion I ...

  6. linux loop device介绍

    在Linux中,有一种特殊的块设备叫loop device,这种loop device设备是通过影射操作系统上的正常的文件而形成的虚拟块设备.因为这种设备的存在,就为我们提供了一种创建一个存在于其他文 ...

  7. Linux下如何创建loop device

    在Linux中,有一种特殊的块设备叫loop device,这种loop device设备是通过映射操作系统上的正常的文件而形成的虚拟块设备 因为这种设备的存在,就为我们提供了一种创建一个存在于其他文 ...

  8. String decryption with de4dot

    Introduction de4dot is a wonderful tool for deobfuscating known and unknown .NET protections. Dealin ...

  9. 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 " ...

随机推荐

  1. .net中实现RSS方法

    引用 如何在.net动态网站中实现RSS呢?主要思想是编写一个能够自动按照RSS格式生成xml文档的通用类.具体步骤如下: 步骤一:创建RSS通用类 C#代码 using System;   usin ...

  2. 反接保护电路 Reverse Voltage Protection

    Reverse Voltage Protection I've long wanted to pull together some reverse polarity protection ideas ...

  3. WinForm通用自动更新器AutoUpdater项目实战

    一.项目背景介绍 最近单位开发一个项目,其中需要用到自动升级功能.因为自动升级是一个比较常用的功能,可能会在很多程序中用到,于是,我就想写一个自动升级的组件,在应用程序中,只需要引用这个自动升级组件, ...

  4. 利用/proc/pid/pagemap将虚拟地址转换为物理地址

    内核文档: Documentation/vm/pagemap.txt pagemap is a new (as of 2.6.25) set of interfaces in the kernel t ...

  5. Windows Phone本地数据库(SQLCE):6、[Index] attribute(翻译)(转)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第六篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...

  6. UINavigationController 、UINavigationBar 、UINavigationItem 超清晰直观详解

    UINavigationController 部分 1. UINavigationController 是一个容器类.里面盛放的是UIViewController. 容器的意思是,如果你不放入UIVi ...

  7. React中的的JSX

    什么是JSX? JSX是JavaScript XML的缩写,其本质是js,表现形式类似于XML,与js区别在于可直接在里面编写html标签. 怎么使用JSX? 语法规则: JSX 的基本语法规则:HT ...

  8. 灵书妙探第八季/全集Castle迅雷下载

    英文全名Castle,第8季(2015)ABC.本季看点:<灵书妙探>讲述性格和背景迥异的两人在不断的斗嘴与摩擦中竟然渐渐培养出了默契,成了名符其实的最佳搭档.在上季Richard Cas ...

  9. JS读取json 文件

    json文件是一种轻量级的数据交互格式.一般在jquery中使用getJSON()方法读取. $.getJSON(url,[data],[callback]) url:加载的页面地址 data: 可选 ...

  10. Orchard模块开发全接触3:分类的实现及内容呈现(Display)

    一:分类用现有技术怎么实现? 实际就是创建 Query 和 Projection,如果不知道怎么做,参考:Orchard之在前台显式一个属于自己的列表(在这篇里,还进行了稍稍拓展),当然,基础的知道, ...