Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,948 questions

51,890 answers

573 users

How to invent basic encryption for a 6-digit password in C#

4 Answers

0 votes
using System;

class Program
{
    static void Main(string[] args)
    {
        int pass = 792531;

        Console.WriteLine("password     = {0}", pass);

        int[] arr = IntToArray(pass);

        int[] encoded = Encode(arr);
        int encodePass = ArrayToInt(encoded);
        Console.WriteLine("encode_pass  = {0}", encodePass);

        int[] decoded = Decode(encodePass);
        int decodePass = ArrayToInt(decoded);
        Console.WriteLine("decode_pass  = {0}", decodePass);
    }

    // ---------------------------------------------------------
    // Encode: swap digit 1 with second‑last, and digit 2 with third‑last
    // ---------------------------------------------------------
    public static int[] Encode(int[] arr) {
        int[] copy = (int[])arr.Clone();

        Swap(copy, 1, copy.Length - 2);
        Swap(copy, 2, copy.Length - 3);

        return copy;
    }

    // ---------------------------------------------------------
    // Decode: reverse the same swaps
    // ---------------------------------------------------------
    public static int[] Decode(int encoded) {
        int[] arr = IntToArray(encoded);

        Swap(arr, arr.Length - 2, 1);
        Swap(arr, arr.Length - 3, 2);

        return arr;
    }

    // ---------------------------------------------------------
    // Convert integer → array of digits
    // ---------------------------------------------------------
    public static int[] IntToArray(int num) {
        char[] chars = num.ToString().ToCharArray();
        int[] arr = new int[chars.Length];

        for (int i = 0; i < chars.Length; i++)
            arr[i] = chars[i] - '0';

        return arr;
    }

    // ---------------------------------------------------------
    // Convert array of digits → integer
    // ---------------------------------------------------------
    public static int ArrayToInt(int[] arr) {
        int value = 0;
        foreach (int d in arr)
            value = value * 10 + d;

        return value;
    }

    // ---------------------------------------------------------
    // Swap helper
    // ---------------------------------------------------------
    public static void Swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}

 
 
/*
run:
 
password     = 792531
encode_pass  = 735291
decode_pass  = 792531
 
*/


answered Apr 7, 2014 by avibootz
edited Jan 10 by avibootz
0 votes
using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        // Original 6‑digit password
        string pass = "463912";
        
        // Encrypt the password using a digit‑shift of 3
        string enc = Encrypt(pass, 3);

        // Decrypt it back using the same shift
        string dec = Decrypt(enc, 3);

        // Show results
        Console.WriteLine(enc); // encrypted output
        Console.WriteLine(dec); // original password restored
    }
    
    // Decrypts a numeric string by shifting each digit backward
    static string Decrypt(string input, int shift) {
        char[] result = new char[input.Length];
    
        for (int i = 0; i < input.Length; i++) {
            // Convert character → digit (0–9)
            int d = input[i] - '0';

            // Reverse the shift, wrap around using modulo
            d = (d - shift + 10) % 10;

            // Convert digit back → character
            result[i] = (char)('0' + d);
        }
    
        return new string(result);
    }

    // Encrypts a numeric string by shifting each digit forward
    static string Encrypt(string input, int shift) {
        char[] result = new char[input.Length];
    
        for (int i = 0; i < input.Length; i++) {
            // Convert character → digit (0–9)
            int d = input[i] - '0';

            // Apply the shift, wrap around using modulo
            d = (d + shift) % 10;

            // Convert digit back → character
            result[i] = (char)('0' + d);
        }
    
        return new string(result);
    }
}


/*
run:

796245
463912

*/

 



answered Jan 10 by avibootz
0 votes
using System;

class Program
{
    static void Main(string[] args)
    {
        // Original 6‑digit password
        string pass = "463912";
        
        // Encrypt the password using the custom method
        string enc = Encrypt(pass);

        // Decrypt it back to verify correctness
        string dec = Decrypt(enc);

        Console.WriteLine(enc); // encrypted output
        Console.WriteLine(dec); // original password restored
    }
    
    // Encrypts a numeric string using:
    // 1. Reverse the digits
    // 2. Add 7 to each digit (mod 10)
    static string Encrypt(string input) {
        // Convert string → char array so we can modify it
        char[] arr = input.ToCharArray();

        // Step 1: reverse the order of digits
        Array.Reverse(arr);
    
        // Step 2: shift each digit by +7 (wrap around with modulo)
        for (int i = 0; i < arr.Length; i++) {
            int d = arr[i] - '0';      // convert char → digit
            d = (d + 7) % 10;          // apply shift
            arr[i] = (char)('0' + d);  // convert digit → char
        }
    
        return new string(arr);
    }

    // Decrypts the string by reversing the encryption steps:
    // 1. Subtract 7 from each digit (mod 10)
    // 2. Reverse the digits back
    static string Decrypt(string input) {
        char[] arr = input.ToCharArray();
    
        // Step 1: reverse the +7 shift by subtracting 7
        for (int i = 0; i < arr.Length; i++) {
            int d = arr[i] - '0';          // convert char → digit
            d = (d - 7 + 10) % 10;         // undo shift safely
            arr[i] = (char)('0' + d);      // convert digit → char
        }
    
        // Step 2: reverse the array back to original order
        Array.Reverse(arr);
        
        return new string(arr);
    }
}



/*
run:

986031
463912

*/


 



answered Jan 10 by avibootz
0 votes
using System;

class Program
{
    static void Main(string[] args)
    {
        // Original 6‑digit password
        string pass = "463912";

        int key = 5;                 // XOR key (must match for encrypt/decrypt)

        // Encrypt the password
        string enc = EncryptXor(pass, key);

        // Decrypt it back
        string dec = DecryptXor(enc, key);

        Console.WriteLine(enc); // encrypted output
        Console.WriteLine(dec); // original password restored
    }
    
    static string EncryptXor(string input, int key) {
        char[] result = new char[input.Length];
    
        for (int i = 0; i < input.Length; i++) {
            int d = input[i] - '0';       // convert digit to int
            d = d ^ key;                  // XOR with key
            result[i] = (char)('A' + d);  // store as letters
        }
    
        return new string(result);
    }

    static string DecryptXor(string input, int key) {
        char[] result = new char[input.Length];
    
        for (int i = 0; i < input.Length; i++) {
            int d = input[i] - 'A';       // convert letter back to number
            d = d ^ key;                  // XOR again to restore digit
            result[i] = (char)('0' + d);  // convert back to digit char
        }
    
        return new string(result);
    }
}


/*
run:

BDGMEH
463912

*/


 



answered Jan 10 by avibootz

Related questions

1 answer 97 views
2 answers 167 views
1 answer 191 views
1 answer 214 views
1 answer 165 views
165 views asked Apr 25, 2017 by avibootz
1 answer 146 views
146 views asked Apr 25, 2017 by avibootz
...