base64加密和平化解密

by admin on 2019年9月3日

复制代码 代码如下:

package com.sms.utils;


import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;


public class AESUtils {
    public static String key = "123456!@#";
    public static String iv = "1234567812345678";

    /**
     * 加密
     *
     * @param data
     * @return
     */
    public static String encrypt(String data) throws Exception {

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        int blockSize = cipher.getBlockSize();

        byte[] dataBytes = data.getBytes();
        int plaintextLength = dataBytes.length;
        if (plaintextLength % blockSize != 0) {
            plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
        }

        byte[] plaintext = new byte[plaintextLength];
        System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
        byte[] encrypted = cipher.doFinal(plaintext);
        return Base64.encodeBase64String(encrypted);

    }


    /**
     * 解密
     *
     * @param data
     * @return
     */
    public static String desEncrypt(String data) throws Exception {
        //base64加密串在http中会把"+"号变为" "
        data = StringUtils.replace(data, " ", "+");
//        byte[] contentBytes = Base64.decode(data, 0);
        byte[] contentBytes = Base64.decodeBase64(data);
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

        byte[] original = cipher.doFinal(contentBytes);
        String originalString = new String(original);
        return originalString;

    }

    public static void main(String[] args) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
//      map.put("username", "admin");
//      map.put("password", "123456");
        System.out.println(AESUtils.encrypt(JSONUtil.toJson(map)));
    }

}

NSData* decodeData = [[NSData alloc]
initWithBase64EncodedString:@”MTIzNDU2″ options:0];

<script type=”text/javascript”>

NSString* decodeStr = [[NSString alloc] initWithData:decodeData
encoding:NSASCIIStringEncoding];

2.从标准输入读取文件内容,base64编码并打印到标准输出

var base64encodechars =
“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”;
var base64decodechars = new Array(
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

NSLog(@”解密:decodeStr  %@”,decodeStr);

[root@localhost test]# base64
snailwarrior
c25haWx3YXJyaW9yCg==

function base64encode(str) {
var out, i, len;
var c1, c2, c3;
len = str.length;
i = 0;
out = “”;
while (i < len) {
c1 = str.charCodeAt(i++) & 0xff;
if (i == len) {
out += base64encodechars.charAt(c1 >> 2);
out += base64encodechars.charAt((c1 & 0x3) << 4);
out += “==”;
break;
}
c2 = str.charCodeAt(i++);
if (i == len) {
out += base64encodechars.charAt(c1 >> 2);
out += base64encodechars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0)
>> 4));
out += base64encodechars.charAt((c2 & 0xf) << 2);
out += “=”;
break;
}
c3 = str.charCodeAt(i++);
out += base64encodechars.charAt(c1 >> 2);
out += base64encodechars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0)
>> 4));
out += base64encodechars.charAt(((c2 & 0xf) << 2) | ((c3 & 0xc0)
>> 6));
out += base64encodechars.charAt(c3 & 0x3f);
}
return out;
}

NSData* originData = [@”123456″
dataUsingEncoding:NSASCIIStringEncoding];

我是输入snailwarrior,回车,然后按Ctrl+D结束文件输入的。

function base64decode(str) {
var c1, c2, c3, c4;
var i, len, out;

NSString* encodeResult = [originData
base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

 

len = str.length;

NSLog(@”加密:%@”,encodeResult);

3、对字符串”snailwarrior”编码,并打印到标准输出

i = 0;
out = “”;
while (i < len) {

[root@localhost test]# echo “snailwarrior” | base64
c25haWx3YXJyaW9yCg==

do {
c1 = base64decodechars[str.charCodeAt(i++) & 0xff];
} while (i < len && c1 == -1);
if (c1 == -1)
break;

 

do {
c2 = base64decodechars[str.charCodeAt(i++) & 0xff];
} while (i < len && c2 == -1);
if (c2 == -1)
break;

4、Base64解码

out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >>
4));

[root@localhost test]# echo “snailwarrior” | base64 | base64 -d
snailwarrior
base64: invalid input

do {
c3 = str.charCodeAt(i++) & 0xff;
if (c3 == 61)
return out;
c3 = base64decodechars[c3];
} while (i < len && c3 == -1);
if (c3 == -1)
break;

 

out += String.fromCharCode(((c2 & 0xf) << 4) | ((c3 & 0x3c)
>> 2));

do {
c4 = str.charCodeAt(i++) & 0xff;
if (c4 == 61)
return out;
c4 = base64decodechars[c4];
} while (i < len && c4 == -1);
if (c4 == -1)
break;
out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
}
return out;
}
</script>

RHEL5自带的base64工具有BUG,base64解码时莫名出错:base64: invalid
input。
使用源码包编译安装:

代码如下: script type=”text/javascript”
var base64encodechars =
“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”; var
base64decodechars = new Arr…

 

wget http://www.fourmilab.ch/webtools/base64/base64-1.5.tar.gz
tar zxf base64-1.5.tar.gz
cd base64-1.5
./configure
make && make -n install

base64 -d gfwlist.txt gfwlist_base64.txt

这个东西的配置似乎不对,还是不行

 

不过可以在解压后的路径里直接处理

[root@localhost base64-1.5]# echo “snailwarrior” | ./base64 |
./base64 -d
snailwarrior

这里的base64-1.5是文件夹

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图