encrypt input text via arguments

This commit is contained in:
Florian Hoss 2022-05-30 13:24:48 +02:00
parent 1b1945779d
commit ed84374a45
3 changed files with 88 additions and 20 deletions

View file

@ -0,0 +1,25 @@
New Zealand's capital, Wellington, has been ranked one of the least affordable cities in the world for buying a property. The picture is also grim for renters, with a 12% rise in prices in the past year. That, along with increases in petrol and food prices, has led many to consider moving to nearby Australia - where they have the right to live and work.
Chris, a builder, his partner Harmony and their four daughters recently left Wellington to start a new life in the Australian city of Brisbane. Despite owning their home and earning reasonable salaries, they were still struggling.
"We have four kids, so it was expensive. We'd notice Australians saying you know the cost of living is going up - but that was the cost five years ago in New Zealand," says Chris.
Leaving New Zealand and the rest of her family was a difficult decision for Harmony. But she says the move was necessary for the children.
"You can't make a living in New Zealand. There is no living. You just go backwards. You don't get a choice if you want to live, you have to move, or New Zealand has to change. I want a future for my children and there is none in New Zealand," she says.
The New Zealand government has tried to increase some short-term measures like fuel subsidies and halving the cost of public transport - but for many, it's not enough.
In Brescia, Italy, steel runs through the veins of the community. In the past 15 years the industry has endured the financial crash and the Covid-19 pandemic. Now, with the war in Ukraine and Covid lockdowns in China, trade is being disrupted further.
Mirella and Lucas met at a cast iron foundry in Brescia. Their two steady wages are up against rising food, petrol and energy costs.
"With regard to electricity, we have recently suffered like everyone else. Our bill has doubled - even though we are never at home", says Mirella. "We are tightening our belts. Instead of saving a lot, you'll save less," says Lucas.
Orders at this cast iron foundry continue. But a crucial source of raw materials from the south-eastern Ukrainian city of Mariupol are in now short supply, after Russian troops occupied the region.
Mark Impraim owns a catering business in Ghana - one of the most expensive countries to live in, in Africa. He shops for ingredients for one of his most popular dishes - jollof rice - at a local market. But prices have doubled in recent months.
Mark looks at a bucket of tomatoes, dismayed at the price tag. "[This box of tomatoes] used to be 20 cedis. Now it's going for 40," he says. "I should double the price of the food I serve, but that would scare away customers. I try to find a way around it by decreasing the quantity."
Among other rising costs eating into Mark's weekly budget is a supply of drinking water. Sachets of water have increased in price twice in four months, due to the devaluation of the cedi. Water suppliers say passing the costs on to customers is unavoidable.

View file

