OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

PHP to JavaScript Conversion Quasar Vue3 Vite

  • Thread starter Thread starter Gyorgy Botond
  • Start date Start date
G

Gyorgy Botond

Guest
I have an API which has an example written in PHP. Unfortunately I need to write in in JavaScript but lack the knowledge to convert it myself.

Are there any online converters or documentation which may be able to help me?

PHP:

public class EncryptDecrypt { private static final String InitVector = "aaaaaaaaaaaaaaaa"; private static final int Keysize = 256; // This constant is used to determine the keysize of the encryption algorithm. private static final String PassPhrase = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//encryption Key text private static final String CipherTransformation = "AES/CBC/PKCS5Padding"; private static final String AesEncryptionAlgorithm = "AES"; private static final char[] HexArray = "0123456789ABCDEF".toCharArray();

Code:
public static byte[] EncryptToBinary(String plainText) throws Exception {
    try {
        byte[] initVectorBytes = InitVector.getBytes("UTF-8");
        byte[] plainTextBytes = plainText.getBytes("UTF-8");
        byte[] passPhraseTextBytes = PassPhrase.getBytes("UTF-8");

        PasswordDeriveBytes password = new PasswordDeriveBytes(initVectorBytes, passPhraseTextBytes, "SHA-1", 1);
        byte[] keyBytes = password.getBytes(Keysize / 8);

        Cipher cipher = Cipher.getInstance(CipherTransformation);
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AesEncryptionAlgorithm);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initVectorBytes);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

        return cipher.doFinal(plainTextBytes);
    }
    catch(Exception ex) {
        throw ex;
    }
}

public static String EncryptToString(String plainText) throws Exception {
    try {
        return Base64.encodeToString(EncryptToBinary(plainText), Base64.DEFAULT);
    }
    catch(Exception ex) {
        throw ex;
    }
}

public static String EncryptToStringWithoutEncrypted(String plainText) throws Exception {
    try {
        return Base64.encodeToString(plainText.getBytes("UTF-8"), Base64.DEFAULT);
    }
    catch(Exception ex) {
        throw ex;
    }
}

public static byte[] DecryptToBinary(String cipherText) throws Exception {  //ellenorizni kell majd hogy a vissza fejtes jo-e!!!
    try {
        byte[] initVectorBytes = InitVector.getBytes("UTF-8");
        byte[] cipherTextBytes = Base64.decode(cipherText, Base64.DEFAULT);

        PasswordDeriveBytes password = new PasswordDeriveBytes(PassPhrase, null);
        byte[] keyBytes = password.getBytes(Keysize / 8);

        Cipher cipher = Cipher.getInstance(CipherTransformation);
        SecretKeySpec secretKeySpecy = new SecretKeySpec(keyBytes, AesEncryptionAlgorithm);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initVectorBytes);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec);

        return cipher.doFinal(cipherTextBytes);
    }
    catch(Exception ex) {
        throw ex;
    }
}

public static String DecryptToString(String cipherText) throws Exception {  //ellenorizni kell majd hogy a vissza fejtes jo-e!!!
    try {
        return new String(DecryptToBinary(cipherText));
    }
    catch(Exception ex) {
        throw ex;
    }
}

public static String ToBinary(byte[] bytes) throws Exception {
    try {
        StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE);
        for (int i = 0; i < Byte.SIZE * bytes.length; i++)
            sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');

        return sb.toString();
    }
    catch(Exception ex) {
        throw ex;
    }
}

public static byte[] FromBinary(String s) throws Exception {
    try {
        int sLen = s.length();
        byte[] toReturn = new byte[(sLen + Byte.SIZE - 1) / Byte.SIZE];
        char c;
        for (int i = 0; i < sLen; i++)
            if ((c = s.charAt(i)) == '1')
                toReturn[i / Byte.SIZE] = (byte) (toReturn[i / Byte.SIZE] | (0x80 >>> (i % Byte.SIZE)));
            else if (c != '0')
                throw new IllegalArgumentException();

        return toReturn;
    }
    catch(Exception ex) {
        throw ex;
    }
}

public static String BytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    for ( int j = 0; j < bytes.length; j++ ) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = HexArray[v >>> 4];
        hexChars[j * 2 + 1] = HexArray[v & 0x0F];
    }
    return new String(hexChars);
}

}

