static bool TryReadEncryptionMode(ref Utf8JsonReader reader, out EncryptionModes mode) { if (!reader.TryReadToken(JsonTokenType.String)) { mode = default; return(false); } if (reader.ValueTextEquals(XSalsa20Poly1305Mode)) { mode = EncryptionModes.XSalsa20_Poly1305; return(true); } if (reader.ValueTextEquals(XSalsa20Poly1305SuffixMode)) { mode = EncryptionModes.XSalsa20_Poly1305_Suffix; return(true); } if (reader.ValueTextEquals(XSalsa20Poly1305LiteMode)) { mode = EncryptionModes.XSalsa20_Poly1305_Lite; return(true); } mode = default; return(false); }
private static string _Encrypt(this string plainText, EncryptionModes encryptionModes) { string salt; Guid guid; switch (encryptionModes) { case EncryptionModes.ConstantSaltNoneUserSessionNone: salt = System.Web.Security.Membership.GeneratePassword(EncryptionHelper.SaltLength, 0); guid = Guid.Empty; break; case EncryptionModes.ConstantSaltNoneUserSession: salt = System.Web.Security.Membership.GeneratePassword(EncryptionHelper.SaltLength, 0); guid = EncryptionHelper.CurrentUserSessionGuid; break; case EncryptionModes.ConstantSaltUserSessionNone: salt = ConstantSalt; guid = Guid.Empty; break; case EncryptionModes.ConstantSaltUserSession: salt = ConstantSalt; guid = EncryptionHelper.CurrentUserSessionGuid; break; default: throw new ArgumentOutOfRangeException("", encryptionModes, null); } plainText = salt + guid.ToString("D") + plainText; return(HttpServerUtility.UrlTokenEncode(plainText._Encrypt())); // return Convert.ToBase64String(plainText._Encrypt()); }
public static void WriteSelectProtocol(Utf8JsonWriter writer, IPEndPoint clientEndPoint, EncryptionModes bestMode) { writer.WriteStartObject(); writer.WriteNumber(OpcodePropertyName, (int)VoiceGatewayOpcode.SelectProtocol); writer.WriteStartObject(PayloadPropertyName); writer.WriteString(ProtocolPropertyName, UdpValue); writer.WriteStartObject(DataPropertyName); Span <char> tempBuffer = stackalloc char[MaximumIPv4Length]; _ = clientEndPoint.Address.TryFormat(tempBuffer, out int length); writer.WriteString(AddressPropertyName, tempBuffer.Slice(0, length)); writer.WriteNumber(PortPropertyName, clientEndPoint.Port); writer.WriteString(ModePropertyName, bestMode switch { EncryptionModes.XSalsa20_Poly1305_Lite => XSalsa20Poly1305LiteMode, _ => throw new InvalidOperationException("Unknown mode") });
public void EncryptinDataGeneationTest([Values] EncryptionModes encryptionMode) { var d = EncryptionDataGenerator.Generate((byte)encryptionMode); var ed = new EncryptionData(Protocol.GpBinaryV162, d); Assert.That(ed.IsValid); Assert.That((EncryptionModes)ed.EncryptionMode, Is.EqualTo(encryptionMode)); switch (encryptionMode) { case EncryptionModes.PayloadEncryption: Assert.That(ed.EncryptionSecret, Is.Not.Null); Assert.That(ed.EncryptionSecret.Length, Is.EqualTo(32)); Assert.That(ed.AuthSecret, Is.Null); break; case EncryptionModes.PayloadEncryptionWithIV: Assert.That(ed.EncryptionSecret, Is.Not.Null); Assert.That(ed.EncryptionSecret.Length, Is.EqualTo(32)); Assert.That(ed.AuthSecret, Is.Null); break; case EncryptionModes.PayloadEncryptionWithIVHMAC: Assert.That(ed.EncryptionSecret, Is.Not.Null); Assert.That(ed.EncryptionSecret.Length, Is.EqualTo(32)); Assert.That(ed.AuthSecret, Is.Not.Null); Assert.That(ed.AuthSecret.Length, Is.EqualTo(32)); break; case EncryptionModes.DatagramEncyption: Assert.That(ed.EncryptionSecret, Is.Not.Null); Assert.That(ed.EncryptionSecret.Length, Is.EqualTo(32)); Assert.That(ed.AuthSecret, Is.Not.Null); Assert.That(ed.AuthSecret.Length, Is.EqualTo(32)); break; case EncryptionModes.DatagramEncyptionWithRandomInitialNumbers: Assert.That(ed.EncryptionSecret, Is.Not.Null); Assert.That(ed.EncryptionSecret.Length, Is.EqualTo(32)); Assert.That(ed.AuthSecret, Is.Not.Null); Assert.That(ed.AuthSecret.Length, Is.EqualTo(32)); break; case EncryptionModes.DatagramEncyptionGCMWithRandomInitialNumbers: Assert.That(ed.EncryptionSecret, Is.Not.Null); Assert.That(ed.EncryptionSecret.Length, Is.EqualTo(32)); Assert.That(ed.AuthSecret, Is.Null); break; default: throw new ArgumentOutOfRangeException(nameof(encryptionMode), encryptionMode, null); } }
public static string Encrypt(this string plainText, EncryptionModes encryptionModes = EncryptionModes.ConstantSaltNoneUserSession) { return(plainText._Encrypt(encryptionModes)); }