Board logo

標題: [【學科】] 【程式碼】如何撰寫凱薩加密法? [打印本頁]

作者: 46733194    時間: 2016-4-26 08:37     標題: 【程式碼】如何撰寫凱薩加密法?

本帖最後由 46733194 於 2016-4-26 03:33 編輯

凱薩加密是用平移的方法去加密透過modulo進行加密解密

凱薩加密的平移數量為3
但是我想輸入一個隨機的數


那麼在JAVA上要如何撰寫凱薩加密
輸出結果如下

key(平移數):
輸入明文:
ciphertext(密文):
輸入密文:
plaintext(明文):
作者: 46733194    時間: 2016-4-26 12:50

import java.util.Scanner;
import java.lang.*;

public class password {

        public static void main(String[] args) {
               
                Scanner sc=new Scanner(System.in);
                System.out.println("key:");
                int key=sc.nextInt();                
                System.out.println("輸入plaintext:");               
                String plain=sc.next();               
               
                char[] d =new char[plain.length()];
               
                for ( int i = 0; i < plain.length(); ++i ) {
                        char c = plain.charAt( i );
                        int n = (int) c;
                        if(n<91 && n>64){
                                n=n+key-65;
                                n=n%26;
                                n+=65;
                        }
                        if(n<123 && n>96){
                                n=n+key-97;
                                n=n%26;
                                n+=97;
                        }
                        if(n<58 && n>47){
                                n=n+key-48;
                                n=n%10;
                                n+=48;
                        }
                       
                        d[i]= (char)n;
                }
                System.out.println("加密ciphertext:");
                System.out.println(d);
                System.out.println("輸入ciphertext:");               
                String cipher=sc.next();
               
                for ( int i = 0; i < cipher.length(); ++i ) {
                        char c =   cipher.charAt( i );
                        int n = (int) c;
                        if(n<91 && n>64){
                                n=n-key-65;
                                if(n<0)
                                        n=n+(26*key);
                                n=n%26;
                                n+=65;
                        }
                        if(n<123 && n>96){
                                n=n-key-97;
                                if(n<0)
                                        n=n+(26*key);
                                n=n%26;
                                n+=97;
                        }
                        if(n<58 && n>47){
                                n=n-key-48;
                                if(n<0)
                                        n=n+(10*key);
                                n=n%10;
                                n+=48;
                        }
                       
                        d[i]= (char)n;
                }
                System.out.println("解密plaintext:");
                System.out.println(d);
        }

}
作者: 46733194    時間: 2016-4-26 12:51

程式碼的部分我已經想到且完成了
如果有人對程式碼內容有興趣
我再解釋吧!
作者: 39475494    時間: 2016-4-26 14:00

n=n-key-97;
                                if(n<0)
                                        n=n+(26*key);
                                n=n%26;

你可以考慮
    key2 = key % 26;
再去處理
作者: 46733194    時間: 2016-4-26 18:05

本帖最後由 46733194 於 2016-4-26 10:34 編輯
key2 = key % 26;


加了這步後
程式碼似乎沒省
而且多了bug
作者: 39475494    時間: 2016-4-27 09:34

本帖最後由 39475494 於 2016-4-27 09:40 編輯
加了這步後
程式碼似乎沒省
而且多了bug
46733194 發表於 2016-4-26 18:05
用key2 是因為不喜歡
  n=n+(26*key);
這種思維,怕溢位問題

不過還好是 int 很大,key不大是不用怕

多了 bug,那就看你怎麼寫的
字母 26 一循環的規則,去縮 key%26 並不影響才對
數字請用 key2 = key % 10;


用減的話,負的要加 26




歡迎光臨 Discuz! Board (http://bbs.61.com.tw/) Powered by Discuz! 7.2