Beispiel #1
0
        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(e, 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;
                }
            }
        }