• 【JavaEE---作业】对外提供密码学算法服务器软件系统


    大概划分

    在这里插入图片描述在这里插入图片描述

    JavaEE密码学算法对外01

    kk+2022-06-08+21-21-07

    1、domain 实体类

    Account

    package person.hyy.domain;
    
    public class Account {
       
        Integer id;
        String username;
        String password;
    
        public Account() {
       
        }
    
        public Account(Integer id, String username, String password) {
       
            this.id = id;
            this.username = username;
            this.password = password;
        }
    
        public Integer getId() {
       
            return id;
        }
    
        public void setId(Integer id) {
       
            this.id = id;
        }
    
        public String getUsername() {
       
            return username;
        }
    
        public void setUsername(String username) {
       
            this.username = username;
        }
    
        public String getPassword() {
       
            return password;
        }
    
        public void setPassword(String password) {
       
            this.password = password;
        }
    }
    
    

    2、dao 数据库持久层

    AccountDao

    package person.hyy.dao;
    
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    import org.springframework.stereotype.Repository;
    import person.hyy.domain.Account;
    
    import java.util.List;
    
    @Mapper
    @Repository
    public interface AccountDao {
       
        /*登录*/
        @Select("select * from account where username=#{username} and password=#{password}")
        List<Account> findByUsernamePassword(Account account);
    
        /*注册之前检验是否用户名重复*/
        @Select("SELECT username FROM account WHERE account.username=#{username}")
        String check(Account account);
    
        /*注册*/
        @Insert("INSERT into account(username,password) VALUES (#{username},#{password})")
        int addAccount(Account account);
    
    }
    
    

    3、service层 业务模块逻辑应用

    以下都是接口
    AccountService

    package person.hyy.service;
    
    public interface AccountService {
       
        /*登录*/
        public boolean validateAccount(String username,String password);
        /*注册*/
        public boolean addAccount(String username,String password);
        /*检查用户名是否重复*/
        public boolean checkAccount(String username);
    }
    
    

    CaesarService

    package person.hyy.service;
    
    public interface CaesarService {
       
        //加密
        public String encryption(String clearText, String key);
    
        //解密
        public String decode(String unClearText,String key);
    }
    
    

    DESService

    package person.hyy.service;
    
    public interface DESService {
       
        //加密
        public String encryption(String clearText, String userKey);
    
        //解密
        /*public String decode(String unClearText,String userKey);*/
    }
    
    

    FenceService

    package person.hyy.service;
    
    public interface FenceService {
       
        //加密
        public String encode(String clearText);
    
    
        //解密
        public String decode(String unclearText);
    }
    
    

    实际方法
    AccountServiceImpl

    package person.hyy.service.impl;
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import person.hyy.dao.AccountDao;
    import person.hyy.domain.Account;
    import person.hyy.service.AccountService;
    
    import java.util.List;
    
    @Service("accountService")
    public class AccountServiceImpl implements AccountService {
       
        @Autowired
        AccountDao accountDao;
    
        /*检验是否该账户,若是,则登录成功*/
        public boolean validateAccount(String username,String password){
       
            Account account = new Account();
            account.setUsername(username);
            account.setPassword(password);
            List<Account> accountList = accountDao.findByUsernamePassword(account);
            if(accountList.isEmpty()){
       
                return false;
            }else {
       
                return true;
            }
        }
    
        /*注册*/
        public boolean addAccount(String username,String password){
       
            Account account = new Account();
            account.setUsername(username);
            account.setPassword(password);
            int i = accountDao.addAccount(account);
            if(i!=0){
       
                return true;
            }else {
       
                return false;
            }
    
        }
    
        /*注册时检查用户名是否重复*/
        @Override
        public boolean checkAccount(String username) {
       
            Account account = new Account();
            account.setUsername(username);
            String check = accountDao.check(account);
            if(check == null){
       
                return true; //为空,即没有重复名
            }else {
       
                return false; //为真,即有重复名
            }
    
        }
    
    
    }
    
    

    CaesarServiceImpl

    package person.hyy.service.impl;
    
    import org.springframework.stereotype.Service;
    import person.hyy.service.CaesarService;
    
    @Service("caesarService")
    public class CaesarServiceImpl implements CaesarService {
       
    
        //加密(输入  明文+密钥)
        @Override
        public String encryption(String clearText, String key) {
       
            int j =Integer.valueOf(key).intValue();//获得偏移量(密钥)
            //加密字符串
            StringBuffer sb = new StringBuffer(" ");
            char b[];
            char ch=' ';
            b=clearText.toCharArray();
            for(int i=0;i<clearText.length();i++){
       
                if(b[i]>='a' && b[i]<='z'){
       
                    ch=(char)((b[i]-'a'+j)%26+'a');
                }
                if(b[i]>='A' && b[i]<='Z'){
       
                    ch=(char)((b[i]-'A'+j)%26+'A');
                }
                sb.append(ch);
            }
            return String.valueOf(sb);
        }
    
        //解密(输入  密文+密钥)
        @Override
        public String decode(String unClearText, String key) {
       
            int j =Integer.valueOf(key).intValue();//获得偏移量
            //加密字符串
            StringBuffer sb = new StringBuffer("");
            char b[];
            char ch=' ';
            b=unClearText.toCharArray();
            for(int i=0;i<unClearText.length();i++){
       
                if(b[i]>='a' && b[i]<='z'){
       
                    ch=(char)((b[i]-'a'+26-j)%26+'a');
                }
                if(b[i]>='A' && b[i]<='Z'){
       
                    ch=(char)((b[i]-'A'+26-j)%26+'A');
                }
                sb.append(ch);
            }
            return String.valueOf(sb);
        }
    }
    
    

    DESServiceImpl

    package person.hyy.service.impl;
    
    import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
    import jdk.nashorn.internal.ir.debug.ClassHistogramElement;
    import org.springframework.stereotype.Service;
    import person.hyy.service.DESService;
    
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    
    @Service("DESService")
    public class DESServiceImpl implements DESService {
       
    
        /*DES加密*/
        @Override
        public String encryption(String clearText, String userKey) {
       
            //根据给定的字节数组构建一个密钥
            String plainText = clearText; //明文
            String originKey = userKey;  //原始密钥
    
            SecretKeySpec key = new SecretKeySpec(originKey.getBytes(),"DES");
    
                //1.获取加密算法工具类
            Cipher cipher = null;
            try {
       
                cipher = Cipher.getInstance("DES");
            } catch (NoSuchAlgorithmException e) {
       
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
       
                e.printStackTrace();
            }
            //2.对工具类对象进行初始化
                //mode 加密/解密模式
                //key  对原始密钥处理之后的密钥
            try {
       
                cipher.init(Cipher.ENCRYPT_MODE,key);
            } catch (InvalidKeyException e) {
       
                e.printStackTrace();
            }
            //3.用加密工具类对象 对明文进行加密
            byte[] encipherByte = new byte[0];
            try {
       
                encipherByte = cipher.doFinal(plainText.getBytes());
            } catch (IllegalBlockSizeException e) {
       
                e.printStackTrace();
            } catch (BadPaddingException e) {
       
                e.printStackTrace();
            }
            //4.防止乱码,使用Base64编码
                String encode = Base64.encode(encipherByte);
    
            return encode;
        }
    
        /*DES解密*/
      /*  @Override
        public String decode(String unClearText,String userKey) {
            String originKey = userKey;  //原始密钥
            String encode = unClearText; //密文
    
            //获取加密算法工具类
            Cipher cipher = null;
           
  • 相关阅读:
    无线能量传输(WPT)中收发线圈的等效电路模型
    java反射机制
    Python基础
    JVM学习五
    小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现
    K线形态识别_大阳线
    动态规划--完全背包问题详解2
    Docker创建Reids容器
    动态资源如何生成
    Docker常用命令
  • 原文地址:https://blog.csdn.net/ketemstion/article/details/126957061