Java加密解密--不需要密钥

import java.io.IOException;

public class Encryption
{
private static byte escapeSpecialTok(byte b)
{
byte map[][]={{0x21, 0x61}, {0x22, 0x62}, {0x26, 0x65}, {0x27, 0x66},
{0x3c, 0x7a}, {0x3e, 0x7b}, {0x3f, 0x7c}, {0x00,0x00}};

for(int i=0; i<map.length; i++) {
if(b==map[i][0])
return map[i][1];
if(b==map[i][1])
return map[i][0];
}
return b;
}

private static byte[] encryptTok(byte[] raw)
{
byte[] cooked=new byte[3];
byte[] byteTemp= new byte[4];

// 0xfc: 11111100, /4: >> 2
byteTemp[0]=(byte)(((int)raw[0] & 0xfc) >> 2 );

// 0x03: 00000011, *8: << 3
byteTemp[1]=(byte)(((int)raw[0]&0x03)<<3);

// 0xe0: 11100000, /32: >> 5
byteTemp[2]=(byte)(((int)raw[1]&0xe0)>>5);

// 0x1f: 00011111
byteTemp[3]=(byte)((int)raw[1] & 0x1f);

cooked[0]=(byte)((int)byteTemp[0]+0x21);
cooked[1]=(byte)((int)(byteTemp[1]|byteTemp[2])+0x21);
cooked[2]=(byte)((int)byteTemp[3]+0x21);

cooked[0]=escapeSpecialTok(cooked[0]);
cooked[1]=escapeSpecialTok(cooked[1]);
cooked[2]=escapeSpecialTok(cooked[2]);

return cooked;
}


public static String encrypt(String strUnicode) throws IOException
{
byte[] bytes, tok;
String strEncrypted;

bytes=strUnicode.getBytes();
tok=new byte[2];
strEncrypted=new String();

for(int i=0; i<bytes.length; i+=2) {
tok[0]=bytes[i];

if(i==bytes.length-1)
tok[1]=0x00;
else
tok[1]=bytes[i+1];

strEncrypted+=new String(encryptTok(tok));
}

return strEncrypted;

}
}


//解密
public class Decryption
{
private static byte escapeSpecialTok(byte b)
{
byte map[][]={{0x21, 0x61}, {0x22, 0x62}, {0x26, 0x65}, {0x27, 0x66},
{0x3c, 0x7a}, {0x3e, 0x7b}, {0x3f, 0x7c}, {0x00, 0x00}};

for(int i=0; i<map.length; i++) {
if(b==map[i][0])
return map[i][1];
if(b==map[i][1])
return map[i][0];
}
return b;
}

private static byte[] decryptTok(byte[] raw)
{

byte[] cooked=new byte[2];
byte[] byteTemp= new byte[4];

raw[0]=escapeSpecialTok(raw[0]);
raw[1]=escapeSpecialTok(raw[1]);
raw[2]=escapeSpecialTok(raw[2]);

// 0x3f : 00111111, << 2
byteTemp[0]=(byte)((((int)raw[0] - 0x21) & 0x3f) << 2);

// 0x18 : 00011000, >> 3
byteTemp[1]=(byte)((((int)raw[1] - 0x21) & 0x18) >> 3);

// 0x07 : 00000111, << 5
byteTemp[2]=(byte)((((int)raw[1] - 0x21) & 0x07) << 5);

// 0x1f: 00011111
byteTemp[3]=(byte)(((int)raw[2] - 0x21) & 0x1f);

cooked[0]=(byte)(byteTemp[0]| byteTemp[1]);
cooked[1]=(byte)(byteTemp[2]| byteTemp[3]);

return cooked;
}

public static String decrypt(String strCode) throws IOException
{
byte[] bytes, tok, decStr, tempStr;
String strDecrypted;

bytes = strCode.getBytes();
tok = new byte[3];
tempStr = new byte[3];
decStr = new byte[bytes.length];

strDecrypted = new String();
int j;
j =0;
for(int i=0; i<bytes.length; i+=3)
{
tok[0]=bytes[i];

if(i==bytes.length-2)
{
tok[1]=bytes[i+1];
tok[2]=0x00;
}
else if(i==bytes.length-1)
{
tok[1]=0x00;
tok[2]=0x00;
}
else
{
tok[1]=bytes[i+1];
tok[2]=bytes[i+2];
}
tempStr = decryptTok(tok);
decStr[j]= tempStr[0];
j++;
decStr[j]= tempStr[1];
j++;
}
strDecrypted = new String(decStr);
return strDecrypted.trim();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值