各种简单常见的加密与解密

主要包含C,python

python

AES、DES、RC4加解密

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
26
27
28
29
30
31
32
#coding=utf-8
from Crypto.Cipher import AES,DES,ARC4
#iv key都是8的倍数,至少16位
key = '0123456789012345'
iv = '0123456789012345'
#msg 也是8的倍数,至少16位
msg='0123456789012345'
print 'AES-----------------------------------'
print msg
obj1 = AES.new(key,AES.MODE_CBC,iv)
enc=obj1.encrypt(msg)
print enc
obj2 = AES.new(key,AES.MODE_CBC,iv)
print obj2.decrypt(enc)
print 'DES-----------------------------------'
print msg
key='01234567'
obj1=DES.new(key,DES.MODE_ECB)
enc=obj1.encrypt(msg)
print enc
obj2=DES.new(key,DES.MODE_ECB)
print obj2.decrypt(enc)
print 'ARC4-----------------------------------'
key='flag{this_is_not_the_flag_hahaha}'
msg='123456789'
ob1=ARC4.new(key)
enc=ob1.encrypt(msg)
print enc
enc="\x20\xc3\x1a\xae\x97\x3c\x7a\x41\xde\xf6\x78\x15\xcb\x4b\x4c\xdc\x26\x55\x8b\x55\xe5\xe9\x55\x75\x40\x3d\x82\x13\xa5\x60\x13\x3b\xf5\xd8\x19\x0e\x47\xcf\x5f\x5e\xde\x9d\x14\xbd"
ob2=ARC4.new(key)
print ob2.decrypt(enc)

Base解密

