首页 技术 正文
技术 2022年11月18日
0 收藏 968 点赞 4,124 浏览 1915 个字

RC4相对是速度快、安全性高的加密算法。在实际应用中,我们可以对安全系数要求高的文本进行多重加密,这样破解就有一定困难了。如下测试给出了先用RC4加密,然后再次用BASE64编码,这样双重锁定,保证数据信息安全(个人见解,不周之处请谅解!)。

package com.bao.tools.encryption;

import java.io.IOException;
import java.util.Scanner;

import org.junit.Test;

/**
 * @title RC4加密解密算法
 * @author Administrator
 * @date 2015-7-16
 */
public class CRC4 {

/*
     * 加密解密算法
     */
    public static String setEncrypted(String value, String key) {
        //声明256位数组
        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) key.charAt((i % key.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 = value.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);
    }

/*
     * 测试
     */
    @Test
    public void test() throws IOException {

Scanner scanner = new Scanner(System.in);
        
        System.out.println(“————使用RC4和BASE64多重加密算法———“);
        System.out.println(“请输入RC4使用的加密值 (value):”);
        String inputStr = scanner.nextLine();
        System.out.println(“请输入RC4加密键(key):”);
        String key = scanner.nextLine();
        String str = setEncrypted(inputStr, key);//使用RC4加密
        str = CBase64.setEncrypted(str);//使用base64编码(CBase64加密类请看 <Java中使用BASE64加密&解密>)

// 打印加密后的字符串
        System.out.println(“\n———————\n加密后字符:”+ str);

str = CBase64.getEncrypted(str);//首先使用CBase64解码
        // 打印解密后的字符串
        System.out.println(“解密后字符 :”+ setEncrypted(str, key));

   scanner.close();
    }
}

运行结果如下

RC4加密解密算法

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848