@ -1,8 +1,10 @@
package main
import (
"fmt"
"flag"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"unicode"
@ -10,33 +12,51 @@ import (
const lettersInTheAlphabet = 26
const startUpperCase = 65
const endUpperCase = 65 + lettersInTheAlphabet
const endUpperCase = startUpperCase + lettersInTheAlphabet
const startLowerCase = 97
const endLowerCase = 97 + lettersInTheAlphabet
var key string
const endLowerCase = startLowerCase + lettersInTheAlphabet
type keyLetter struct {
upperCase string
position int32
}
func getKeyWord() {
var input string
fmt.Println("Please enter a key word:")
_, err := fmt.Scanln(&input)
func parseArguments() (string, string) {
var inputKey string
var inputFile string
flag.StringVar(&inputKey, "k", "", "Specify key word.")
flag.StringVar(&inputFile, "f", "", "Specify file to encrypt.")
flag.Parse()
return inputKey, inputFile
}
func readFile(relativePath string) string {
content, err := ioutil.ReadFile(relativePath)
if err != nil {
log.Fatalln(err)
}
return string(content)
}
func writeFile(relativePath string, message string) {
file, err := os.Create(relativePath)
if err != nil {
log.Fatalln(err)
}
defer file.Close()
file.WriteString(message)
}
func validateKeyWord(inputKey string) string {
reg, err := regexp.Compile("[^a-zA-Z]+")
if err != nil {
log.Fatalln(err)
}
processedString := reg.ReplaceAllString(input, "")
key = strings.ToUpper(processedString)
processedString := reg.ReplaceAllString(inputKey, "")
return strings.ToUpper(processedString)
}
func initKeyWord() []keyLetter {
func initKeyWord(key string) []keyLetter {
var keyWord []keyLetter
for _, letter := range key {
keyWord = append(keyWord, keyLetter{
@ -44,7 +64,6 @@ func initKeyWord() []keyLetter {
position: letter - 'A',
})
}
fmt.Println(keyWord)
return keyWord
}
@ -52,8 +71,8 @@ func encryptMessage(keyWord []keyLetter, message string) string {
encrypted := ""
index := 0
for _, letter := range message {
if letter == 32 {
encrypted += " "
if !unicode.IsLetter(letter) {
encrypted += string(letter)
} else {
keyWordIndex := index % len(keyWord)
newLetter := letter + keyWord[keyWordIndex].position
@ -69,9 +88,8 @@ func encryptMessage(keyWord []keyLetter, message string) string {
}
func main() {
message := "Make It Happen"
fmt.Println(message)
getKeyWord()
keyWord := initKeyWord()
fmt.Println(encryptMessage(keyWord, message))
inputKey, inputFile := parseArguments()
message := readFile(inputFile)
keyWord := initKeyWord(validateKeyWord(inputKey))
writeFile("output.txt", encryptMessage(keyWord, message))
}

View file

@ -0,0 +1,25 @@
Fsi Dxhtntv'w eugmlox, Axstvtyxqh, yek pqig yiaqwh qhv sx hti elifz sjhiihspxi vpbvkk mp nyi ocdpw mwe hmckhx e hfatxybl. Zzi rctxmfq ml htfu yvkg wsj fqrmlzf, caxj u 12% imks ur iyqpkk mp nyi hoex rlie. Zzev, ucsfu immo qaijicmvw ab bimywy gfh hifh hfugxz, pny dif grrq ha ghuavjwv oimmfu fs gliehq Ewmkvszue - pomek llgs yens flx yqtnl xq fzzw ozh pvzx.
Izvkm, r fmwxhxy, pvy hetneij Vmvfvvl gfh vbvmj tayk kihmzxglj vwqqrmsg ykxx Yycpabsxhu bb yletn r rwk xmyl qa zzi Cojxjoxmtu kvzq sh Vimkpmrx. Kmfvaxg inrabs xalqe ngqg ueh wodrbuo ekswqhrfds eeehzvkk, xjyp awfq wmpty ylvwaxpabs.
"Ax oiik xswl bmvg, es ba eny wbryewajq. Ax'k vbzagg Ulwlfmpbhvf ysckhx cgi wrhd buk usun fj dwhmgn qf mgmpa lt - tif xahb jgk xjy tskh rmol grgjw caf mf Bqa Sliygfh," uupw Uvdml.
Smnbari Hva Rsmptul ntv xjy iikh aj alz sgemns nek o pmymqpadx fytmkwar yvz Ugjqqhp. Fmh elx zily llg gfzw kmw glkrykets wsj hti voqyjjip.
"Sfy uoz'x fhsr g dmxcek ab Zip Gmnrsrf. Nyijs uw gv tvbari. Sfy biex zv jnicacluw. Qcg hhu'b tkl e ebfmus uj rvc jgfx vi cmns, ksn oiik ls oimi, gf Zip Gmnrsrf brw lc oltuor. O oepn r jmhgvx mwe sq gjcchjsz egk bukji km esfs ur Gle Mkspchu," wzs eerz.
Buk Fiy Tvedozh zvdrxfqghk lsg fvbll gu arelveks esfl auujx-vyiq esmwnymf raog zlid ggflplvkk epx yedjurz apr igwv iw tmpxmv azntktqlk - fmh rsk tiae, ax'u hfx wbayzo.
Qa Hjiuwze, Ahmpr, zbrkd vwhj xzfayzo buk nikhj sx hti vvuzafmvs. Zr lvq ttzb 15 lksvu nyi abpylazl nsw ghuyjsp xal nvtsrecrp ufmwa hvq zzi Eimmv-19 dmrwluvi. Fsy, qzxz hti phz vt Motuzrw ozh Vvdvj dseeusobe mg Jpvts, xtuui ag nibuo qokvwjkiv tgvmome.
Savgfce sbp Pnjif swx cn r gsgf mkvv sumrflp mf Pdiljqn. Zzikl kag gfitkg jgyiu uii md mktpvfz jmucek xcah, ilbeud epx vrwfsc vvagy.
"Omvb iiyodh mv mykuxtctmlm, ii ahdr xwgghkpq ggjylzrj dmmy vzwfksgl myyw. Swl smdz tel kwhhdif - ymif htsnnp jk svg hvzwf mx avur", yscu Gzvwzxe. "Pl iek lmibkifwzk hbz okdxu. Cewlsmh hm anbari u csl, may'es anbw pgmj," wsme Pnjif.
Ujhglj el html jifz avqh wsmbpvr jwazarwy. Syl o ovnjqnr kswlti gt dep tigkjmcfj jjcy xal aball-gujxwfz Ydyivtaep wzxq cr Qtyqhvgp clv mf baa lowez kyrjcc, stfik Ycfyaep nisgde svjccowh vbv vwuusg.
Tieq Aqrlrme cirl h knzwvkhx fmgurxza vt Ylchr - sfs aj mom zukx grgifguzx jwhtlvkyj xg zuzx pv, vt Sjtcte. Zs elhwa suj mpaiivwqrmz nbx grg iw lag ysla xbvmpcl umkvqw - cvtyux vkwv - el o xsvht zgjogn. Syl ddmvla ugni filfdsp mg ympkfx oiexzg.
Yekr tbucw cn r fmqwim vn gueevivw, vweqtfmq gl xjy gvaqq xtn. "[Buok fqr fj lcyemvmf] akif nf fw 20 qqhbz. Vbc ax'u afmfu rsk 40," om fgqw. "K mysmzp hhbjyk llg jimus aj mom sugh K mvvns, nym apnz oswfu wuodi tdil imwvidijg. U xkf bb larf u neq odsnul vz tc fytvwoemgn buk iychkmlm."
Mqhuo bzzit lzwabs ghzbf ksxkhx mfha Qtys'f cwimfp fmrsim pa n ymtrfp sx rdmgrqam oevyi. Wsqtimz ws csxgl yens urvymnywh kh gvaqq xppkr of jqoi qgbfll, kcr zg xjy uinoxytaqbt gj vbv gwru. Atame ymtrfzijg eer wifyari nyi ucexl vv gu uyunfqwfe ml bvnbgmfuspw.