base64,base32,base16,(base58是python3以后才引入的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import base64
s="flag{hgsada1d3fd13d1f3df1s3dfsdf5e4f6ew8r4}"
enc=base64.b64encode(s)
print enc
enc1=base64.b32encode(enc)
print enc1
enc2=base64.b16encode(enc1)
print enc2
enc1=base64.b16decode(enc2)
print enc1
enc=base64.b32decode(enc1)
print enc
st=base64.b64decode(enc)
print st

base64解密表替换解密

1
2
3
4
5
6
7
8
9
10
11
12
import string
import base64
s=""
custom="9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678+/"
Base64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
ciphertext = "YlwgY2s8"
for ch in ciphertext:
if ch in Base64:
s = s + Base64[string.find(custom,str(ch))]
elif ch == '=':
s =s+'='
print base64.b64decode(s)

循环移位

1
2
3
4
5
6
7
8
9
#k为正,左移k位,k位负,右移k位
def demo(lst, k):
return lst[k:] + lst[:k]
def main():
k=bin(0xB54C8D9B)[2:]
k=demo(k,-0xe)
k=hex(int(k,2))
print(k)
main()

strings_to_hex

1
2
3
4
5
6
7
字符串转16进制
data=bytearray.fromhex('d4e8e1f4a0f7e1f3a0e6e1f3f4a1a0d4e8e5a0e6ece1e7a0e9f3baa0c4c4c3d4c6fbb9e1e6b3e3b9e4b3b7b7e2b6b1e4b2b6b9e2b1b1b3b3b7e6b3b3b0e3b9b3b5e6fd')
flag = ''
print data[0]
for i in data:
flag = flag + chr(i & 0x7f)
print flag

各种进制转换libnum包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import libnum
s="flag{ESE}"
print libnum.s2n(s)
n=0x666c61677b706361747d
print libnum.n2s(n)
#这个转换不用在意十六进制的位数是否为偶数
b='01110000011000110110000101110100'
print libnum.b2s(b)
s='ESE'
print libnum.s2b(s)
#生成1024位在质数
print libnum.generate_prime(1024)
#目前libnum暂时只支持python2,若是用python3安装会因为没有reduce、xrange而报错。

替换分割

1
s=open('5ae30bffc8f7d.txt','r').read().replace("\n"," ").split()

排序方式

8种排序,插入排序、希尔排序、冒泡排序、快速排序、直接选择排序、堆排序、归并排序、基数排序。

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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#直接爆破
import string
def func1(n):
r = n
for i in range(0,7):
if i&1:
r ^= 1<< i
else:
r |= 1<< i
return r
def func2(n):
r = 1
for i in range(7):
r = (n*r)%481
return r
enc=[0x19C,0x169,0x30,0x1D6,0x30,0x30,0x199,0x6A,0x157,0xc2,
0x10A,0x155,0x150,0x107,0x37,0x12E,0x22,0x0F1,0x1AE,
0x151,0x0F1,0x1A,0x1A5,0x1AE,0x0C9,0x12C,0x1,0x166,0x12c,
0x0CB,0x30,0x107,0x166,0x1B4,0x1AE,0x14C,0x46]
out =''
for i in range(len(enc)):
for c in string.printable:
s = ord(c) ^ func1(i)
s = func2(s)
if s == enc[i]:
out+=c
break
print out
#插入排序,时间复杂度为O(n^2)。是稳定的排序方法。
def insert_sort(lst):
count=len(lst)
for i in range(1,count):
key=lst[i]
j=i-1
while j>=0:
if lst[j]>key:
lst[j+1]=lst[j]
lst[j]=key
j-=1
return lst
# 希尔排序是插入排序的一种希尔排序是非稳定排序算法
def shell_sort(lst):
    count = len(lst)
    step = 2
    group = count / step
    while group > 0:
        for i in range(0, group):
            j = i + group
            while j < count:
                k = j - group
                key = lst[j]
                while k >= 0:
                    if lst[k] > key:
                        lst[k + group] = lst[k]
                        lst[k] = key
                    k -= group
                j += group
        group /= step
    return lst
# 冒泡排序
def bubble_sort(lst):
    count = len(lst)
    for i in range(0, count):
        for j in range(i + 1, count):
            if lst[i] > lst[j]:
                lst[i], lst[j] = lst[j], lst[i]
    return lst
# 选择排序
def select_sort(lst):
    count = len(lst)
    for i in range(0, count):
        min = i
        for j in range(i + 1, count):
            if lst[min] > lst[j]:
                min = j
        lst[min], lst[i] = lst[i], lst[min]
    return lst
# 快速排序,整个排序过程可以递归进行
def quick_sort(lst, left, right):
    if left >= right:
        return lst
    key = lst[left]
    low = left
    high = right
    while left < right:
        while left < right and lst[right] >= key:
            right -= 1
        lst[left] = lst[right]
        while left < right and lst[left] <= key:
            left += 1
        lst[right] = lst[left]
    lst[right] = key
    quick_sort(lst, low, left - 1)
    quick_sort(lst, left + 1, high)
    return lst
#堆排序是指利用堆积树数据结构,它是选择排序的一种,堆分为大根堆和小根堆,是完全二叉树,大根堆的要求是每个节点的值都不大于其父节点的值
def adjust_heap(lst, i, size):
    lchild = 2 * i + 1
    rchild = 2 * i + 2
    max = i
    if i < size / 2:
        if lchild < size and lst[lchild] > lst[max]:
            max = lchild
        if rchild < size and lst[rchild] > lst[max]:
            max = rchild
        if max != i:
            lst[max], lst[i] = lst[i], lst[max]
            adjust_heap(lst, max, size)
 
def build_heap(lst, size):
    for i in range(0, (size/2))[::-1]:
        adjust_heap(lst, i, size)
 
def heap_sort(lst):
    size = len(lst)
    build_heap(lst, size)
    for i in range(0, size)[::-1]:
        lst[0], lst[i] = lst[i], lst[0]
        adjust_heap(lst, 0, i)
#归并排序
def merge(left, right):
    i, j = 0, 0
    result = []
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
    result += left[i:]
    result += right[j:]
    return result
 
def merge_sort(lst):
    # 归并排序
    if len(lst) <= 1:
        return lst
    num = len(lst) / 2
    left = merge_sort(lst[:num])
    right = merge_sort(lst[num:])
#基数排序
import math
def radix_sort(lst, radix=10):
    k = int(math.ceil(math.log(max(lst), radix)))
    bucket = [[] for i in range(radix)]
    for i in range(1, k+1):
        for j in lst:
            bucket[j/(radix**(i-1)) % (radix**i)].append(j)
        del lst[:]
        for z in bucket:
            lst += z
            del z[:]
    return lst

C

读取文件

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include<time.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
void main( void )
{
FILE *stream;
char list[20000];
int start,end,fd,size;
int i, numread, numwritten;
for ( i = 0; i < 20000; i++ )
list[i] = (char)('z');
start=clock();
if( (stream = fopen( "fread.out", "w+t" )) != NULL )
{
numwritten = fwrite( list, sizeof( char ), 20000, stream );
fclose( stream );
}
else
{
printf( "Problem opening the file\n" );
}
if( (stream = fopen( "fread.out", "r+t" )) != NULL )
{
numread = fread( list, sizeof( char ), 20000, stream );
fclose( stream );
}
else
{
printf( "File could not be opened\n" );
}
end=clock();
printf("fread all time:%d\n",end-start);
start=clock();
fd = open("read.out", O_WRONLY|O_CREAT);
write(fd, list, sizeof(list));
close(fd);
fd = open("read.out", O_RDONLY);
size = read(fd, list, sizeof(list));
close(fd);
end=clock();
printf("read all time:%d\n",end-start);
}

base64

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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdio.h>
#include <malloc.h>
#include <string.h>
char *base64_encode(const char *src);
char *base64_decode(const char *src);
char *table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char *base64_encode(const char *src)
{
int count;
char *dst;
long tmp, buf;
char in[4];
int i, j;
count = strlen(src) / 3 + (strlen(src) % 3 ? 1 : 0);
dst = (char*)malloc((count * 4 + 1));
memset(dst, 0, count * 4 + 1);
for(j = 0; j < count; j++)
{
memset(in, 0, sizeof(in));
strncpy(in, src + j * 3, 3);
buf = 0;
for(i = 0; i < strlen(in); i++)
{
tmp = (long)in[i];
tmp <<= (16 - i * 8);
buf |= tmp;
}
for(i = 0; i < 4; i++)
{
if(strlen(in) + 1 > i)
{
tmp = buf >> (18 - 6 * i);
tmp &= 0x3f;
dst[j * 4 + i] = table[tmp];
}
else
{
dst[j * 4 + i] = '=';
}
}
}
return dst;
}
char *base64_decode(const char *src)
{
int count, len;
char *dst;
long tmp, buf;
int i, j, k;
char in[5];
len = strlen(src);
count = len / 4;
dst = (char *)malloc(count * 3 + 1);
memset(dst, 0, count * 3 + 1);
for(j = 0; j < count; j++)
{
memset(in, 0, sizeof(in));
strncpy(in, src + j * 4, 4);
buf = 0;
for(i = 0; i < 4; i++)
{
tmp = (long)in[i];
if(tmp == '=')
{
tmp = 0;
}
else
{
for(k = 0; ; k++)
{
if(table[k] == tmp)
break;
}
tmp = k;
}
tmp <<= (18 - i * 6);
buf |= tmp;
}
for(i = 0; i < 3; i++)
{
tmp = buf >> (16 - i * 8);
tmp &= 0xff;
dst[j * 3 + i] = tmp;
}
}
return dst;
}
int main()
{
char *src, *dst;
src = "flag{y0u_knoW_Ba4e64}";
dst = base64_encode(src);
printf("%s\n", dst);
printf("%s\n", base64_decode(src));
free(src);
free(dst);
return 0;
}

rc4

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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<stdio.h>
#include<string.h>
typedef unsigned longULONG;
/*初始化函数*/
void rc4_init(unsigned char*s, unsigned char*key, unsigned long Len)
{
int i = 0, j = 0;
char k[256] = { 0 };
unsigned char tmp = 0;
for (i = 0; i<256; i++)
{
s[i] = i;
k[i] = key[i%Len];
}
for (i = 0; i<256; i++)
{
j = (j + s[i] + k[i]) % 256;
tmp = s[i];
s[i] = s[j];//交换s[i]和s[j]
s[j] = tmp;
}
}
/*加解密*/
void rc4_crypt(unsigned char*s, unsigned char*Data, unsigned long Len)
{
int i = 0, j = 0, t = 0;
unsigned long k = 0;
unsigned char tmp;
for (k = 0; k<Len; k++)
{
i = (i + 1) % 256;
j = (j + s[i]) % 256;
tmp = s[i];
s[i] = s[j];//交换s[x]和s[y]
s[j] = tmp;
t = (s[i] + s[j]) % 256;
Data[k] ^= s[t];
}
}
int main()
{
unsigned char s[256] = { 0 }, s2[256] = { 0 };//S-box
char key[256] = { "flag{this_is_not_the_flag_hahaha}" };
char pData[50] ;
unsigned long len = strlen(pData);
int i;
printf("input data:\n");
scanf("%50s",pData);
rc4_init(s, (unsigned char*)key, strlen(key));//已经完成了初始化
for (i = 0; i<256; i++)//用s2[i]暂时保留经过初始化的s[i],很重要的!!!
{
s2[i] = s[i];
}
rc4_crypt(s, (unsigned char*)pData, len);//加密
printf("加密\n");
for(i=0;i<44;i++)
{
printf("\\x%x",pData[i]&0xff);
}
printf("\n已经加密,现在解密:\n\n");
//rc4_init(s,(unsignedchar*)key,strlen(key));//初始化密钥
rc4_crypt(s2, (unsigned char*)pData, len);//解密
printf("\n%s\n", pData);
return 0;
}

rc5

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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int w=16;//字长
int r=12;//12;//轮数12
int b=16;//密钥长度
int t=26;//2*r+2=12*2+2=26
int c=8; //b*8/w = 16*8/16
typedef unsigned long int FOURBYTEINT;//四字节
typedef unsigned short int TWOBYTEINT;//2字节
typedef unsigned char BYTE;
void InitialKey(unsigned char* KeyK,int b);
void generateChildKey(unsigned char* KeyK,TWOBYTEINT* ChildKeyS);
void Encipher(TWOBYTEINT* In,TWOBYTEINT* Out,TWOBYTEINT* S);
void Decipher(TWOBYTEINT* In,TWOBYTEINT* Out,TWOBYTEINT* S);
#define NoOfData 4
/**2、循环移位函数
由于在生成子密钥,加密,解密过程中都要进行循环移位,故要首先定义循环以为函数。
* 循环左移和右移函数
* x : 被循环的数
* y : 将要循环的位数
*/
#define ROTL(x,y) (((x)<<(y&(w-1))) | ((x)>>(w-(y&(w-1)))))
#define ROTR(x,y) (((x)>>(y&(w-1))) | ((x)<<(w-(y&(w-1)))))
/**3、 初始密钥产生函数
生成一个初始的长度为b字节的密钥。
产生初始密钥的函数
*/
void InitialKey(unsigned char* KeyK,int b)
{
int i,j;
for( i=0;i<b;i++)//初始化
{
KeyK[i]=0;
}
int intiSeed=3;
KeyK[0]=intiSeed;
for(j=1;j<b;j++)//生成
{
KeyK[j] = (BYTE) ( ((int)(pow(3,j))%(255-j)));
//KeyK[j] = (BYTE) ( ((int)(pow(double(3),j))%(255-j)));
}
}
/**4、 密钥扩展函数
由于需加密r轮,每轮需要两个子密钥,所以需要密钥扩展函数根据初始密钥来扩展出2r+2个子密钥。
产生子密钥的函数
*/
void generateChildKey(unsigned char* KeyK,TWOBYTEINT* ChildKeyS)
{
//const double e = 2.718281828459;
//const double Phia = 1.618033988749;
int PW = 47073;//0xb7e1;
int QW = 40503;//0x9e37;//genggai
int i;
TWOBYTEINT L[c];
//初始化数组S
ChildKeyS[0]=PW;
for (i=1;i<t;i++)
{
ChildKeyS[i]=(ChildKeyS[i-1]+ QW);
}
//将K数组转换为L数组
for(i=0;i<c;i++)//初始化L数组c=8
{
L[i]=0;
}
int u = w/8;
for (i=b-1;i!=-1; i--)//转换,数组L每一元素长为32bit,数组K每一元素长为8bit
{
L[i/u] = (L[i/u]<<8)+KeyK[i];
}
for (i=0;i<c;i++)//16进制输出gaidong
{
printf("%.4X ",L[i]);
}
printf("\n");
//产生子密钥,存储在ChildKeyS中
TWOBYTEINT A,B,X,Y;
A=B=X=Y=0;
for(i=0;i<3*t;i++)
{
X = ChildKeyS[A] = ROTL(ChildKeyS[A]+X+Y,3);
A = (A+1) % t;
Y = L[B] = ROTL(L[B]+X+Y,(X+Y));
B = (B+1) % c;
}
for (i=0;i<t;i++)//16进制输出
{
printf("%.4X ",ChildKeyS[i]);
}
printf("\n");
}
/**5、 加密函数
加密函数
*/
void Encipher(TWOBYTEINT * In,TWOBYTEINT * Out,TWOBYTEINT* S)
{
TWOBYTEINT X,Y; //定义两个32位存储器
int i,j;
for(j=0;j<NoOfData;j+=2)
{
X = In[j]+S[0];
Y = In[j+1]+S[1];
for( i=1;i<=r;i++)
{
X=ROTL((X^Y),Y) + S[2*i]; //异或,循环移位,相加
Y=ROTL((Y^X),X) + S[2*i+1];
}
Out[j]=X;
Out[j+1]=Y; //密文
}
}
/**6、 解密函数
解密函数
*/
void Decipher(TWOBYTEINT* In,TWOBYTEINT* Out,TWOBYTEINT* S)
{
int i=0,j;
TWOBYTEINT X,Y;
for(j=0;j<NoOfData;j+=2)
{
X = In[j];
Y = In[j+1];
for(i=r;i>0;i--)
{
// Y = ROTR(Y-S[2*i+1],X)^X; //相减,循环移位,异或
// X = ROTR(X-S[2*i],Y)^Y;
// Y = ROTR(Y-S[2*i+1],X)^X; //相减,循环移位,异或
// X = ROTR(X-S[2*i],Y)^Y;
// 改为(对数据进行强制转换):
Y = ROTR((unsigned short int)(Y-S[2*i+1]),X)^X;
X = ROTR((unsigned short int)(X-S[2*i]),Y)^Y;
}
Out[j]=X - S[0];
Out[j+1]=Y - S[1]; //明文
}
}
/**7、 主函数测试
主函数
*/
int main(void)
{
TWOBYTEINT ChildKeyS[2*r+2]; //64bit
TWOBYTEINT ChildKey1[26]={0x9b9a};
//{0xe25b,0x4338,0x36ab,0xd59f,0x9b9a,0xc0f1,0xdc4f,
// 0xc0d2,0xf03a,0xff5a,0x771f,0x5952,0xb797,0x28ad,
//0x5c9a,0xfd9a,0xbd4b,0x3b12,0xd198,0x17f8,0x7f19,
//0x458e,0x1629,0xaa8a,0xb609,0x9b3c};//{123,434,1,123,1,34,123,56,123,8};
BYTE KeyK[b];//8bit=byte
InitialKey(KeyK,b); //生成初始密钥
int k;
generateChildKey(KeyK,ChildKeyS); //根据初始密钥生成子密钥
// TWOBYTEINT Source[]={'1','2','1','1'};//测试明文
char arr[] = "what can i do for you?";
unsigned long ll = strlen(arr);
int m;
for (m=0; NoOfData * m < ll; m++) {
TWOBYTEINT Source[4] = {arr[4 * m], arr[4 * m + 1], arr[4 * m + 2], arr[4 * m + 3]};
printf("加密以前的明文:");
for (k=0;k<NoOfData;k++)
{
printf("%.4X ",Source[k]); //16进制输出
printf("%c ",Source[k]); //16进制输出
}
printf("\n");
TWOBYTEINT Dest[NoOfData]; //用来存储密文
for(k=0;k<26;k++)
{
ChildKey1[k]=ChildKeyS[k];//如果此处自定义简单的数值为加密密钥,则可以解密出密文
printf("%.4X ",ChildKey1[k]);
}
Encipher(Source,Dest,ChildKey1); //加密
printf("\n");
printf("加密以后的密文:");
for (k=0;k<NoOfData;k++)
{
printf("%.4X ",Dest[k]);
printf("%c ",Dest[k]);
}
printf("\n");
TWOBYTEINT Data[NoOfData]={0}; //用来存储解密后的密文
Decipher(Dest,Data,ChildKey1); //解密
printf("解密以后的明文:");
for (k=0;k<NoOfData;k++)
{
printf("%.4X ",Data[k]);
printf("%c ",Data[k]);
}
printf("\n\n\n\n");
}
//printf("sizeof unsigned short int: %d",sizeof(unsigned short int));
system("pause\n");
}

java

re4

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
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
52
53
54
55
56
57
58
public static String HloveyRC4(String aInput,String aKey)
{
int[] iS = new int[256];
byte[] iK = new byte[256];
for (int i=0;i<256;i++)
iS[i]=i;
int j = 1;
for (short i= 0;i<256;i++)
{
iK[i]=(byte)aKey.charAt((i % aKey.length()));
}
j=0;
for (int i=0;i<255;i++)
{
j=(j+iS[i]+iK[i]) % 256;
int temp = iS[i];
iS[i]=iS[j];
iS[j]=temp;
}
int i=0;
j=0;
char[] iInputChar = aInput.toCharArray();
char[] iOutputChar = new char[iInputChar.length];
for(short x = 0;x<iInputChar.length;x++)
{
i = (i+1) % 256;
j = (j+iS[i]) % 256;
int temp = iS[i];
iS[i]=iS[j];
iS[j]=temp;
int t = (iS[i]+(iS[j] % 256)) % 256;
int iY = iS[t];
char iCY = (char)iY;
iOutputChar[x] =(char)( iInputChar[x] ^ iCY) ;
}
return new String(iOutputChar);
}
public static void main(String[] args) {
String inputStr = "7894dsfdsdfsd";
String key = "abcdefg";
String str = HloveyRC4(inputStr,key);
//打印加密后的字符串
System.out.println(str);
//打印解密后的字符串
System.out.println(HloveyRC4(str,key));
}

byte和Strings互转

1
2
3
4
5
6
7
8
9
String str = "hello";
byte[] srtbyte = null;
try {
srtbyte = str.getBytes("UTF-8");
String res = new String(srtbyte,"UTF-8");
System.out.println(res);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Donate
-------------本文结束感谢您的阅读-------------