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 Session 使用简介

    6.session和数据存储 6.1 session和cookie 6.2 Go如何使用session 6.3 session存储 6.4 预防session劫持 6.5 小结

  2. 使用log4jdbc记录SQL信息

    一.log4jdbc的简单介绍 使用log4jdbc在不改变原有代码的情况下,就可以收集执行的SQL文和JDBC执行情况. 平时开发使用的ibatis,hibernate,spring jdbc的sq ...

  3. Win7 ArcGIS 10.5 安装错误 A service pack is required on this operatiing system

    前段时间有朋友反馈在Win7专业版下安装ArcGIS 10.5, 无法安装,弹出错误: A service pack is required on this operatiing system,如图: ...

  4. Android基础笔记(九)- 广播

    广播的概念 广播的生命周期 案例-监听短信到来并解析短信内容 案例-拦截外拨电话并设置区号 案例-SD卡状态监听 介绍一些经常使用的广播 发送自己定义广播 有序广播和无序广播 启程!! ! 广播的概念 ...

  5. Occlusion Culling遮挡剔除理解设置和地形优化应用

    这里使用的是unity5.5版本 具体解释网上都有,就不多说了,这里主要说明怎么使用,以及参数设置和实际注意点 在大场景地形的优化上,但也不是随便烘焙就能降低帧率的,必须结合实际情况来考虑,当然还有透 ...

  6. Swift - 绘制背景线条

    Swift - 绘制背景线条 效果 源码 // // BackgroundLineView.swift // LineBackgroundView // // Created by YouXianMi ...

  7. SpringBoot下的Job定时任务

    编写Job定时执行任务十分有用,能解决很多问题,这次实习的项目里做了一下系统定时更新三方系统订单状态的功能,这里用到了Spring的定时任务使用的非常方便,下面总结一下如何使用: 一,@schedul ...

  8. Mybatis ResultMap Collection 复合主键

    <resultMap type="XX" id="XXMap">          <id property="id" c ...

  9. 得到ImageView中drawable显示的区域的计算方法

    我们都知道Imageview中有不同的拉伸比率,比如fitStart,centCrop这样的,所以imageview中的drawable不一定和imageview占有相同的位置和大小,那么怎么计算呢? ...

  10. android R.layout 中找不到已存在的布局文件

    在R.layout.test文件时,总是找不到您想要的文件,可是它明明就在layout文件下面,而且在R.Java中也已经生成了,那么找不到的原因就是你导入了Android.R的包,这样你永远找不到你 ...