本文介绍了使用RC4在Java中加密字符串并在VBScript中解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在vb脚本中有一个代码,试图将其转换为java.
I have a code in vb script which i am trying to convert it into java .
Here is my VBScript:
Sub a()
strEncrypt = "jane"
strKey = "apple"
intSeed = "6"
strEncryptedText = RunRC4(strEncrypt, strKey)
MsgBox (strEncryptedText)
strDecryptedText = RunRC4(strEncryptedText, strKey)
MsgBox (strDecryptedText)
End Sub
Function RunRC4(sMessage, strKey)
Dim kLen, x, y, i, j, temp, l
Dim s(256), k(256)
'Init keystream
kLen = Len(strKey)
For i = 0 To 255
s(i) = i
l = Mid(strKey, (i Mod kLen) + 1, 1)
k(i) = Asc(Mid(strKey, (i Mod kLen) + 1, 1))
Next
j = 0
For i = 0 To 255
j = (j + k(i) + s(i)) Mod 255
temp = s(i)
s(i) = s(j)
s(j) = temp
Next
'Drop n bytes from keystream
x = 0
y = 0
For i = 1 To 3072
x = (x + 1) Mod 255
y = (y + s(x)) Mod 255
temp = s(x)
s(x) = s(y)
s(y) = temp
Next
'Encode/Decode
For i = 1 To Len(sMessage)
x = (x + 1) Mod 255
y = (y + s(x)) Mod 255
temp = s(x)
s(x) = s(y)
s(y) = temp
temp1 = Asc(Mid(sMessage, i, 1))
temp2 = Chr(s((s(x) + s(y)) Mod 255))
RunRC4 = RunRC4 & Chr(s((s(x) + s(y)) Mod 255) Xor Asc(Mid(sMessage, i, 1)))
Next
End Function
这是我的Java代码:
And here is my java code :
package com.portal.util;
public class Sample {
public static void main(String[] args) {
String strEncrypt = "jane";
String strKey = "apple";
String intSeed = "6";
String hi = RunRC4(strEncrypt, strKey);
}
public static String RunRC4(String sMessage, String strKey) {
int kLen = 0, x = 0, y = 0, i = 0, j = 0, temp = 0 ,f=1;
int[] s = new int[500];
int[] k = new int[500];
String RunRC4 = "";
kLen = strKey.length();
for (i = 0; i < 255; i++) {
s[i] = i;
String s1="";
// int ascii = (int) strKey.charAt(i);
int modular = i % kLen;
int modular1 = modular;
//k[i] = strKey.substring(modular1, 1);
s1 = strKey.substring(modular1, modular1+1);
char c1=s1.charAt(0);
k[i]=(int)c1;
//f=f+1;
}
j=0;
for (i = 0; i < 255; i++) {
j = (j + k[i] + s[i]) % 255;
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
// 'Drop n bytes from keystream
x = 0;
y = 0;
for (i = 0; i < 3072; i++) {
x = (x + 1) % 255;
y = (y + s[x]) % 255;
temp = s[x];
s[x] = s[y];
s[y] = temp;
}
System.out.println("Hello");
// 'Encode/Decode
for (i = 0; i < sMessage.length(); i++) {
x = (x + 1) % 255;
y = (y + s[x]) % 255;
temp = s[x];
s[x] = s[y];
s[y] = temp;
String s2 = sMessage.substring(i);
char c2=s2.charAt(0);
int index = s[x] + s[y];
int value = s[index] % 255;
char temp1=(char)value;
RunRC4 = RunRC4 +(temp1 ^ (int)c2) ;
String str = RunRC4 ;
System.out.println(RunRC4);
System.out.println(Character.toString ((char) Integer.parseInt(RunRC4)));
// String RunRC4 = RunRC4 & Chr(s((s(x) + s(y)) Mod 255) Xor
// Asc(Mid(sMessage, i, 1)))
}
return RunRC4;
}
}
所以我想做的是使用JAVA(RC4算法)获得一个字符串对其进行加密,并使用Vb脚本进行解密.
So what I am trying to do is get a string encrypt it using JAVA(RC4 Algorithm) and Decryption using Vb script .
我一直试图将可以正常工作的VB脚本中的代码转换为JAVA,在那里我发现了问题.
I have been trying to convert the code which is in VB script that is working fine to JAVA there is where I finding problem .
任何解决方案吗?
推荐答案
这是我为上述相同的vb代码加密字符串的答案:
here is my answer for encrypting a string for the same vb code asked above :
公共类EncryptCode {公共静态无效main(String [] args){
public class EncryptCode { public static void main(String[] args) {
String strEncrypt = "58"; String strKey = "apple";
String intSeed = "6"; String encryptedString = RunRC4(strEncrypt, strKey);
}
public static String RunRC4(String sMessage, String strKey) {
int kLen = 0, x = 0, y = 0, i = 0, j = 0, temp = 0;
int[] s = new int[500];
int[] k = new int[500];
String RunRC4 = "";
String finalChar = "";
kLen = strKey.length();
for (i = 0; i <= 255; i++) {
s[i] = i;
String s1 = "";
int modular = i % kLen;
s1 = strKey.substring(modular, modular + 1);
char c1 = s1.charAt(0);
k[i] = (int) c1;
}
j = 0;
for (i = 0; i <= 255; i++) {
j = (j + k[i] + s[i]) % 255;
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
// 'Drop n bytes from keystream
x = 0;
y = 0;
for (i = 0; i <= 3072; i++) {
x = (x + 1) % 255;
y = (y + s[x]) % 255;
temp = s[x];
s[x] = s[y];
s[y] = temp;
}
// 'Encode/Decode
for (i = 0; i <= sMessage.length(); i++) {
x = (x + 1) % 255;
y = (y + s[x]) % 255;
temp = s[x];
s[x] = s[y];
s[y] = temp;
String s2 = sMessage.substring(i);
if (s2.equalsIgnoreCase("")) {
return finalChar;
} else {
char c2 = s2.charAt(0);
int xy = s[x] + s[y];
int value = s[xy % 255];
RunRC4 = RunRC4 + (value ^ (int) c2);
finalChar = finalChar
+ (Character.toString((char) Integer.parseInt(RunRC4)));
RunRC4 = "";
}
}
return finalChar;
}
}
这篇关于使用RC4在Java中加密字符串并在VBScript中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!