zoukankan      html  css  js  c++  java
  • 字符串加密

    一..程序设计思想

      进行字符串的加密以及解密,因为已经知道啦规律是key是3所以在进行错位操作时需读取一个字符,然后与数字3相加,再转换为char类型,就实现了错3位加密操作,同理解密则减3。在加密操作中需要注意的是字母表最后三个,必须实现循环操作,使用ASCII码,即X加密后是A,Y加密后是B,Z加密后是C,,同理解密时则是注意abc对应为xyz。

    二.程序设计流程图

    三.源程序

    import java.util.Scanner;
    public class Encryption
    {
      public static char[] encryption(String str)
        {
          char chararry[] = str.toCharArray();
          for(int i =0; i< str.length(); i++)
          {

            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)
      {
        char chararry[] = str.toCharArray();
        for(int i =0; i< str.length(); i++)
        {

          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("加密字符串请输入1");
        System.out.println("解密字符串请输入2");


        int x = imput1.nextInt();
        System.out.print("输入一个英文字符串:");
        String string = imput2.nextLine();
          if(x == 1)
          {
            System.out.print("加密后为:");
            for(int i =0;i <string.length(); i++)
            {
              System.out.print(Encryption.encryption(string)[i]);
            }
          }
            if(x == 2)
            {
              System.out.print("解密后为:");
              for(int i =0;i <string.length(); i++)
              {
                System.out.print(Encryption.deciphering(string)[i]);
              }
            }

      imput1.close();
      imput2.close();
    }

    }

    四.结果截图

  • 相关阅读:
    安装openssl后yum不能使用的解决办法
    使用xShell 连接 docker 使用说明
    /usr/bin/ld: cannot find -lcrypto
    Mac包管理神器:Home-brew
    FinalShell远程连接工具推荐
    make编译出错 usr/bin/ld: /data/app/openssl/lib/libcrypto.a(ecs_asn1.o): relocation R_X86_64_PC32 against symbol `ECDSA_SIG_it' can not be used when making a shared object; recompile with -fPIC
    交叉编译环境搭建
    安装Gitlab
    Git的详细使用
    服务器里Centos 7安装KVM,并通过KVM安装Centos 7
  • 原文地址:https://www.cnblogs.com/877612838zzx/p/7744297.html
Copyright © 2011-2022 走看看