public byte[] Encrypt(byte[] data, int offset, int length) { using var buffer = new MemoryStream(255); using var writer = new BinaryWriter(buffer); using (var sha1 = new SHA1Managed()) { var hashsum = sha1.ComputeHash(data, offset, length); writer.Write(hashsum); } buffer.Write(data, offset, length); if (length < 235) { var padding = Helpers.GenerateRandomBytes(235 - length); buffer.Write(padding, 0, padding.Length); } var ciphertext = new BigInteger(1, buffer.ToArray()).ModPow(Exponent, Modulus).ToByteArrayUnsigned(); if (ciphertext.Length == 256) { return(ciphertext); } var paddedCiphertext = new byte[256]; var paddingLength = 256 - ciphertext.Length; for (var i = 0; i < paddingLength; i++) { paddedCiphertext[i] = 0; } ciphertext.CopyTo(paddedCiphertext, paddingLength); return(paddedCiphertext); }
public void WriteDecimal(decimal value, int scale, int len) { var bytes = new BigInteger(value * (long)Math.Pow(10, scale)).ToByteArray(isBigEndian: true); var fix = new byte[len]; var offset = len - bytes.Length; if (value < 0) { Array.Fill(fix, (byte)0xFF, 0, offset); } bytes.CopyTo(fix, offset); WriteFixed(fix); }
public byte[] Encrypt(byte[] data, int offset, int length) { logger.debug("rsa plaintext: {0}", BitConverter.ToString(data, offset, length)); using (MemoryStream buffer = new MemoryStream(255)) using (BinaryWriter writer = new BinaryWriter(buffer)) { using (SHA1 sha1 = new SHA1Managed()) { byte[] hashsum = sha1.ComputeHash(data, offset, length); logger.debug("hashsum: {0}", BitConverter.ToString(hashsum)); writer.Write(hashsum); } buffer.Write(data, offset, length); if (length < 235) { byte[] padding = new byte[235 - length]; new Random().NextBytes(padding); buffer.Write(padding, 0, padding.Length); } logger.debug("rsa plaintext with hashsum and padding: {0}", BitConverter.ToString(buffer.ToArray())); byte[] ciphertext = new BigInteger(1, buffer.ToArray()).ModPow(e, m).ToByteArrayUnsigned(); logger.debug("ciphertext length: {0}, ciphertext: {1}", ciphertext.Length, BitConverter.ToString(ciphertext)); if (ciphertext.Length == 256) { return(ciphertext); } else { byte[] paddedCiphertext = new byte[256]; int padding = 256 - ciphertext.Length; for (int i = 0; i < padding; i++) { paddedCiphertext[i] = 0; } ciphertext.CopyTo(paddedCiphertext, padding); logger.debug("ciphertext with padding: {0}", BitConverter.ToString(paddedCiphertext)); return(paddedCiphertext); } } }
public byte[] Encrypt(byte[] data, int offset, int length) { using (var buffer = new MemoryStream(255)) using (var writer = new BinaryWriter(buffer)) { using (var sha1 = SHA1.Create()) { var hashsum = sha1.ComputeHash(data, offset, length); writer.Write(hashsum); } buffer.Write(data, offset, length); if (length < 235) { var padding = new byte[235 - length]; // new Random().NextBytes(padding); buffer.Write(padding, 0, padding.Length); } var ciphertext = new BigInteger(1, buffer.ToArray()).ModPow(_e, _m).ToByteArrayUnsigned(); if (ciphertext.Length == 256) { return(ciphertext); } { var paddedCiphertext = new byte[256]; var padding = 256 - ciphertext.Length; for (var i = 0; i < padding; i++) { paddedCiphertext[i] = 0; } ciphertext.CopyTo(paddedCiphertext, padding); return(paddedCiphertext); } } }
public byte[] Encrypt(byte[] data, int offset, int length) { using (MemoryStream buffer = new MemoryStream(255)) using (BinaryWriter writer = new BinaryWriter(buffer)) { using (SHA1 sha1 = new SHA1Managed()) { byte[] hashsum = sha1.ComputeHash(data, offset, length); writer.Write(hashsum); } buffer.Write(data, offset, length); if (length < 235) { byte[] padding = new byte[235 - length]; new Random().NextBytes(padding); buffer.Write(padding, 0, padding.Length); } byte[] ciphertext = new BigInteger(1, buffer.ToArray()).ModPow(this.e, this.m).ToByteArrayUnsigned(); if (ciphertext.Length == 256) { return(ciphertext); } else { byte[] paddedCiphertext = new byte[256]; int padding = 256 - ciphertext.Length; for (int i = 0; i < padding; i++) { paddedCiphertext[i] = 0; } ciphertext.CopyTo(paddedCiphertext, padding); return(paddedCiphertext); } } }