java实现对字符串的加密与解密

加密类如下:

package com.test;
import java.io.*;

/**
 * 加密与解密
 */

public class Encipher{
	
	// 加密
    public static String EncodePasswd(String strPasswd){

        String strEncodePasswd = new String("");

        int i;

        int n;

        char code;

        String des = new String();

        String strKey = new String();

        if((strPasswd == null) || strPasswd.length() == 0){

            strEncodePasswd = "";

            return strEncodePasswd;

        }

        strKey = EncipherConst.m_strKey1 + EncipherConst.m_strKey2 + EncipherConst.m_strKey3 + EncipherConst.m_strKey4 + EncipherConst.m_strKey5 + EncipherConst.m_strKey6;

        while(strPasswd.length() < 8){

            strPasswd = strPasswd + (char)1;

        }

        des = "";

        for(n = 0; n <= strPasswd.length() - 1; n++){

            while(true){

                code = (char)Math.rint(Math.random() * 100);

                while((code > 0) && (((code ^ strPasswd.charAt(n)) < 0) || ((code ^ strPasswd.charAt(n)) > 90))){

                    code = (char)((int)code - 1);

                }

                char mid = 0;

                int flag;

                flag = code ^ strPasswd.charAt(n);

                if(flag > 93){

                    mid = 0;

                } else{

                    mid = strKey.charAt(flag); //Asc(Mid(strKey, (code Xor Asc(Mid(strPasswd, n, 1))) + 1, 1))

                }

                if((code > 35) & (code < 122) & (code != 124) & (code != 39) & (code != 44) & (mid != 124) & (mid != 39) & (mid != 44)){

                    break;

                }

                //确保生成的字符是可见字符并且在SQL语句中有效

            }

            char temp = 0;

            temp = strKey.charAt(code ^ strPasswd.charAt(n));

            des = des + (char)code + temp;

        }

        strEncodePasswd = des;

        return strEncodePasswd;

    }
    
    // 把经过加密的文件解密
    public static String DecodePasswd(String varCode){

        int n;

        String des = new String();

        String strKey = new String();

        if((varCode == null) || (varCode.length() == 0)){

            return "";

        }

        strKey = EncipherConst.m_strKey1 + EncipherConst.m_strKey2 + EncipherConst.m_strKey3 + EncipherConst.m_strKey4 + EncipherConst.m_strKey5 + EncipherConst.m_strKey6;

        if(varCode.length() % 2 == 1){

            varCode = varCode + "?";

        }

        des = "";

        for(n = 0; n <= varCode.length() / 2 - 1; n++){

            char b;

            b = varCode.charAt(n * 2);

            int a;

            a = (int)strKey.indexOf(varCode.charAt(n * 2 + 1));

            des = des + (char)((int)b ^ a);

        }

        n = des.indexOf(1);

        if(n > 0){

            return des.substring(0, n);

        } else{

            return des;

        }

    }
    
    static class EncipherConst{
        public final static String m_strKey1 = "zxcvbnm,./asdfg";

        public final static String m_strKeya = "cjk;";

        public final static String m_strKey2 = "hjkl;'qwertyuiop";

        public final static String m_strKeyb = "cai2";

        public final static String m_strKey3 = "[]\\1234567890-";

        public final static String m_strKeyc = "%^@#";

        public final static String m_strKey4 = "=` ZXCVBNM<>?:LKJ";

        public final static String m_strKeyd = "*(N";

        public final static String m_strKey5 = "HGFDSAQWERTYUI";

        public final static String m_strKeye = "%^HJ";

        public final static String m_strKey6 = "OP{}|+_)(*&^%$#@!~";

    }
    public static void  main(String[] args) throws Exception{

    	  String a = EncodePasswd("abc") ;
          System.out.println(a); // 输出加密后的字符串
          System.out.println(DecodePasswd(a));   // 把加密字符串解密后输出
      }
}

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值