Пример #1
0
        public void TestEncryptDecrypt()
        {
            string testString = "This is a test string969692465!*leusndlYHFBSPWP\"";
            int    rotation   = 3;

            IEncryptor encryptor = new EncryptRotate(rotation);
            IEncryptor decryptor = new DecryptRotate(rotation);

            string encryptedString = encryptor.Map(testString);
            string decryptedString = decryptor.Map(encryptedString);

            Assert.AreEqual(testString, decryptedString, false, "String not equal after encrypt/decrypt");
            Assert.AreNotEqual(encryptedString, decryptedString, false, "Encryption failed as the input and output are the same.");
        }
Пример #2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger for encryption/decryption.");

            //Process input


            Mode   m    = Mode.Unset;
            string data = null;

            IEnumerable <KeyValuePair <string, string> > inputs = req.GetQueryNameValuePairs();

            foreach (KeyValuePair <string, string> kvp in inputs)
            {
                if (kvp.Key.Equals("encrypt", StringComparison.InvariantCultureIgnoreCase))
                {
                    log.Info("Encryption specified.");
                    data = kvp.Value;
                    m    = Mode.Encrypt;
                    break;
                }

                if (kvp.Key.Equals("decrypt", StringComparison.InvariantCultureIgnoreCase))
                {
                    log.Info("Decryption specified.");
                    data = kvp.Value;
                    m    = Mode.Decrypt;
                    break;
                }
            }

            dynamic body = await req.Content.ReadAsAsync <object>();

            //Execute
            IEncryptor ise;

            switch (m)
            {
            case Mode.Encrypt:
                ise = new EncryptRotate(Constants.Rotation);
                break;

            case Mode.Decrypt:
                ise = new DecryptRotate(Constants.Rotation);
                break;

            case Mode.Unset:
                log.Info("Returning advice string.");
                ise = new NullEncryptor("Please use ?encrypt=[StringToEncrypt] or ?decrypt=[StringToDecrypt]");
                break;

            default:
                log.Error("Encryption mode switch not recognised.");
                throw new NotImplementedException("Crypto mode not valid.");
            }


            string processedString = ise.Map(data);

            StringContent sc = ContentGenerator.MakeJsonPayload(processedString);

            return(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = sc
            });
        }