void Main() { // Proof of Concept. Jan., 01, 2015. rah // CryptoLambdaIO is a stream of lambda expressions that may come from anywhere, and forms the base of what // is a type of distributed dispatch system for constructing secure and trusted applications that have a built // in payment, versioning, redundancy and copyright protection system. // Code will execute on the Bitcoin protocol, using coins to prevent a whole range of // attack vectors and create a new kind of distributed programming environment for developers where code is shared // and any unique contribution of code provided by any user anywhere will yield automatic royalties to the original developers // and residuals to forkers or maintainers. // In a distributed environment, such as the "Internet of Things" - IoT, these instructions are completely // untrustable without something like the Bitcoin protocol. We also don't want any middle man attacks or other // vectors to somehow interfere with the execution of a distributed program. // Limitations of the C# 5 compiler don't allow lambda expressions assigned to implicitly typed locals or anonymous type // properties. var instructions = new Dictionary(); Expression> add = (x, y) => x + y; Expression> multiply = (x, y) => x * y; instructions.Add(add.GetHashCode(), (LambdaExpression)add); instructions.Add(multiply.GetHashCode(), (LambdaExpression)multiply); instructions.Dump("The instructions:"); var ico = Observable.Create(o => { o.OnNext(add); o.OnNext(multiply); o.OnCompleted(); return Disposable.Empty; }); using (var aes = Aes.Create()) { var encryptedIO = ico.Encrypt(aes.Key, aes.IV); // Note: The signature of the function must be known as well or it will be the wrong type. var plaintextIO = encryptedIO.Decrypt>(aes.Key, aes.IV); // attempt to use a bad key using (var badAes = Aes.Create()) { plaintextIO = encryptedIO.Decrypt>(badAes.Key, badAes.IV); plaintextIO.Subscribe(f => f(6, 7).Dump()); } plaintextIO.Subscribe(f => f(6, 7)); Console.ReadLine(); } } public static class EncryptionExtensions { public static IObservable Encrypt(this IObservable source, byte[] key, byte[] IV) where T : LambdaExpression { return Observable.Create(o => source.Subscribe(t => { using (var aes = Aes.Create()) { var encryptor = aes.CreateEncryptor(key, IV); using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { var formatter = new BinaryFormatter(); formatter.Serialize(cs, t); } var encrypted = ms.ToArray(); o.OnNext(encrypted); o.OnCompleted(); } } }) ); } public static IObservable Decrypt(this IObservable source, byte[] key, byte[] IV) { return Observable.Create(o => source.Subscribe(t => { using (var aes = Aes.Create()) { var decryptor = aes.CreateDecryptor(key, IV); using (var ms = new MemoryStream(t)) { using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) { var formatter = new BinaryFormatter(); T decrypted = (T)formatter.Deserialize(cs); try { o.OnNext(decrypted); o.OnCompleted(); } catch (CryptographicException cex) { o.OnError(cex); } } } } })); } }