字符串加解密
題目描述
題目描述 1、對輸入的字符串進行加解密,并輸出。 2加密方法為: 當內容是英文字母時則用該英文字母的后一個字母替換,同時字母變換大小寫,如字母a時則替換為B;字母Z時則替換為a; 當內容是數字時則把該數字加1,如0替換1,1替換2,9替換0; 其他字符不做變化。 3、解密方法為加密的逆過程。 ? 接口描述: ????實現接口,每個接口實現1個基本操作: void?Encrypt?(char?aucPassword[],?char?aucResult[]):在該函數中實現字符串加密并輸出 說明: 1、字符串以\0結尾。 2、字符串最長100個字符。 ? int?unEncrypt?(char?result[],?char?password[]):在該函數中實現字符串解密并輸出 說明: 1、字符串以\0結尾。 ????2、字符串最長100個字符。輸入描述:
輸入說明 輸入一串要加密的密碼 輸入一串加過密的密碼輸出描述:
輸出說明 輸出加密后的字符 輸出解密后的字符輸入例子:
abcdefg BCDEFGH輸出例子:
BCDEFGH abcdefg方法一: import java.util.Scanner; /** ?* Created by Administrator on 2015/12/23. ?*/ public class Main { ????public static void main(String[] args) { ????????Scanner sc=new Scanner(System.in); ????????while (sc.hasNext()){ ????????????String strA="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; ????????????String strB="bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA1234567890"; ????????????String str1=sc.next(); ????????????String str2=sc.next(); ????????????StringBuffer sb1=new StringBuffer(); ????????????StringBuffer sb2=new StringBuffer(); ????????????for(int i=0;i<str1.length();i++){ ????????????????sb1.append(strB.charAt(strA.indexOf(str1.charAt(i)))); ????????????} ????????????for(int i=0;i<str2.length();i++){ ????????????????sb2.append(strA.charAt(strB.indexOf(str2.charAt(i)))); ????????????} ????????????System.out.println(sb1); ????????????System.out.println(sb2); ????????} ????} } 方法二: import java.util.Scanner; /** ?* Author: 王俊超 ?* Date: 2015/12/23 13:31 ?* All Rights Reserved !!! ?*/ public class Main { ????public static void main(String[] args) { ????????Scanner scanner = new Scanner(System.in); //??????? Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt")); ????????while (scanner.hasNext()) { ????????????String input = scanner.nextLine(); ????????????System.out.println(encrypt(input)); ????????????input = scanner.nextLine(); ????????????System.out.println(unencrypt(input)); ????????} ????????scanner.close(); ????} ????/** ?????* 字符串解密 ?????* ?????* @param s ?????* @return ?????*/ ????private static String unencrypt(String s) { ????????char[][] mask = { ????????????????{'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y'}, ????????????????{'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'}, ????????????????{'9', '0', '1', '2', '3', '4', '5', '6', '7', '8'} ????????}; ????????return convert(s, mask); ????} ????/** ?????* 字符串加密 ?????* ?????* @param s ?????* @return ?????*/ ????private static String encrypt(String s) { ????????char[][] mask = { ????????????????{'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a'}, ????????????????{'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A'}, ????????????????{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'} ????????}; ????????return convert(s, mask); ????} ????/** ?????* 根據掩碼表對字符串進行轉換 ?????* ?????* @param s ?????* @param mask ?????* @return ?????*/ ????private static String convert(String s, char[][] mask) { ????????StringBuilder builder = new StringBuilder(s.length()); ????????for (int i = 0; i < s.length(); i++) { ????????????char c = s.charAt(i); ????????????if (c >= 'A' && c <= 'Z') { ????????????????builder.append(mask[0][c - 'A']); ????????????} else if (c >= 'a' && c <= 'z') { ????????????????builder.append(mask[1][c - 'a']); ????????????} else { ????????????????builder.append(mask[2][c - '0']); ????????????} ????????} ????????return builder.toString(); ????} }
轉載于:https://www.cnblogs.com/bb3q/p/5073241.html
總結
- 上一篇: 最厉害的计算机病毒厉害到什么程度?
- 下一篇: 张雨绮和周星驰关系还挺好呢?大家怎么看?