2012-07-05 111 views
6

在我的應用程序,我想實施一些加密。因此我需要Vigenere密碼的代碼。有誰知道我可以在哪裏找到Java的源代碼?我在哪裏可以找到Vigenere密碼的Java源代碼?

+2

AFAIK這是一個非常簡單的密碼,爲什麼不自己實現它?事實上,您可以檢查Java Cryptography庫是否具有實現,無論如何,我不會推薦在現實應用程序中使用Vigenere密碼。 – Egor 2012-07-05 15:14:06

+0

你可以在這裏找到你的答案鏈接 http://stackoverflow.com/questions/10280637/vigenere-cipher-in-java-for-all-utf-8-characters – 2013-04-17 16:25:02

回答

2

這裏是一個鏈接到Vigenere密碼實現Sample Java Code to Encrypt and Decrypt using Vigenere Cipher,除此之外,我不建議使用Vigenere密碼作爲加密。我想推薦jBCrypt

+1

你發佈的鏈接現已停止。 – GeoGriffin 2013-03-27 12:44:47

+0

@GeoGriffin謝謝指出,我已經更新了另一個例子的鏈接。 – 2013-03-27 18:29:11

+0

再次鏈接死亡。 – Omore 2017-04-21 16:34:07

11

這是Vigenere密碼類,您可以使用它,只需調用加密和解密函數: 該代碼是從Rosetta Code

public class VigenereCipher { 
    public static void main(String[] args) { 
     String key = "VIGENERECIPHER"; 
     String ori = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"; 
     String enc = encrypt(ori, key); 
     System.out.println(enc); 
     System.out.println(decrypt(enc, key)); 
    } 

    static String encrypt(String text, final String key) { 
     String res = ""; 
     text = text.toUpperCase(); 
     for (int i = 0, j = 0; i < text.length(); i++) { 
      char c = text.charAt(i); 
      if (c < 'A' || c > 'Z') continue; 
      res += (char)((c + key.charAt(j) - 2 * 'A') % 26 + 'A'); 
      j = ++j % key.length(); 
     } 
     return res; 
    } 

    static String decrypt(String text, final String key) { 
     String res = ""; 
     text = text.toUpperCase(); 
     for (int i = 0, j = 0; i < text.length(); i++) { 
      char c = text.charAt(i); 
      if (c < 'A' || c > 'Z') continue; 
      res += (char)((c - key.charAt(j) + 26) % 26 + 'A'); 
      j = ++j % key.length(); 
     } 
     return res; 
    } 
} 
1

This pos t將幫助你。提供瞭解密的整個代碼。你可以用它來編寫加密代碼

+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/19031471) – Lucifer 2018-03-07 10:42:37

相關問題