8 Aralık 2022

Basit Encrpytion C# and JS

!Don't use it for important data.

I use it to prevent search engines from collecting data.


.C#


 public static class EasyCrypt

    {

        public static string Encrypt(string input, int shft)

        {

            if (string.IsNullOrEmpty(input))

                return string.Empty;

            return Convert.ToHexString(input.Select(c => Convert.ToByte((((int)c) + shft) % 255)).ToArray());

        }


        public static string Decrypt(string input, int shft)

        {

            if (string.IsNullOrEmpty(input))

                return string.Empty;

            return System.Text.ASCIIEncoding.ASCII.GetString(

                Convert.FromHexString(input).Select(b => Convert.ToByte(((int)b + 255 - shft) % 255)).ToArray());

        }

    }


JS


const cardNo = '321321312';


function encrypt(text, shft = 83) {

    if (text == null ||

        typeof (text) == 'undefined' ||

        text.length < 1)

        return '';

    let encrypted = '';

    for (let i = 0; i < text.length; i += 1) {

        const  charCode = text.charCodeAt(i);

        let shiftedChar = (charCode + shft) % 255;

        encrypted += shiftedChar.toString(16);

    }

    return encrypted;

}


function decrypt(text, shft = 83) {

    if (text == null ||

        typeof (text) == 'undefined' ||

        text.length < 1)

        return '';

    let decrypted = '';

    for (let i = 0; i < text.length; i += 2) {

        const hex = text.substring(i, i + 2);

        const charCode = parseInt(hex,16);        

        let shiftedChar = (255 +(charCode - shft)) % 255;

        decrypted += String.fromCharCode(shiftedChar);

    }

    return decrypted;

}


const encryptedCardNo = encrypt(cardNo);

const decryptedCardNo = decrypt(encryptedCardNo);

console.log(cardNo);

console.log(encryptedCardNo);

console.log(decryptedCardNo);