String encryptedHex = EncryptDecrypt.BytesToHex(EncryptDecrypt.EncryptToBinary('abcde')); Result: 8DE925AE9509DAE98D5936C5C9268A06 in Javascript :)

<p>I have an API which has an example written in PHP. Unfortunately I need to write in in JavaScript but lack the knowledge to convert it myself.</p>
<p>Are there any online converters or documentation which may be able to help me?</p>
<p>PHP:</p>
<p>public class EncryptDecrypt {
private static final String InitVector = "aaaaaaaaaaaaaaaa";
private static final int Keysize = 256; // This constant is used to determine the keysize of the encryption algorithm.
private static final String PassPhrase = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//encryption Key text
private static final String CipherTransformation = "AES/CBC/PKCS5Padding";
private static final String AesEncryptionAlgorithm = "AES";
private static final char[] HexArray = "0123456789ABCDEF".toCharArray();</p>
<pre><code>public static byte[] EncryptToBinary(String plainText) throws Exception {
try {
byte[] initVectorBytes = InitVector.getBytes("UTF-8");
byte[] plainTextBytes = plainText.getBytes("UTF-8");
byte[] passPhraseTextBytes = PassPhrase.getBytes("UTF-8");

PasswordDeriveBytes password = new PasswordDeriveBytes(initVectorBytes, passPhraseTextBytes, "SHA-1", 1);
byte[] keyBytes = password.getBytes(Keysize / 8);

Cipher cipher = Cipher.getInstance(CipherTransformation);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, AesEncryptionAlgorithm);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initVectorBytes);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

return cipher.doFinal(plainTextBytes);
}
catch(Exception ex) {
throw ex;
}
}

public static String EncryptToString(String plainText) throws Exception {
try {
return Base64.encodeToString(EncryptToBinary(plainText), Base64.DEFAULT);
}
catch(Exception ex) {
throw ex;
}
}

public static String EncryptToStringWithoutEncrypted(String plainText) throws Exception {
try {
return Base64.encodeToString(plainText.getBytes("UTF-8"), Base64.DEFAULT);
}
catch(Exception ex) {
throw ex;
}
}

public static byte[] DecryptToBinary(String cipherText) throws Exception { //ellenorizni kell majd hogy a vissza fejtes jo-e!!!
try {
byte[] initVectorBytes = InitVector.getBytes("UTF-8");
byte[] cipherTextBytes = Base64.decode(cipherText, Base64.DEFAULT);

PasswordDeriveBytes password = new PasswordDeriveBytes(PassPhrase, null);
byte[] keyBytes = password.getBytes(Keysize / 8);

Cipher cipher = Cipher.getInstance(CipherTransformation);
SecretKeySpec secretKeySpecy = new SecretKeySpec(keyBytes, AesEncryptionAlgorithm);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initVectorBytes);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec);

return cipher.doFinal(cipherTextBytes);
}
catch(Exception ex) {
throw ex;
}
}

public static String DecryptToString(String cipherText) throws Exception { //ellenorizni kell majd hogy a vissza fejtes jo-e!!!
try {
return new String(DecryptToBinary(cipherText));
}
catch(Exception ex) {
throw ex;
}
}

public static String ToBinary(byte[] bytes) throws Exception {
try {
StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE);
for (int i = 0; i < Byte.SIZE * bytes.length; i++)
sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');

return sb.toString();
}
catch(Exception ex) {
throw ex;
}
}

public static byte[] FromBinary(String s) throws Exception {
try {
int sLen = s.length();
byte[] toReturn = new byte[(sLen + Byte.SIZE - 1) / Byte.SIZE];
char c;
for (int i = 0; i < sLen; i++)
if ((c = s.charAt(i)) == '1')
toReturn[i / Byte.SIZE] = (byte) (toReturn[i / Byte.SIZE] | (0x80 >>> (i % Byte.SIZE)));
else if (c != '0')
throw new IllegalArgumentException();

return toReturn;
}
catch(Exception ex) {
throw ex;
}
}

public static String BytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HexArray[v >>> 4];
hexChars[j * 2 + 1] = HexArray[v & 0x0F];
}
return new String(hexChars);
}
</code></pre>
<p>}</p>
<p>String encryptedHex = EncryptDecrypt.BytesToHex(EncryptDecrypt.EncryptToBinary('abcde'));
Result: 8DE925AE9509DAE98D5936C5C9268A06
in Javascript :)</p>
 

Latest posts

T
Replies
0
Views
1
Timothy Treaster
T
Top