網(wǎng)站首頁 編程語言 正文
Vigenere 加密算法
該密碼由意大利密碼學(xué)家 Giovan Battista Bellaso 于 1553 年發(fā)明,但幾個(gè)世紀(jì)以來一直歸功于 16 世紀(jì)的法國密碼學(xué)家 Blaise de Vigenère,他在 1586 年設(shè)計(jì)了類似的密碼。
Vigenere Cipher 是一種加密字母文本的方法。它使用一種簡單的多字母表替換形式。多字母密碼是基于替換的任何密碼,使用多個(gè)替換字母表。原始文本的加密是使用 Vigenère square 或 Vigenère table 完成的。
該表由在不同行中寫出 26 次的字母組成,與前一個(gè)字母相比,每個(gè)字母循環(huán)向左移動,對應(yīng)于 26 種可能的凱撒密碼。
在最簡單的 Vigenère 類型系統(tǒng)中,密鑰是一個(gè)單詞或短語,它可以根據(jù)需要重復(fù)多次以加密消息。如果密鑰是欺騙性的,并且消息是我們被發(fā)現(xiàn)了,請自救,那么生成的密碼將是
在加密過程的不同點(diǎn),密碼使用與其中一行不同的字母表。每個(gè)點(diǎn)使用的字母取決于重復(fù)的關(guān)鍵字。
又例如:
Input : Plaintext : GEEKSFORGEEKS Keyword : AYUSH Output : Ciphertext : GCYCZFMLYLEIM For generating key, the given keyword is repeated in a circular manner until it matches the length of the plain text. The keyword "AYUSH" generates the key "AYUSHAYUSHAYU" The plain text is then encrypted using the process explained below.
加密:
明文的第一個(gè)字母 G 與密鑰的第一個(gè)字母 A 配對。所以使用 Vigenère 正方形的 G 行和 A 列,即 G。同理,對于明文的第二個(gè)字母,使用密鑰的第二個(gè)字母,E 行的字母,Y 列的字母是 C。明文以類似的方式加密。
解密的方法是到表中與密鑰對應(yīng)的行,找到該行中密文字母的位置,然后將該列的標(biāo)簽作為明文。例如,在 A 行(來自 AYUSH)中,密文 G 出現(xiàn)在 G 列中,這是第一個(gè)明文字母。接下來,我們轉(zhuǎn)到 Y 行(來自 AYUSH),找到在 E 列中找到的密文 C,因此 E 是第二個(gè)明文字母。
一個(gè)更簡單的實(shí)現(xiàn)可能是通過將 [A-Z] 轉(zhuǎn)換為數(shù)字 [0-25] 以代數(shù)方式可視化 Vigenère。
Go 代碼
package main import ( "fmt" "strings" ) func encodeString(cipher, key rune) rune { const asciiA rune = 65 const numLetters = 26 plainTextIndex := cipher + key asciiLetter := (plainTextIndex+numLetters)%numLetters + asciiA return asciiLetter } func encode(message, kw string) string { var plainText strings.Builder kwChars := []rune(kw) for i, cipherChar := range message { key := i % len(kwChars) plainText.WriteRune(encodeString(cipherChar, kwChars[key])) } return plainText.String() } func decipherString(cipher, key rune) rune { const asciiA rune = 65 const numLetters = 26 plainTextIndex := cipher - key asciiLetter := (plainTextIndex+numLetters)%numLetters + asciiA return asciiLetter } func decipher(message, kw string) string { var plainText strings.Builder kwChars := []rune(kw) for i, cipherChar := range message { key := i % len(kwChars) plainText.WriteRune(decipherString(cipherChar, kwChars[key])) } return plainText.String() } func main() { fmt.Println("Enter Your string: ") var first string fmt.Scanln(&first) fmt.Println("Enter your KEY: ") var second string fmt.Scanln(&second) cipherText := first keyword := second fmt.Print("Do you want to 1. Encrypt or 2. Decrypt") var option int fmt.Scanln(&option) if option == 1 { fmt.Println(encode(cipherText, keyword)) } else if option == 2 { fmt.Println(decipher(cipherText, keyword)) } else { fmt.Println("please choose the right option") } }
原文鏈接:https://juejin.cn/post/7140296036429348878
相關(guān)推薦
- 2022-08-21 python數(shù)字圖像處理之邊緣輪廓檢測_python
- 2022-10-09 淺談C++中各種不同意義的new和delete的使用_C 語言
- 2022-07-03 Golang之空結(jié)構(gòu)體和零長數(shù)組的實(shí)踐
- 2022-11-28 協(xié)程作用域概念迭代RxTask?實(shí)現(xiàn)自主控制_Android
- 2023-03-25 Android?Jetpack組件ViewModel基本用法詳解_Android
- 2022-03-14 Springboot中遇到的問題——Failed to load ApplicationContex
- 2022-12-29 Python利用卡方Chi特征檢驗(yàn)實(shí)現(xiàn)提取關(guān)鍵文本特征_python
- 2022-03-19 Linux系統(tǒng)下安裝Redis數(shù)據(jù)庫過程_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支