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. Go 导入当前项目下的包

    其实和其他语言很类似 import (     "../controllers" //这里就是导入上一级目录中的controllers     "./models&quo ...

  2. Javascript:原型模式类继承

    原型模式 每个函数(准确说不是类.对象)都有一个prototype属性,这个属性是一个指针,指向一个对象. 使用原型对象的好处是可以让所有对象实例共享它包含的属性和方法.   1.原型对象 (1)当创 ...

  3. ASIHTTPRequest学习笔记

    1.creating requestsrequest分为同步和异步两种.不同之处在于开始request的函数:[request startSynchronous];[request startAsyn ...

  4. WordPress主题开发:开启导航菜单功能

    步骤一:开启导航菜单功能 <?php /* register_nav_menu( $location, $description ) 函数功能:开启导航菜单功能 @参数 string $loca ...

  5. Linux学习1-Xshell连接阿里云ECS服务器

    前言 前段时间双十一活动,头脑发热买了个阿里云的服务器,买了后也不知道怎么用一直放着在.最近想起来,于是看了下阿里云ECS的相关资料. 由于平常一直用xshell比较多,不太习惯阿里云的ESC终端连接 ...

  6. 关于面试总结5-python笔试题(递归)

    前言 本篇继续收集一些常见的python笔试题,以基础知识为主,递归是面试最喜欢考的一个问题,不管是做开发还是测试,都无法避免考递归.本篇结合实际案例,讲下几种关于递归的场景. 计算n的阶乘 计算n! ...

  7. scriptcs简介

    一.scriptcs简介 scriptcs易于编写和执行C #用一个简单的文本编辑器. 在Visual Studio中,和其他的思想,是功能强大的工具,他们有时会阻碍生产力比他们更促进它. 您并不总是 ...

  8. Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)

    在Java中,实现定时任务有多种方式,本文介绍4种,Timer和TimerTask.Spring.QuartZ.Linux Cron. 以上4种实现定时任务的方式,Timer是最简单的,不需要任何框架 ...

  9. LaTeX技巧207:使用align环境输入多行公式的技巧

    align是输入多行公式中最好用的环境,仅仅是个人浅见,因为他的对齐非常灵活,如果大家需要非常灵巧的对齐方式的多行公式,建议使用align环境,对应的也还有align*和aligned等等类似的环境, ...

  10. 【.Net】 C#访问修饰符

    一 类的修饰符:  C#中类的默认修饰符是internal.1 private 只有对包.NET中的应用程序或库才能访问.2 public 不限制对类的访问. 3 protected 只可以被本类和其 ...