java版
测试工具:https://www.runoob.com/try/runcode.php?filename=HelloWorld&type=java
打印结果:
加密后的文本1(英文): C3B0641A1B3CC2B8C38F1579C2B93DC39FC290
加密后的文本2(中文): E591B0E59389E592BEE596AA
解密后的文本1(英文): Hello, World!
解密后的文本2(中文): 哈哈哈嗝
解密后的文本: Hello, World!
解密后的文本: 哈哈哈哈哈哈哈
import java.io.UnsupportedEncodingException;
public class RC4Util {
public static String rc4Encrypt(String data, String key) throws UnsupportedEncodingException {
return bytesToHexString(HloveyRC4(data, key).getBytes("UTF-8"));
}
public static String rc4Decrypt(String data, String key) throws UnsupportedEncodingException {
return HloveyRC4(new String(hexStringToBytes(data), "UTF-8"), key);
}
public static String bytesToHexString(byte[] b) {
StringBuffer result = new StringBuffer();
String hex;
for (int i = 0; i < b.length; i++) {
hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
result.append(hex.toUpperCase());
}
return result.toString();
}
private static byte[] hexStringToBytes(String src) {
int len = src.length() / 2;
byte[] ret = new byte[len];
for (int i = 0; i < len; i++) {
ret[i] = (byte) Integer.parseInt(src.substring(i * 2, i * 2 + 2), 16);
}
return ret;
}
private 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 (int 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) {
try {
String key = "Aa_12345678";
String plainText1 = "Hello, World!";
String encryptedText1 = rc4Encrypt(plainText1, key);
System.out.println("加密后的文本1(英文): " + encryptedText1);
String plainText2 = "哈哈哈嗝";
String encryptedText2 = rc4Encrypt(plainText2, key);
System.out.println("加密后的文本2(中文): " + encryptedText2);
String decryptedText1 = rc4Decrypt(encryptedText1, key);
System.out.println("解密后的文本1(英文): " + decryptedText1);
String decryptedText2 = rc4Decrypt(encryptedText2, key);
System.out.println("解密后的文本2(中文): " + decryptedText2);
String encryptedStr1 = "C3B0641A1B3CC2B8C38F1579C2B93DC39FC290";
String decryptedStr1 = rc4Decrypt(encryptedStr1, key);
System.out.println("解密后的文本: " + decryptedStr1);
String encryptedStr2 = "E591B0E59389E592BEE592BFE5929BE5919CE590A7";
String decryptedStr2 = rc4Decrypt(encryptedStr2, key);
System.out.println("解密后的文本: " + decryptedStr2);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
} DevEco Studio 3.1.1 Release
api 9
真机打印结果(注意,预览器不行,只能用真机测)
加密后的文本1(英文): c3b0641a1b3cc2b8c38f1579c2b93dc39fc290
加密后的文本2(中文): e591b0e59389e592bee596aa
解密后的文本1(英文): Hello, World!
解密后的文本2(中文): 哈哈哈嗝
解密后的文本: Hello, World!
解密后的文本: 哈哈哈哈哈哈哈
import util from '@ohos.util';
// Java版的hexString2Bytes转换为TypeScript版
function hexString2Bytes(src: string): Uint8Array {
const l = src.length / 2;
const ret: number[] = new Array(l);
for (let i = 0; i < l; i++) {
ret[i] = parseInt(src.substr(i * 2, 2), 16);
}
return new Uint8Array(ret);
}
// 将Java版HloveyRC4转换为TypeScript版
function hloveyRC4(input: string, key: string): string {
const s: number[] = new Array(256).fill(0);
const k: number[] = [];
for (let i = 0; i < 256; i++) {
s[i] = i;
}
let j = 1;
for (let i = 0; i < 256; i++) {
k[i] = key.charCodeAt(i % key.length);
}
j = 0;
for (let i = 0; i < 255; i++) {
j = (j + s[i] + k[i]) % 256;
const temp = s[i];
s[i] = s[j];
s[j] = temp;
}
let i = 0;
j = 0;
const inputCharCode = Array.from(input).map(c => c.charCodeAt(0));
const outputCharCode: number[] = new Array(inputCharCode.length);
for (let x = 0; x < inputCharCode.length; x++) {
i = (i + 1) % 256;
j = (j + s[i]) % 256;
const temp = s[i];
s[i] = s[j];
s[j] = temp;
const t = (s[i] + (s[j] % 256)) % 256;
const y = s[t];
outputCharCode[x] = inputCharCode[x] ^ y;
}
// 将混淆后的字符码转换为字符串
return String.fromCharCode(...outputCharCode);
}
// RC4加密方法
function rc4Encrypt(text: string, key: string): string {
const encryptedBytes = hloveyRC4(text, key);
const utf8EncodedBytes = new util.TextEncoder().encode(encryptedBytes);
return Array.from(utf8EncodedBytes).map(byte => byte.toString(16).padStart(2, '0')).join('');
}
// RC4解密方法
function rc4Decrypt(hexData: string, key: string): string {
// 将十六进制字符串转换为字节数组
const encryptedBytes = hexString2Bytes(hexData);
// 将加密字节数组解码为字符串
const encryptedText = new util.TextDecoder().decode(encryptedBytes);
// 使用RC4算法进行解密
const decryptedText = hloveyRC4(encryptedText, key);
return decryptedText;
}
// 测试用例
@Entry
@Component
struct Test {
build() {
Column() {
Button('测试').onClick(() => {
const key = 'Aa_12345678';
// 加密
let encryptedText1 = rc4Encrypt('Hello, World!', key);
console.log('加密后的文本1(英文):', encryptedText1);
let encryptedText2 = rc4Encrypt('哈哈哈嗝', key);
console.log('加密后的文本2(中文):', encryptedText2);
// 解密
let decryptedText1 = rc4Decrypt(encryptedText1, key);
console.log('解密后的文本1(英文):', decryptedText1);
let decryptedText2 = rc4Decrypt(encryptedText2, key);
console.log('解密后的文本2(中文):', decryptedText2);
let str1 = 'C3B0641A1B3CC2B8C38F1579C2B93DC39FC290'
console.log('解密后的文本:', rc4Decrypt(str1, key));//Hello, World!'
let str2 = 'E591B0E59389E592BEE592BFE5929BE5919CE590A7'
console.log('解密后的文本:', rc4Decrypt(str2, key));//哈哈哈哈哈哈哈
});
}
}
} 




































苏公安网备 32011402010933号