使用java语言实现移位密码加密过程

本文介绍了一个简单的字符串加密算法实现,通过偏移字符的ASCII值来达到加密的效果。用户可以输入任意字符串和偏移值,程序将输出加密后的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.Scanner;

public class Cipher
{
    public static void main(String [] args)
   {
      Scanner kb=new Scanner(System.in);
      System.out.println("Please enter a String: ");
      String str=kb.nextLine();
   
      System.out.println("Please enter the offset value: ");
      int offset=kb.nextInt();
      
      Cipher c = new Cipher();  //new个类Cipher的实例
      System.out.println(c.encrypt(str, offset));  //执行encrypt()方法,并输出
      
   }
    public String encrypt(String str,int offset)
   { 
     String result = str;  //这里再定义一个返回结果的字符串
      for(int i=0;i<str.length();i++)
      {
         int a=(int)str.charAt(i)+offset;
         if(a>126)
         {
          result = str.replace(str.charAt(i),(char)(a-95));
         }
         else
          result = str.replace(str.charAt(i),(char)(a));
      }
      //return str;  //永远返回原来的字符串
      return result;  
    
   }

} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值