public byte[] Decrypt(byte[] message, CryptoPadding padding = null, bool checkSign = false) { // Reinterpret encrypted message as a number BigInteger cryptomessage = new BigInteger(message); // Reverse encryption message = Support.ModExp(cryptomessage, checkSign ? e : d, n).ToByteArray(); // Remove fixed padding byte[] b1 = new byte[message.Length - 1]; Array.Copy(message, b1, message.Length - 1); message = b1; // Remove dynamic padding message = (padding ?? NO_PADDING).Unpad(message); return(message); }
public byte[] Decrypt(byte[] message, CryptoPadding padding) { // Reinterpret encrypted message as a number BigInteger cryptomessage = new BigInteger(message); // Reverse encryption message = Support.ModExp(cryptomessage, d, n).ToByteArray(); // Remove fixed padding byte[] b1 = new byte[message.Length - 1]; Array.Copy(message, b1, message.Length - 1); message = b1; // Remove dynamic padding message = padding.Unpad(message); return(message); }
public byte[] Encrypt(byte[] message, CryptoPadding padding = null, bool sign = false) { // Apply dynamic padding message = (padding ?? NO_PADDING).Pad(message); // Apply fixed padding byte[] b1 = new byte[message.Length + 1]; Array.Copy(message, b1, message.Length); b1[message.Length] = 1; message = b1; // Represent message as a number BigInteger m = new BigInteger(message); // Encrypt message BigInteger cryptomessage = Support.ModExp(m, sign ? d : e, n); // Convert encrypted message back to bytes return(cryptomessage.ToByteArray()); }
public static string SerializePadding(CryptoPadding padding) { XmlDocument doc = new XmlDocument(); doc.AppendChild(padding.GetParameters().Compile(doc)); string output; using (var stream = new MemoryStream()) { XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8); doc.WriteTo(writer); writer.Flush(); stream.Position = 0; using (var reader = new StreamReader(stream)) { output = reader.ReadToEnd(); } } return(output); }
// Encrypt (duh) public byte[] EncryptString(string message, Encoding encoding = null, CryptoPadding padding = null, bool sign = false) => Encrypt((encoding ?? DEFAULT_ENCODING).GetBytes(message), padding, sign);
// Decrypt (duh) public string DecryptString(byte[] message, Encoding encoding = null, CryptoPadding padding = null, bool checkSign = false) => new string((encoding ?? DEFAULT_ENCODING).GetChars(Decrypt(message, padding, checkSign)));
// WIP public static string NetSerialize(CryptoPadding padding) => NetSerialize(padding.GetParameters(), new StringBuilder()).ToString();
public SequentialPadding WithPadding(CryptoPadding padding) { pads.Add(padding); return(this); }
public string DecryptString(byte[] message, CryptoPadding padding) => new string(DEFAULT_ENCODING.GetChars(Decrypt(message, padding)));
public string DecryptString(byte[] message, Encoding encoding, CryptoPadding padding) => new string(encoding.GetChars(Decrypt(message, padding)));
public byte[] EncryptString(string message, Encoding encoding, CryptoPadding padding) => Encrypt(encoding.GetBytes(message), padding);
public byte[] EncryptString(string message, CryptoPadding padding) => Encrypt(DEFAULT_ENCODING.GetBytes(message), padding);