java摩斯加解密小工具

import cn.hutool.core.util.RandomUtil;

import java.util.Arrays;
import java.util.List;

public class MorseTest {
    public static void main(String[] args) {
        String sourceStr="一二三四五六七八九十工具佛IP烘干机偶数人hi噢评估师4u590235iogrhjoieagh规划日俄爱何辜iopap";
        String morse=convertStringToMorse(sourceStr);
        System.out.println(morse);
        String morseToStrResult=convertMorseToString(morse);
        System.out.println(morseToStrResult);
        System.out.println(sourceStr.equalsIgnoreCase(morseToStrResult));
    }

    //摩斯单个字的分隔
    private static final char morseSplit='/';
    //摩斯内单个字符的分隔
    private static final String morseCharSplit=".";
    //-表示2,~表示1
    private static final char[] morseCharIndex=new char[]{'-','~'};
    //随机字符串
    private static final List randomStrs= Arrays.asList("#","*","@","`","$");
    //是否添加随机字符串混淆
    private static final boolean addRandomStr=false;

    public static String convertStringToMorse(String str){
        StringBuilder result=new StringBuilder();
        //1.获取字符串对应的字符数组
        char[] chars=str.toCharArray();
        //2.将字符转换为数字
        for(char c:chars){
            result.append(morseSplit);
            //3.将数字转换为摩斯字符串
            int n=(int) c;
            String s=convertIntToMorseStr(n);
            result.append(s);
        }
        return result.substring(1);
    }

    public static String convertIntToMorseStr(int num){
        StringBuilder result=new StringBuilder();
        String numStr=String.valueOf(num);
        char[] charArray=numStr.toCharArray();
        for(char c:charArray){
            result.append(morseCharSplit);
            //每一位的数值
            int n=Integer.parseInt(String.valueOf(c));
            //摩斯码1的数量
            int morse1Num=n/morseCharIndex.length;
            //摩斯码2的数量
            int morse2Num=n%morseCharIndex.length;
            for(int i=0;i

你可能感兴趣的:(java,加密)