RC4 Encryption and decryption algorithm

Source: Internet
Author: User
Tags base64

RC4 is relatively fast, high security encryption algorithm. In the practical application, we can make multiple encryption for the text with high safety factor requirement, so the crack has some difficulties. The following test gives the first use of RC4 encryption, and then again with the BASE64 code, so that double locking, to ensure the security of data information (personal opinion, please understand the shortcomings!) )。

Package com.bao.tools.encryption;

Import Java.io.Console;
Import java.io.IOException;
Import Java.util.Scanner;

Import Org.junit.Test;

/**
* @title RC4 encryption and decryption algorithm
* @author Administrator
* @date 2015-7-16
*/
public class CRC4 {

/*
* Encryption and decryption algorithm
*/
public static string setencrypted (string value, string key) {
Declaring a 256-bit array
Int[] is = new int[256];
byte[] IK = new byte[256];
Assign value
for (int i = 0; i <; i++)
Is[i] = i;

int j = 1;

Retrieving locations
for (short i = 0; i < i++) {
Ik[i] = (byte) key.charat ((i% key.length ()));
}

j = 0;

Replace location
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 ();//Character conversion
char[] Ioutputchar = new char[iinputchar.length];//Get character 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);//do character xor or arithmetic
}
return new String (Ioutputchar);
}

/*
* Test
*/
@Test
public void Test () throws IOException {

Scanner Scanner = new Scanner (system.in);

System.out.println ("------------using RC4 and BASE64 multiple encryption Algorithms---------");
System.out.println ("Please enter the encryption value used by the RC4 (value):");
String inputstr = Scanner.nextline ();
System.out.println ("Please enter the RC4 encryption Key (key):");
String key = Scanner.nextline ();
String str = setencrypted (INPUTSTR, key);//using RC4 encryption
str = cbase64.setencrypted (str);//Using Base64 encoding (CBASE64 encryption class see <java using BASE64 encryption & decryption >)

Print the encrypted string
System.out.println ("\ n---------------------\ n Encrypted characters:" + str);

str = cbase64.getencrypted (str);//decode with CBase64 first
Print the decrypted string
System.out.println ("decrypted character:" + setencrypted (str, key));
}
}

The operation results are as follows

RC4 encryption and decryption algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.