public CipherEncoderTests(int keyLength = Crypto.DefaultKeylength, bool encrypt = false) { _stringData = "random-string"; _key = GenerateKey(keyLength); _channelOptions = new ChannelOptions(encrypt, new CipherParams(Crypto.DefaultAlgorithm, _key, Encryption.CipherMode.CBC)); _crypto = Crypto.GetCipher(_channelOptions.CipherParams); _encryptedData = _crypto.Encrypt(_stringData.GetBytes()); _encryptedBinaryData = _crypto.Encrypt(_binaryData); encoder = new CipherEncoder(Defaults.Protocol); }
public void SwitchLetter_UppercaseLetter() { string unencodedLetter = "A"; int offset = 1; string expectedEncodedLetter = "B"; string actualEncodedLetter; CipherEncoder encoder = new CipherEncoder(); actualEncodedLetter = encoder.SwitchLetter(unencodedLetter, offset); Assert.AreEqual(expectedEncodedLetter, actualEncodedLetter, "Switched letters are not equal"); }
public void SwitchLetter_NonLetterSymbol() { string unencodedSymbol = "%"; int offset = 1; string expectedEncodedSymbol = "%"; string actualEncodedSymbol; CipherEncoder encoder = new CipherEncoder(); actualEncodedSymbol = encoder.SwitchLetter(unencodedSymbol, offset); Assert.AreEqual(expectedEncodedSymbol, actualEncodedSymbol, "Switched letters are not equal"); }
public void Encrypt_EmptyString() { string unencodedString = ""; int offset = 2; string expectedEncodedString = ""; string actualEncodedString; CipherEncoder encoder = new CipherEncoder(); actualEncodedString = encoder.Encrypt(unencodedString, offset); Assert.AreEqual(expectedEncodedString, actualEncodedString, "Encoded strings are not equal"); }
public void Encrypt_LargeOffset() { string unencodedString = "abcdefghijklmnopQRSTUVwxyz123456!@#$%^"; int offset = 2602; string expectedEncodedString = "cdefghijklmnopqrSTUVWXyzab123456!@#$%^"; string actualEncodedString; CipherEncoder encoder = new CipherEncoder(); actualEncodedString = encoder.Encrypt(unencodedString, offset); Assert.AreEqual(expectedEncodedString, actualEncodedString, "Encoded strings are not equal"); }
public void WithInvalidKey_Throws() { var options = new ChannelOptions(new CipherParams(Crypto.DefaultAlgorithm, new byte[] { 1, 2, 3 })); var encoder = new CipherEncoder(); var error = Assert.Throws <AblyException>(delegate { encoder.Encode(new Message { Data = "string" }, options.ToDecodingContext()); }); error.InnerException.Should().BeOfType <CryptographicException>(); }
public void WithInvalidKeyLength_Throws() { var options = new ChannelOptions(new CipherParams(Crypto.DefaultAlgorithm, new byte[] { }, keyLength: 10)); var encoder = new CipherEncoder(Protocol.MsgPack); var error = Assert.Throws <AblyException>(delegate { encoder.Encode(new Message() { Data = "string" }, options); }); error.InnerException.Should().BeOfType <CryptographicException>(); }
public void WithInvalidAlgorithm_Throws() { var keyGen = new Rfc2898DeriveBytes("password", 8); var key = keyGen.GetBytes(Crypto.DefaultKeylength / 8); var options = new ChannelOptions(new CipherParams("mgg", key)); var encoder = new CipherEncoder(); var error = Assert.Throws <AblyException>(() => { encoder.Encode(new Message { Data = "string" }, options.ToDecodingContext()); }); error.Message.Should().Contain("Currently only the AES encryption algorithm is supported"); }