-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Added a Simple XOR Cipher #5490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2eda377
Implemented Simple XOR Cipher
lcsjunior 1f6f98a
fix clang format
lcsjunior 36e1ab0
fix class XORCipher due to build error in pipeline
lcsjunior 2d1f6ed
Merge branch 'TheAlgorithms:master' into master
lcsjunior f2d4ccc
Added improvements and decrypt test
lcsjunior 5f33161
Merge branch 'master' into master
lcsjunior d98740c
Merge branch 'master' into master
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.thealgorithms.ciphers; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
/** | ||
* A simple implementation of XOR cipher that, given a key, allows to encrypt and decrypt a plaintext. | ||
* | ||
* @author <a href="https://github.com/lcsjunior">lcsjunior</a> | ||
* | ||
*/ | ||
public final class XORCipher { | ||
|
||
private static final Charset CS_DEFAULT = StandardCharsets.UTF_8; | ||
|
||
private XORCipher() { | ||
} | ||
|
||
private static byte[] xor(final byte[] inputBytes, final byte[] keyBytes) { | ||
byte[] outputBytes = new byte[inputBytes.length]; | ||
for (int i = 0; i < inputBytes.length; ++i) { | ||
outputBytes[i] = (byte) (inputBytes[i] ^ keyBytes[i % keyBytes.length]); | ||
} | ||
return outputBytes; | ||
} | ||
|
||
public static String encrypt(final String plaintext, final String key) { | ||
byte[] plaintextBytes = plaintext.getBytes(CS_DEFAULT); | ||
byte[] keyBytes = key.getBytes(CS_DEFAULT); | ||
return Base64.getEncoder().encodeToString(xor(plaintextBytes, keyBytes)); | ||
} | ||
|
||
public static String decrypt(final String cipher, final String key) { | ||
byte[] cipherBytes = Base64.getDecoder().decode(cipher); | ||
byte[] keyBytes = key.getBytes(CS_DEFAULT); | ||
return new String(xor(cipherBytes, keyBytes), CS_DEFAULT); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/java/com/thealgorithms/ciphers/XORCipherTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.thealgorithms.ciphers; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class XORCipherTest { | ||
|
||
@Test | ||
void shouldEncryptAndDecryptTest() { | ||
// given | ||
String plaintext = "My t&xt th@t will be ençrypted..."; | ||
String key = "My ç&cret key!"; | ||
// when | ||
String cipherText = XORCipher.encrypt(plaintext, key); | ||
// then | ||
assertEquals(XORCipher.decrypt(cipherText, key), plaintext); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.