package com.frank.common.utils;
import org.apache.http.util.TextUtils;
/**
* Description:恺撒移位工具
* 明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。
* 例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推X将变成A,Y变成B,Z变成C。
* 由此可见,位数就是凯撒密码加密和解密的密钥
* Auth: Frank
* Date: 2017-10-30
* Time: 上午 11:21
*/
public class KaisaCodeUtil {
private static final String NORMAL_SEQ_LOWERCASE = "abcdefghijklmnopqrstuvwxyz";//小写字母
private static final String NORMAL_SEQ_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";//大写字母
private static final String NORMAL_SEQ_NUMBER = "0123456789";//数字
/**
* 加密
* <p>
* 加密规则:
* 1,判断明文中是否含有{@link #NORMAL_SEQ_LOWERCASE},{@link #NORMAL_SEQ_UPPERCASE}和{@link #NORMAL_SEQ_NUMBER}中的任意char,
* 2,如果有,就逐步获得那些char,然后根据移动的位数分别找到{@link #NORMAL_SEQ_LOWERCASE},{@link #NORMAL_SEQ_UPPERCASE}和{@link #NORMAL_SEQ_NUMBER}中移动后对应密文的char.
* 3,合成密文
* </p>
* @param clearStr 明文
* @param moveByte 移动位数
* @return
*/
public static String encodeKaisa(String clearStr, int moveByte){
StringBuffer cipherSb = new StringBuffer();
if(TextUtils.isEmpty(clearStr)){
return "";
}
int clearStrLength = clearStr.length();
for(int i= 0;i<clearStrLength;i++){
char clearChar = clearStr.charAt(i);
boolean hasPos = false;//当前字符是否存在于NORMAL_SEQ_LOWERCASE或者NORMAL_SEQ_UPPERCASE
//处理字符存在于NORMAL_SEQ_LOWERCASE情况
int pos = NORMAL_SEQ_LOWERCASE.indexOf(clearChar);
int lastPos = pos + moveByte;
if(-1 != pos){
if(lastPos<26) {
cipherSb.append(NORMAL_SEQ_LOWERCASE.charAt(lastPos));
}else {
cipherSb.append(NORMAL_SEQ_LOWERCASE.charAt(lastPos-NORMAL_SEQ_LOWERCASE.length()));
}
hasPos = true;
}
if(hasPos){
continue;
}
//处理字符存在于NORMAL_SEQ_UPPERCASE情况
pos = NORMAL_SEQ_UPPERCASE.indexOf(clearChar);
lastPos = pos + moveByte;
if(-1 != pos){
if(lastPos<26) {
cipherSb.append(NORMAL_SEQ_UPPERCASE.charAt(lastPos));
}else {
cipherSb.append(NORMAL_SEQ_UPPERCASE.charAt(lastPos-NORMAL_SEQ_UPPERCASE.length()));
}
hasPos = true;
}
//处理字符存在于NORMAL_SEQ_NUMBER情况
pos = NORMAL_SEQ_NUMBER.indexOf(clearChar);
lastPos = pos + moveByte;
if(-1 != pos){
if(lastPos<10) {
cipherSb.append(NORMAL_SEQ_NUMBER.charAt(lastPos));
}else {
cipherSb.append(NORMAL_SEQ_NUMBER.charAt(lastPos-NORMAL_SEQ_NUMBER.length()));
}
hasPos = true;
}
if(hasPos){
continue;
}else {
cipherSb.append(clearChar);
}
}
return cipherSb.toString();
}
/**
* 解密
* @param cipherStr 密文
* @param moveByte 移动位数
* @return
*/
public static String decodeKaisa(String cipherStr, int moveByte){
StringBuffer clearSb = new StringBuffer();
if(TextUtils.isEmpty(cipherStr)){
return "";
}
int cipherStrLength = cipherStr.length();
for(int i= 0;i<cipherStrLength;i++){
char clearChar = cipherStr.charAt(i);
boolean hasPos = false;//当前字符是否存在于NORMAL_SEQ_LOWERCASE或者NORMAL_SEQ_UPPERCASE
//处理字符存在于NORMAL_SEQ_LOWERCASE情况
int pos = NORMAL_SEQ_LOWERCASE.indexOf(clearChar);
int lastPos = pos - moveByte;
if(-1 != pos){
if(lastPos >= 0) {
clearSb.append(NORMAL_SEQ_LOWERCASE.charAt(lastPos));
}else {
clearSb.append(NORMAL_SEQ_LOWERCASE.charAt(lastPos+NORMAL_SEQ_LOWERCASE.length()));
}
hasPos = true;
}
if(hasPos){
continue;
}
//处理字符存在于NORMAL_SEQ_UPPERCASE情况
pos = NORMAL_SEQ_UPPERCASE.indexOf(clearChar);
lastPos = pos - moveByte;
if(-1 != pos){
if(lastPos >= 0) {
clearSb.append(NORMAL_SEQ_UPPERCASE.charAt(lastPos));
}else {
clearSb.append(NORMAL_SEQ_UPPERCASE.charAt(lastPos+NORMAL_SEQ_UPPERCASE.length()));
}
hasPos = true;
}
//处理字符存在于NORMAL_SEQ_NUMBER情况
pos = NORMAL_SEQ_NUMBER.indexOf(clearChar);
lastPos = pos - moveByte;
if(-1 != pos){
if(lastPos >= 0) {
clearSb.append(NORMAL_SEQ_NUMBER.charAt(lastPos));
}else {
clearSb.append(NORMAL_SEQ_NUMBER.charAt(lastPos+NORMAL_SEQ_NUMBER.length()));
}
hasPos = true;
}
if(hasPos){
continue;
}else {
clearSb.append(clearChar);
}
}
return clearSb.toString();
}
public static void main(String[] args) {
String cipherStr = encodeKaisa("6783$%#!~!#$%^&*&(FRANKfrank22411", 5);
System.out.println(cipherStr);
String clearStr = decodeKaisa(cipherStr,5);
System.out.println(clearStr);
}
}
Token生成规则以及工具相关代码
5星 · 超过95%的资源 需积分: 50 124 浏览量
2018-06-26
09:45:01
上传
评论 6
收藏 11KB RAR 举报
Frankeen
- 粉丝: 37
- 资源: 26
最新资源
- 卡方数据.sav
- Python实现基于Django框架的二手物品购物网站设计源码+数据库.zip
- 基于Python和Django的二手物品购物网站设计源码+数据库脚本.zip
- 【计算机毕业设计】基于h5的移动网赚项目小程序【源码+lw+部署文档+讲解】
- 【计算机毕业设计】基于ssm-vue的oa系统设计与实现【源码+lw+部署文档+讲解】
- 基于pytorch实现的YOLOV5+SORT的车辆行人目标识别及追踪系统源码.zip
- 【计算机毕业设计】基于servlet+jdbc的在线选房系统设计与实现【源码+lw+部署文档+讲解】
- 投身科技创新,勇担时代先锋.pptx
- 【计算机毕业设计】基于SSM的仿微博系统的设计与实现【源码+lw+部署文档+讲解】
- 使用 Web Components 实现,遵循 Material You 设计规范的 Web 前端组件库
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