Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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());
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
 // Encrypt (duh)
 public byte[] EncryptString(string message, Encoding encoding = null, CryptoPadding padding = null, bool sign = false) => Encrypt((encoding ?? DEFAULT_ENCODING).GetBytes(message), padding, sign);
Esempio n. 6
0
 // 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)));
Esempio n. 7
0
 // WIP
 public static string NetSerialize(CryptoPadding padding) => NetSerialize(padding.GetParameters(), new StringBuilder()).ToString();
Esempio n. 8
0
 public SequentialPadding WithPadding(CryptoPadding padding)
 {
     pads.Add(padding);
     return(this);
 }
Esempio n. 9
0
 public string DecryptString(byte[] message, CryptoPadding padding) => new string(DEFAULT_ENCODING.GetChars(Decrypt(message, padding)));
Esempio n. 10
0
 public string DecryptString(byte[] message, Encoding encoding, CryptoPadding padding) => new string(encoding.GetChars(Decrypt(message, padding)));
Esempio n. 11
0
 public byte[] EncryptString(string message, Encoding encoding, CryptoPadding padding) => Encrypt(encoding.GetBytes(message), padding);
Esempio n. 12
0
 public byte[] EncryptString(string message, CryptoPadding padding) => Encrypt(DEFAULT_ENCODING.GetBytes(message), padding);