日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

字符串加解密

發布時間:2023/12/1 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字符串加解密 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目描述

題目描述 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

總結

以上是生活随笔為你收集整理的字符串加解密的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。