Virginia Algorithm encryption algorithm used in Unity

Introduction to Virginia algorithm:

Insert image description here

The Vigenère cipher (also translated as the Vigenard cipher) is an encryption algorithm that uses a series of Caesar ciphers to form a cipher alphabet. It is a simple form of polyalphabetic cipher.
The Vigenère cipher has been invented many times.
The method was first recorded in Giovan Battista Bellaso's 1553 book The Cipher of Signor Giovan Battista Bellaso (Italian: La cifra del. Sig. Giovan Battista Bellaso). However, it was later mistakenly attributed to French diplomat Blaise De Vigenère in the 19th century, and is now known as the "Vigenère Code."
The Vigenère cipher is known for its simplicity and ease of use, and is often difficult for beginners to crack, so it is also known as "the unbreakable cipher" (French: le chiffre indéchiffrable).

Main script:

You can mount the following script to an empty GameObject, and then perform encryption and decryption operations by calling the Encrypt and Decrypt methods.

using UnityEngine;

public class VigenereCipher : MonoBehaviour
{
    
    
    private string key = "SECRET"; // 密钥

    // 加密方法
    public string Encrypt(string plaintext)
    {
    
    
        string encryptedText = "";
        int keyIndex = 0;

        for (int i = 0; i < plaintext.Length; i++)
        {
    
    
            char currentChar = plaintext[i];
            int shift = key[keyIndex] - 'A'; // 将密钥转换为0-25的数字

            if (char.IsLetter(currentChar))
            {
    
    
                if (char.IsUpper(currentChar))
                    encryptedText += (char)((currentChar - 'A' + shift) % 26 + 'A');
                else
                    encryptedText += (char)((currentChar - 'a' + shift) % 26 + 'a');

                keyIndex = (keyIndex + 1) % key.Length;
            }
            else
            {
    
    
                encryptedText += currentChar; // 非字母字符保持不变
            }
        }

        return encryptedText;
    }

    // 解密方法
    public string Decrypt(string encryptedText)
    {
    
    
        string decryptedText = "";
        int keyIndex = 0;

        for (int i = 0; i < encryptedText.Length; i++)
        {
    
    
            char currentChar = encryptedText[i];
            int shift = key[keyIndex] - 'A';

            if (char.IsLetter(currentChar))
            {
    
    
                if (char.IsUpper(currentChar))
                    decryptedText += (char)((currentChar - 'A' - shift + 26) % 26 + 'A');
                else
                    decryptedText += (char)((currentChar - 'a' - shift + 26) % 26 + 'a');

                keyIndex = (keyIndex + 1) % key.Length;
            }
            else
            {
    
    
                decryptedText += currentChar;
            }
        }
        return decryptedText;
    }
}

Key generation script:

using UnityEngine;
using System;
using System.Text;

public class KeyGenerator : MonoBehaviour
{
    
    
    private const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // 生成随机密钥
    public string GenerateRandomKey(int length)
    {
    
    
        StringBuilder keyBuilder = new StringBuilder();

        for (int i = 0; i < length; i++)
        {
    
    
            char randomChar = alphabet[UnityEngine.Random.Range(0, alphabet.Length)];
            keyBuilder.Append(randomChar);
        }

        return keyBuilder.ToString();
    }
}

generate random key

The above script can be mounted to another empty GameObject. You can then call the GenerateRandomKey method to generate a random key of a specified length. For example:

KeyGenerator keyGenerator = GetComponent<KeyGenerator>();
string randomKey = keyGenerator.GenerateRandomKey(10); // 生成长度为10的随机密钥
Debug.Log("Random Key: " + randomKey);

Also note that the key generation should be random and complex enough to increase the security of the password. In a real application, you may use more strict and complex methods to generate keys to ensure security. At the same time, key management is also a very important part and needs to be properly kept and distributed.

Guess you like

Origin blog.csdn.net/weixin_45375968/article/details/132585494