How to resolve the algorithm Substitution cipher step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Substitution cipher step by step in the V (Vlang) programming language

Table of Contents

Problem Statement

Substitution Cipher Implementation - File Encryption/Decryption

Encrypt an input/source file by replacing every upper/lower case alphabets of the source file with another predetermined upper/lower case alphabets or symbols and save it into another output/encrypted file and then again convert that output/encrypted file into original/decrypted file. This type of Encryption/Decryption scheme is often called a Substitution Cipher.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Substitution cipher step by step in the V (Vlang) programming language

Source code in the v programming language

const 
(
	key = "]kYV}(!7P\$n5_0i R:?jOWtF/=-pe'AD&@r6%ZXs\"v*N[#wSl9zq2^+g;LoB`aGh{3.HIu4fbK)mU8|dMET><,Qc\\C1yxJ"
	text = "The quick brown fox jumps over the lazy dog, who barks VERY loudly!"
)

fn main() {
	encoded := encode(text)
	println(encoded)
	println(decode(encoded))
}

fn encode(str string) string {
	mut chr_arr := []u8{}
	for chr in str {
		chr_arr << key[u8(chr - 32)]
	}
	return chr_arr.bytestr()
}

fn decode(str string) string {
	mut chr_arr := []u8{}
	for chr in str {
		chr_arr << u8(key.index_u8(chr) + 32)
	}
	return chr_arr.bytestr()
}

  

You may also check:How to resolve the algorithm Bitmap/Read an image through a pipe step by step in the zkl programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the jq programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the Nim programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the Scilab programming language
You may also check:How to resolve the algorithm Soundex step by step in the TMG programming language