Encrypt a string in java and decryption in VBScrip

2019-08-11 11:43发布

问题:

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

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;
    }
}

So what I am trying to do is get a string encrypt it using JAVA(RC4 Algorithm) and Decryption using Vb script .

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 .

Any solution ?

回答1:

here is my answer for encrypting a string for the same vb code asked above :

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;
}

}