網站首頁 編程語言 正文
Vigenere 加密算法
該密碼由意大利密碼學家 Giovan Battista Bellaso 于 1553 年發明,但幾個世紀以來一直歸功于 16 世紀的法國密碼學家 Blaise de Vigenère,他在 1586 年設計了類似的密碼。
Vigenere Cipher 是一種加密字母文本的方法。它使用一種簡單的多字母表替換形式。多字母密碼是基于替換的任何密碼,使用多個替換字母表。原始文本的加密是使用 Vigenère square 或 Vigenère table 完成的。
該表由在不同行中寫出 26 次的字母組成,與前一個字母相比,每個字母循環向左移動,對應于 26 種可能的凱撒密碼。
在最簡單的 Vigenère 類型系統中,密鑰是一個單詞或短語,它可以根據需要重復多次以加密消息。如果密鑰是欺騙性的,并且消息是我們被發現了,請自救,那么生成的密碼將是
在加密過程的不同點,密碼使用與其中一行不同的字母表。每個點使用的字母取決于重復的關鍵字。
又例如:
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 與密鑰的第一個字母 A 配對。所以使用 Vigenère 正方形的 G 行和 A 列,即 G。同理,對于明文的第二個字母,使用密鑰的第二個字母,E 行的字母,Y 列的字母是 C。明文以類似的方式加密。
解密的方法是到表中與密鑰對應的行,找到該行中密文字母的位置,然后將該列的標簽作為明文。例如,在 A 行(來自 AYUSH)中,密文 G 出現在 G 列中,這是第一個明文字母。接下來,我們轉到 Y 行(來自 AYUSH),找到在 E 列中找到的密文 C,因此 E 是第二個明文字母。
一個更簡單的實現可能是通過將 [A-Z] 轉換為數字 [0-25] 以代數方式可視化 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
相關推薦
- 2022-06-09 Nginx速查手冊及常見問題_nginx
- 2022-08-04 django中使用memcached示例詳解_python
- 2023-12-15 Linux系統中date命令、hwclock命令 語法詳解
- 2022-03-30 聊聊redis-dump工具安裝問題_Redis
- 2022-10-01 SQL中concat和substr組合運用解析_MsSql
- 2022-09-20 Python使用Flask?Migrate模塊遷移數據庫_python
- 2022-07-31 Android中View.post和Handler.post的關系_Android
- 2022-12-26 python-opencv如何讀取圖片及尺寸修改_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支