Skip to content

Commit

Permalink
simplified ICryptoAlgorithm interface
Browse files Browse the repository at this point in the history
  • Loading branch information
budgetpreneur committed Sep 14, 2023
1 parent 5924c84 commit 89df7f1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public EncryptedData Encrypt(string data)
{
return new EncryptedData
{
Metadata = null,
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Input is null", new Exception("Input is null")), StatusCode = 400 }
};
Expand All @@ -76,15 +75,13 @@ public EncryptedData Encrypt(string data)
{
return new EncryptedData
{
Metadata = Convert.ToBase64String(ivBytes),
Data = Convert.ToBase64String(encryptedBytes.Data)
};
}
else
{
return new EncryptedData
{
Metadata = null,
Data = null,
Status = encryptedBytes.Status
};
Expand All @@ -94,7 +91,6 @@ public EncryptedData Encrypt(string data)
{
return new EncryptedData
{
Metadata = null,
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Encryption error", ex), StatusCode = 400 }
};
Expand All @@ -106,7 +102,6 @@ public EncryptedBytes Encrypt(byte[] data)
{
return new EncryptedBytes
{
Metadata = null,
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Input is null", new Exception("Input is null")), StatusCode = 400 }
};
Expand All @@ -128,7 +123,6 @@ public EncryptedBytes Encrypt(byte[] data)
{
return new EncryptedBytes
{
Metadata = null,
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Encryption error", ex), StatusCode = 400 }
};
Expand Down Expand Up @@ -167,7 +161,6 @@ private EncryptedBytes InternalEncrypt(byte[] dataBytes, byte[] ivBytes, byte[]

return new EncryptedBytes
{
Metadata = _useRandomIV ? ivBytes : null,
Data = buffer,
Status = null
};
Expand All @@ -183,9 +176,9 @@ private EncryptedBytes InternalEncrypt(byte[] dataBytes, byte[] ivBytes, byte[]
};
}
}
public DecryptedData Decrypt(DataEnvelope encryptedData)
public DecryptedData Decrypt(string encryptedData)
{
if (encryptedData == null || encryptedData.Data == null)
if (encryptedData == null)
{
return new DecryptedData
{
Expand All @@ -194,20 +187,11 @@ public DecryptedData Decrypt(DataEnvelope encryptedData)
};
}

if (!(encryptedData is EncryptedData encData))
{
return new DecryptedData
{
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Input is not EncryptedData", new Exception("Input is not EncryptedData")), StatusCode = 400 }
};
}

byte[] dataBytes;
byte[] ivBytes;
try
{
dataBytes = Convert.FromBase64String(encData.Data);
dataBytes = Convert.FromBase64String(encryptedData);
ivBytes = dataBytes.Take(16).ToArray();
dataBytes = dataBytes.Skip(16).ToArray();
}
Expand All @@ -219,9 +203,9 @@ public DecryptedData Decrypt(DataEnvelope encryptedData)
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Base64 conversion error", ex), StatusCode = 400 }
};
}
byte[] keyBytes = Util.GetEncryptionKeyBytes(_cipherKey);
try
{
byte[] keyBytes = Util.GetEncryptionKeyBytes(_cipherKey);
DecryptedBytes decryptedBytes = InternalDecrypt(dataBytes, ivBytes, keyBytes);
if (decryptedBytes.Data != null)
{
Expand Down Expand Up @@ -250,9 +234,9 @@ public DecryptedData Decrypt(DataEnvelope encryptedData)
};
}
}
public DecryptedBytes Decrypt(BytesEnvelope encryptedBytes)
public DecryptedBytes Decrypt(byte[] encryptedBytes)
{
if (encryptedBytes == null || encryptedBytes.Data == null)
if (encryptedBytes == null)
{
return new DecryptedBytes
{
Expand All @@ -261,20 +245,11 @@ public DecryptedBytes Decrypt(BytesEnvelope encryptedBytes)
};
}

if (!(encryptedBytes is EncryptedBytes))
{
return new DecryptedBytes
{
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Input is not EncryptedData", new Exception("Input is not EncryptedData")), StatusCode = 400 }
};
}

byte[] ivBytes = encryptedBytes.Data.Take(16).ToArray();
byte[] dataBytes = encryptedBytes.Data.Skip(16).ToArray();
byte[] keyBytes = Util.GetEncryptionKeyBytes(_cipherKey);
try
{
byte[] ivBytes = encryptedBytes.Take(16).ToArray();
byte[] dataBytes = encryptedBytes.Skip(16).ToArray();
byte[] keyBytes = Util.GetEncryptionKeyBytes(_cipherKey);
return InternalDecrypt(dataBytes, ivBytes, keyBytes);
}
catch(Exception ex)
Expand Down
27 changes: 10 additions & 17 deletions src/Api/PubnubApi/Security/Crypto/Cryptors/Cryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public EncryptedBytes Encrypt(byte[] data)
return _algorithm.Encrypt(data);
}

public DecryptedData Decrypt(DataEnvelope data)
public DecryptedData Decrypt(string data)
{
if (data == null || data.Data == null)
if (data == null)
{
return new DecryptedData
{
Expand All @@ -44,7 +44,7 @@ public DecryptedData Decrypt(DataEnvelope data)
}
else
{
CryptorHeader header = CryptorHeader.FromBytes(Convert.FromBase64String(data.Data));
CryptorHeader header = CryptorHeader.FromBytes(Convert.FromBase64String(data));
if (header == null || !header.Identifier.SequenceEqual(_algorithm.Identifier))
{
return new DecryptedData
Expand All @@ -53,16 +53,13 @@ public DecryptedData Decrypt(DataEnvelope data)
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("CryptorHeader mismatch", new Exception("CryptorHeader mismatch")), StatusCode = 400 }
};
}
DataEnvelope internalDataEnvelope = new EncryptedData
{
Data = Convert.ToBase64String(Convert.FromBase64String(data.Data).Skip(5 + header.Identifier.Length + ((header.DataSize < 255) ? 1 : 3)).ToArray())
};
return _algorithm.Decrypt(internalDataEnvelope);
string actualData = Convert.ToBase64String(Convert.FromBase64String(data).Skip(5 + header.Identifier.Length + ((header.DataSize < 255) ? 1 : 3)).ToArray());
return _algorithm.Decrypt(actualData);
}
}
public DecryptedBytes Decrypt(BytesEnvelope data)
public DecryptedBytes Decrypt(byte[] data)
{
if (data == null || data.Data == null)
if (data == null)
{
return new DecryptedBytes
{
Expand All @@ -76,7 +73,7 @@ public DecryptedBytes Decrypt(BytesEnvelope data)
}
else
{
CryptorHeader header = CryptorHeader.FromBytes(data.Data);
CryptorHeader header = CryptorHeader.FromBytes(data);
if (header == null || !header.Identifier.SequenceEqual(_algorithm.Identifier))
{
return new DecryptedBytes
Expand All @@ -85,12 +82,8 @@ public DecryptedBytes Decrypt(BytesEnvelope data)
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("CryptorHeader mismatch", new Exception("CryptorHeader mismatch")), StatusCode = 400 }
};
}
BytesEnvelope internalBytesEnvelope = new EncryptedBytes
{
Data = data.Data.Skip(5 + header.Identifier.Length + ((header.DataSize < 255) ? 1 : 3)).ToArray()
};

return _algorithm.Decrypt(internalBytesEnvelope);
byte[] actualBytes = data.Skip(5 + header.Identifier.Length + ((header.DataSize < 255) ? 1 : 3)).ToArray();
return _algorithm.Decrypt(actualBytes);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public EncryptedData Encrypt(string data)
{
return new EncryptedData
{
Metadata = null,
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Input is null", new Exception("Input is null")), StatusCode = 400 }
};
Expand All @@ -84,15 +83,13 @@ public EncryptedData Encrypt(string data)
{
return new EncryptedData
{
Metadata = null,
Data = Convert.ToBase64String(encryptedBytes.Data)
};
}
else
{
return new EncryptedData
{
Metadata = null,
Data = null,
Status = encryptedBytes.Status
};
Expand All @@ -102,7 +99,6 @@ public EncryptedData Encrypt(string data)
{
return new EncryptedData
{
Metadata = null,
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Encryption error", ex), StatusCode = 400 }
};
Expand All @@ -114,7 +110,6 @@ public EncryptedBytes Encrypt(byte[] data)
{
return new EncryptedBytes
{
Metadata = null,
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Input is null", new Exception("Input is null")), StatusCode = 400 }
};
Expand All @@ -136,7 +131,6 @@ public EncryptedBytes Encrypt(byte[] data)
{
return new EncryptedBytes
{
Metadata = null,
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Encryption error", ex), StatusCode = 400 }
};
Expand Down Expand Up @@ -194,7 +188,7 @@ private EncryptedBytes InternalEncrypt(byte[] dataBytes, byte[] ivBytes, string
};
}
}
public DecryptedData Decrypt(DataEnvelope encryptedData)
public DecryptedData Decrypt(string encryptedData)
{
if (encryptedData == null)
{
Expand All @@ -205,19 +199,10 @@ public DecryptedData Decrypt(DataEnvelope encryptedData)
};
}

if (!(encryptedData is EncryptedData encData))
{
return new DecryptedData
{
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Input is not EncryptedData", new Exception("Input is not EncryptedData")), StatusCode = 400 }
};
}

byte[] dataBytes;
try
{
dataBytes = Convert.FromBase64String(encData.Data);
dataBytes = Convert.FromBase64String(encryptedData);
}
catch(Exception ex)
{
Expand Down Expand Up @@ -263,9 +248,9 @@ public DecryptedData Decrypt(DataEnvelope encryptedData)
};
}
}
public DecryptedBytes Decrypt(BytesEnvelope encryptedBytes)
public DecryptedBytes Decrypt(byte[] encryptedBytes)
{
if (encryptedBytes == null || encryptedBytes.Data == null)
if (encryptedBytes == null)
{
return new DecryptedBytes
{
Expand All @@ -274,20 +259,11 @@ public DecryptedBytes Decrypt(BytesEnvelope encryptedBytes)
};
}

if (!(encryptedBytes is EncryptedBytes))
{
return new DecryptedBytes
{
Data = null,
Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Input is not EncryptedData", new Exception("Input is not EncryptedData")), StatusCode = 400 }
};
}

byte[] ivBytes = _useRandomIV ? encryptedBytes.Data.Take(16).ToArray() : Encoding.UTF8.GetBytes("0123456789012345");
byte[] dataBytes = _useRandomIV ? encryptedBytes.Data.Skip(16).ToArray() : encryptedBytes.Data;
string keyString = Util.GetLegacyEncryptionKey(_cipherKey);
try
{
byte[] ivBytes = _useRandomIV ? encryptedBytes.Take(16).ToArray() : Encoding.UTF8.GetBytes("0123456789012345");
byte[] dataBytes = _useRandomIV ? encryptedBytes.Skip(16).ToArray() : encryptedBytes;
string keyString = Util.GetLegacyEncryptionKey(_cipherKey);
return InternalDecrypt(dataBytes, ivBytes, keyString);
}
catch(Exception ex)
Expand Down
39 changes: 10 additions & 29 deletions src/Api/PubnubApi/Security/Crypto/ICryptoAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,25 @@

namespace PubnubApi.Security.Crypto
{
public abstract class DataEnvelopeBase
{
public abstract string Data { get; set; }
}
public abstract class BytesEnvelopeBase
{
public abstract byte[] Data { get; set; }
}
public abstract class DataEnvelope : DataEnvelopeBase
{
public abstract string Metadata { get; set; }
}
public abstract class BytesEnvelope : BytesEnvelopeBase
{
public abstract byte[] Metadata { get; set; }
}

public class EncryptedData : DataEnvelope
public class EncryptedData
{
public PNStatus Status { get; set; } //TODO: Need to identify the format to send error/success status
public override string Data { get; set; }
public override string Metadata { get; set; }
public string Data { get; set; }
}
public class EncryptedBytes : BytesEnvelope
public class EncryptedBytes
{
public PNStatus Status { get; set; } //TODO: Need to identify the format to send error/success status
public override byte[] Data { get; set; }
public override byte[] Metadata { get; set; }
public byte[] Data { get; set; }
}
public class DecryptedData : DataEnvelopeBase
public class DecryptedData
{
public override string Data { get; set; }
public string Data { get; set; }
public PNStatus Status { get; set; } //TODO: Need to identify the format to send error/success status
}

public class DecryptedBytes : BytesEnvelopeBase
public class DecryptedBytes
{
public override byte[] Data { get; set; }
public byte[] Data { get; set; }
public PNStatus Status { get; set; } //TODO: Need to identify the format to send error/success status
}
public interface ICryptoAlgorithm
Expand All @@ -61,7 +42,7 @@ public interface ICryptoAlgorithm
EncryptedData Encrypt(string data);
EncryptedBytes Encrypt(byte[] data);

DecryptedData Decrypt(DataEnvelope encryptedData);
DecryptedBytes Decrypt(BytesEnvelope encryptedBytes);
DecryptedData Decrypt(string encryptedData);
DecryptedBytes Decrypt(byte[] encryptedData);
}
}
Loading

0 comments on commit 89df7f1

Please sign in to comment.