Giter Club home page Giter Club logo

kylebanks / xorencryption Goto Github PK

View Code? Open in Web Editor NEW
349.0 17.0 84.0 34 KB

XOR encryption implementations for several languages.

Home Page: http://kylewbanks.com/blog/Simple-XOR-Encryption-Decryption-in-Cpp

License: MIT License

C# 5.49% C++ 6.20% C 5.17% Dart 4.75% Groovy 4.26% Java 5.74% JavaScript 4.35% Objective-C 9.21% Python 5.01% F# 3.81% Go 8.98% Ruby 3.42% Kotlin 5.83% Swift 7.82% PHP 4.33% Visual Basic .NET 11.70% CoffeeScript 3.93%
xor xor-encryption encryption multilanguage

xorencryption's Introduction

XOR Encryption

Simple implementation of XOR Encryption/Decrypting in various languages, including:

This implementation goes beyond the basic single-key model to use multiple keys in a particular sequence, making it that much more difficult to brute-force.

In these examples, I'm encrypting the same string with the same keys in order to keep consistency with the output, and to demonstrate that an encrypted string from a C program can be decrypted in a Java application, or any combination, so long as the keys remain the same.

For an in-depth explanation of the code, check out KyleWBanks.com.

xorencryption's People

Contributors

albinodrought avatar ankitapurv avatar bcse avatar benphelps avatar chaikew avatar dautermann avatar denissimon avatar kylebanks avatar nexus166 avatar pizycki avatar woodongwong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xorencryption's Issues

Go version is not Unicode safe

Hi Kyle,

Came across your blog post when searching for "golang xor encryption" and was testing it. Unfortunately, your function isn't Unicode safe. I've created a few test cases in https://play.golang.org/p/UdCubbEZECx to show the issue.

Might be worth putting a warning into the code/blog post, as string handling in Go has its gotchas unfortunately.

Thanks,
tsak

there is a bug with the c version

I had encountered a bug with the c version code when I had tried to decrypt the encrypted string.

void encryptDecrypt(char *input, char *output) {
	char key[] = {'K', 'C', 'Q'}; //Can be any chars, and any size array
	
	int i;
	for(i = 0; i < strlen(input); i++) {
		output[i] = input[i] ^ key[i % (sizeof(key)/sizeof(char))];
	}
}

There is a bug when do the encryption and decryption in one function above.
As the strlen(input) may be smaller than the original string length.So is the below:

void encryptDecrypt(char *input, char *output, int size) {
	char key[] = {'K', 'C', 'Q'}; //Can be any chars, and any size array
	
	int i;
	for(i = 0; i < size; i++) {
		output[i] = input[i] ^ key[i % (sizeof(key)/sizeof(char))];
	}
}

Lua version

Here's a Lua version

function M.obfuscate(input, key)
	key = key or M.obfuscation_key
	local output = ""
	local key_iterator = 1
	
	local input_length = #input
	local key_length = #key
	
	for i=1, input_length do
		local character = string.byte(input:sub(i,i))
		if key_iterator >= key_length then key_iterator = 1 end -- cycle
		local key_byte = string.byte(key:sub(key_iterator,key_iterator))
		output = output .. string.char(bit.bxor( character , key_byte))

		key_iterator = key_iterator + 1
		
	end
	return output
end

CPP to PHP non-compatible

I am trying to encrypt something on Cpp and decrypt it in PHP but they won't work. Any tips, they are the same key.

Problems with C version.

On the C version I made some modifications for my own needs, however now it seems to not work properly on windows. (the encrypted string does not decrypt to be the same as the original and I test printing them all out as well, I started with making the key be passed to the function itself, and thinking if it is because output has to be larger than the input string or something.

void encryptDecrypt(char *input, char *output, char *key) {
	size_t i;
	for (i = 0; i < strlen(input); i++)
		output[i] = input[i] ^ key[i % sizeof(key)];
}

On this I am taking a key of size 134 characters, and a input string of 114 characters and I expect and output (encrypted string of about the same size of larger, however I am not sure the actual size and in some places it returns multiple characters with the int value of -52. It does that value however on the decryption though after 52 characters into the string decryption. I am officially left wondering why this is.

https://cdn.discordapp.com/attachments/184740186436009984/367877048011718656/ConsoleApplication1.c

This code above demonstrates the valid problems I got with all of this.

Also why are you dividing by 1 because sizeof(char) is always 1 no matter what the platform is.

fixed

std::string EncryptDecrypt(std::string toEncrypt) { char key[4] = { 'j', 'T', 'J','d' }; std::string output = toEncrypt; for (int i = 0; i < toEncrypt.size(); i++) { output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))]; if (output[i] == 0) { output[i] = toEncrypt[i]; } } output[toEncrypt.size()] = 0; return output; }

GoLang XOR function works heavily

Source code: https://github.com/KyleBanks/XOREncryption/blob/master/Go/xor.go

The string concat operator output += will make this XOR function run heavily. I tested input with ~500k characters and it took about 22s to complete. Here is my suggestion:

func Xor(input, key string) (output string) {
	var tmp []string

	for i := 0; i < len(input); i++ {
		tmp = append(tmp, string(input[i]^key[i%len(key)]))
	}
	output = strings.Join(tmp, "")
	return output
}

By doing this, input with ~500k characters took ~0.1s to complete. Much better.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.