patterncsharpMinor
Custom byte cipher
Viewed 0 times
custombytecipher
Problem
I posted a custom byte cipher on this site a few months back, and after spending three days recoding it from scratch, wanted to post it again to see if you can offer any improvements. (I am not looking for: "You should use var instead of explicit declaration". I'm looking for significant code improvements, not coding style improvements.)
The base concept of my cipher works a little like a Caesar cipher, except it also includes a few "twists".
```
using System.Text;
namespace DreamforceFramework.Framework.Cryptography
{
///
/// DreamforceByteCipher
/// Gordon Kyle Wallace, "Krythic"
/// Copyright (C) 2015 Gordon Kyle Wallace, "Krythic" - All Rights Reserved
///
public static class DreamforceByteCipher
{
///
/// Encrypts a string into a byte array.
///
///
///
///
public static byte[] Encrypt(string data, string password)
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
string passwordHash = DreamforceHashing.GenerateSHA256(password);
byte[] hashedPasswordBytes = Encoding.ASCII.GetBytes(passwordHash);
int passwordShiftIndex = 0;
bool shiftFlag = false;
for (int i = 0; i
/// Decrypts a byte array back into a string.
///
///
///
///
public static string Decrypt(byte[] data, string password)
{
string passwordHash = DreamforceHashing.GenerateSHA256(password);
byte[] hashedPasswordBytes = Encoding.UTF8.GetBytes(passwordHash);
int passwordShiftIndex = 0;
bool shiftFlag = false;
for (int i = 0; i < data.Length; i++)
{
int shift = hashedPasswordBytes[passwordShiftIndex];
data[i] = shift <= 128
? (byte)(data[i] - (shiftFlag
? (byte)(((shift << 2)) % 255)
: (byte)(((shift << 4)) %
The base concept of my cipher works a little like a Caesar cipher, except it also includes a few "twists".
```
using System.Text;
namespace DreamforceFramework.Framework.Cryptography
{
///
/// DreamforceByteCipher
/// Gordon Kyle Wallace, "Krythic"
/// Copyright (C) 2015 Gordon Kyle Wallace, "Krythic" - All Rights Reserved
///
public static class DreamforceByteCipher
{
///
/// Encrypts a string into a byte array.
///
///
///
///
public static byte[] Encrypt(string data, string password)
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
string passwordHash = DreamforceHashing.GenerateSHA256(password);
byte[] hashedPasswordBytes = Encoding.ASCII.GetBytes(passwordHash);
int passwordShiftIndex = 0;
bool shiftFlag = false;
for (int i = 0; i
/// Decrypts a byte array back into a string.
///
///
///
///
public static string Decrypt(byte[] data, string password)
{
string passwordHash = DreamforceHashing.GenerateSHA256(password);
byte[] hashedPasswordBytes = Encoding.UTF8.GetBytes(passwordHash);
int passwordShiftIndex = 0;
bool shiftFlag = false;
for (int i = 0; i < data.Length; i++)
{
int shift = hashedPasswordBytes[passwordShiftIndex];
data[i] = shift <= 128
? (byte)(data[i] - (shiftFlag
? (byte)(((shift << 2)) % 255)
: (byte)(((shift << 4)) %
Solution
The bodies of methods are strikingly similar. In fact, you can make the similarity even more striking by reverting the
Now the difference is literally in one operator. I don't know idiomatic way to pass the operator into a method as a parameter (honestly I don't know
That done, you may factor out the non-DRY part into a helper method:
and call it from both
shift <= 128 condition (and swapping branches, of course) in Decrypt:data[i] = shift > 128
? (byte)(data[i] + (shiftFlag
? (byte)(((shift << 2)) % 255)
: (byte)(((shift << 4)) % 255)))
: (byte)(data[i] - (shiftFlag
? (byte)(((shift << 4)) % 255)
: (byte)(((shift << 2)) % 255)));Now the difference is literally in one operator. I don't know idiomatic way to pass the operator into a method as a parameter (honestly I don't know
C# whatsoever) - but I have no doubt there is. Lambda perhaps?That done, you may factor out the non-DRY part into a helper method:
byte[] transform(byte[] data, string password, operator compare)
{
string passwordHash = DreamforceHashing.GenerateSHA256(password);
byte[] hashedPasswordBytes = Encoding.ASCII.GetBytes(passwordHash);
int passwordShiftIndex = 0;
bool shiftFlag = false;
for (int i = 0; i < bytes.Length; i++)
{
int shift = hashedPasswordBytes[passwordShiftIndex];
data[i] = compare(shift)
? (byte)(data[i] + (shiftFlag
? (byte)(((shift << 2)) % 255)
: (byte)(((shift << 4)) % 255)))
: (byte)(data[i] - (shiftFlag
? (byte)(((shift << 4)) % 255)
: (byte)(((shift << 2)) % 255)));
passwordShiftIndex = (passwordShiftIndex + 1) % 64;
shiftFlag = !shiftFlag;
}
return data;
}and call it from both
Encrypt and Decrypt.Code Snippets
data[i] = shift > 128
? (byte)(data[i] + (shiftFlag
? (byte)(((shift << 2)) % 255)
: (byte)(((shift << 4)) % 255)))
: (byte)(data[i] - (shiftFlag
? (byte)(((shift << 4)) % 255)
: (byte)(((shift << 2)) % 255)));byte[] transform(byte[] data, string password, operator compare)
{
string passwordHash = DreamforceHashing.GenerateSHA256(password);
byte[] hashedPasswordBytes = Encoding.ASCII.GetBytes(passwordHash);
int passwordShiftIndex = 0;
bool shiftFlag = false;
for (int i = 0; i < bytes.Length; i++)
{
int shift = hashedPasswordBytes[passwordShiftIndex];
data[i] = compare(shift)
? (byte)(data[i] + (shiftFlag
? (byte)(((shift << 2)) % 255)
: (byte)(((shift << 4)) % 255)))
: (byte)(data[i] - (shiftFlag
? (byte)(((shift << 4)) % 255)
: (byte)(((shift << 2)) % 255)));
passwordShiftIndex = (passwordShiftIndex + 1) % 64;
shiftFlag = !shiftFlag;
}
return data;
}Context
StackExchange Code Review Q#105649, answer score: 3
Revisions (0)
No revisions yet.