凱撒加密、解密演算法,凱撒解密演算法

來源:互聯網
上載者:User

凱撒加密、解密演算法,凱撒解密演算法

古羅馬皇帝凱撒在打仗時曾經使用過以下方法加密軍事情報,主要方法就是字母錯位,下面這張圖加密方法就是錯三個位來實現加密功能

              

(1)  設計思想:

(2)  程式流程圖:

                            

(3)  原始碼:

package temp;import java.util.Scanner;public class Tast2 {    public static  char[] encrypation(String str)    {//加密方法        char chararry[] = str.toCharArray();//把字串轉化為字元數組        for(int i =0; i< str.length(); i++)        {            //當讀到字母表後3位時,加密後是字母表前三位             if(str.charAt(i) >= 88 && str.charAt(i) <= 90)            {//大寫時                chararry[i] = (char)(str.charAt(i) - 23);            }            else if(str.charAt(i) >= 120 && str.charAt(i) <= 122)            {//小寫時                chararry[i] = (char)(str.charAt(i) - 23);            }             //一般加密方法            else            {                chararry[i]= (char) (str.charAt(i) + 3) ;            }                    }        return chararry;    }    public static char[] deciphering(String str)    {//j解密方法        char chararry[] = str.toCharArray();        for(int i =0; i< str.length(); i++)        {            //當讀到字母表前3位時,加密後是字母表後三位             if(str.charAt(i) >= 65 && str.charAt(i) <= 67)            {//大寫時                chararry[i] = (char)(str.charAt(i) + 23);            }            else if(str.charAt(i) >= 97 && str.charAt(i) <= 99)            {//小寫時                chararry[i] = (char)(str.charAt(i) + 23);            }             //一般解密密方法            else            {                chararry[i]= (char) (str.charAt(i) - 3) ;            }                    }        return chararry;    }    public static void main(String[] args) {        Scanner imput1 = new Scanner(System.in);        Scanner imput2 = new Scanner(System.in);        System.out.println("*****************************");        System.out.println("*****加密字串輸入---1---");        System.out.println("*****解密字串輸入---2---");        System.out.println("*****************************");        System.out.print("請輸入:");        int flag = imput1.nextInt();        System.out.print("請輸入一個英文字串:");        String  string = imput2.nextLine();        if(flag == 1)        {            System.out.print("經過加密後為:");            for(int i =0;i <string.length(); i++)            {                System.out.print(Tast2.encrypation(string)[i]);            }        }        if(flag == 2)        {            System.out.print("經過解密後為:");            for(int i =0;i <string.length(); i++)            {                System.out.print(Tast2.deciphering(string)[i]);            }        }            imput1.close();        imput2.close();    }}

(4)  結果:

 

附錄:String類型常用方法整理:

(1)String.equals():

  例:str1.equals(str2);如果字串st1的內容與字串str2內容相同,則返回true,否則返回false

(2)String.length():

     例:str1.length();返回字串str1的長度,或者說是大小

(3)String.charAt():

  例:str1.charAt(index);返回字串str1中下標為index的字元,下標從0開始計數

(4)String.getChars():

  例:str1.getChars(0,5,charArray,0);把字串str1中從下標0開始到下標5結束的子串拷貝到字元數組charArray中,字元數組的起始下標為0

(5)Sting.replace():

  例:str1.replace(oldChar,newChar);返回一個新字元,它擁有與字串str1相同的字元,但是把每一個oldCase字元都替換為newCase字元

(6)Sting.toUpperCase():

  例:str1.toUpperCase();返回一個新字元,它擁有與字串str1相同的字元,但是把所有的小寫字母都轉換成了大寫

(7)Sting.toLowerCase():

  例:str1.toLowerCase();返回一個新字元,它擁有與字串str1相同的字元,但是把所有的大寫字母都轉換成了小寫

(8)Sting.trim():

  例:str1.trim();返回一個新字元,它擁有與字串str1相同的字元,但是刪除了字串str1的前後空白字元,(空白字元包括空格、定位字元以及轉行字元)

(9)String.toCharArray():

  例:chararray[] = str1.toCharArray();把字串str1轉換成字元數組,並儲存在chararrayp[]中

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.