Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anders Schmidt 76 posts 207 karma points
    Dec 10, 2013 @ 23:29
    Anders Schmidt
    0

    Using encryption class with razor

    I am trying to do some encryption with razor. But without any luck. I have a class in the AppCode folder I am trying to access from razor:

    krypFac.cs

    using System;
    using System.IO;
    using System.Text;
    using System.Security.Cryptography;
    
    namespace Test{
    
    public class krypFac
    {
        private byte[] key = { };
        private byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
    
        public string Decrypt(string stringToDecrypt, string sEncryptionKey)
        {
    
            byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
    
            try
            {
    
                key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey.ToCharArray(), 0, 8);
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(stringToDecrypt);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception e) {
    
                return e.Message;
            }    
        }
    
        public string Encrypt(string stringToEncrypt, string sEncryptionKey)
        {
    
            try
            {
    
                key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey.ToCharArray(), 0, 8);
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception e)
            {
    
                return e.Message;
            }
        }
    
    }
    }

    my view example:

    @using umbraco.BusinessLogic
    @using umbraco.cms.businesslogic.member
    @using umbraco.cms.businesslogic.web
    @using umbraco.editorControls.SettingControls.Pickers
    @using DocumentType = umbraco.cms.businesslogic.web.DocumentType
    @using System.Configuration;
    @using Test;
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
    @{
    
         string Key = ConfigurationManager.AppSettings["GetKey"];    
         var CrypteString = krypFac.Encrypt("myString", Key);

     

    What am i doing wrong?

    Is there another "Best case" solution?

    Thanks in advance

     

  • lothar 25 posts 99 karma points
    Jan 09, 2014 @ 15:44
    lothar
    0

    There are 2 issues i'm seeing in your view.

    1st: The ';' after @using should be removed (no ; after using in views)

    @using System.Configuration
    @using Test
    

    2nd: You are trying to call the Encrypt method in a statical way when you should call it with an object.

    string Key = ConfigurationManager.AppSettings["GetKey"];
    var encryptionObj = new krypFac();
    var CrypteString = encryptionObj.Encrypt("myString", Key);
    

    These 2 should do the trick.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies