Android Question Help java to B4x Function

victormedranop

Well-Known Member
Licensed User
Longtime User
Hi, is there a way to convert this java function to b4a. I converted in library an tested in 7.01.
my knowledge of java its low to mediun. in the library converted not work.

thanks.

victor

B4X:
public static String Encriptar(String texto,String clave) {
  String secretKey = clave;
  String base64EncryptedString = "";
  try {
  MessageDigest md = MessageDigest.getInstance("MD5");
  byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
  byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
  SecretKey key = new SecretKeySpec(keyBytes, "DESede");
  Cipher cipher = Cipher.getInstance("DESede");
  cipher.init(Cipher.ENCRYPT_MODE, key);
  byte[] plainTextBytes = texto.getBytes("utf-8");
  byte[] buf = cipher.doFinal(plainTextBytes);
  byte[] base64Bytes = Base64.encodeBase64(buf);
  base64EncryptedString = new String(base64Bytes);
  } catch (Exception ex) {
  }
 
  return base64EncryptedString;
  }
 

wes58

Active Member
Licensed User
Longtime User
Hi, is there a way to convert this java function to b4a. I converted in library an tested in 7.01.
my knowledge of java its low to mediun. in the library converted not work.

thanks.

victor

B4X:
public static String Encriptar(String texto,String clave) {
  String secretKey = clave;
  String base64EncryptedString = "";
  try {
  MessageDigest md = MessageDigest.getInstance("MD5");
  byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
  byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
  SecretKey key = new SecretKeySpec(keyBytes, "DESede");
  Cipher cipher = Cipher.getInstance("DESede");
  cipher.init(Cipher.ENCRYPT_MODE, key);
  byte[] plainTextBytes = texto.getBytes("utf-8");
  byte[] buf = cipher.doFinal(plainTextBytes);
  byte[] base64Bytes = Base64.encodeBase64(buf);
  base64EncryptedString = new String(base64Bytes);
  } catch (Exception ex) {
  }

  return base64EncryptedString;
}
Hi, is there a way to convert this java function to b4a. I converted in library an tested in 7.01.
my knowledge of java its low to mediun. in the library converted not work.

thanks.

victor

B4X:
public static String Encriptar(String texto,String clave) {
  String secretKey = clave;
  String base64EncryptedString = "";
  try {
  MessageDigest md = MessageDigest.getInstance("MD5");
  byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
  byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
  SecretKey key = new SecretKeySpec(keyBytes, "DESede");
  Cipher cipher = Cipher.getInstance("DESede");
  cipher.init(Cipher.ENCRYPT_MODE, key);
  byte[] plainTextBytes = texto.getBytes("utf-8");
  byte[] buf = cipher.doFinal(plainTextBytes);
  byte[] base64Bytes = Base64.encodeBase64(buf);
  base64EncryptedString = new String(base64Bytes);
  } catch (Exception ex) {
  }

  return base64EncryptedString;
  }
You could try something like this:
Include JavaObject library in you project
B4X:
Sub test
Dim jo As JavaObject
Dim result, str1, str2 As String
  str1 = "put you string for texto "
  str2 = "put your string for clave"
  jo.InitializeContext
result = jo.RunMethod("Encriptar", Array(str1, str2))
End Sub

#if JAVA
import java.security.MessageDigest;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
import android.util.Base64;

public static String Encriptar(String texto,String clave) {
 String secretKey = clave;
  String base64EncryptedString = "";
  try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
    byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] plainTextBytes = texto.getBytes("utf-8");
    byte[] buf = cipher.doFinal(plainTextBytes);
    byte[] base64Bytes = Base64.encodeBase64(buf);
    base64EncryptedString = new String(base64Bytes);
  } catch (Exception ex) {
 
  }
  Return base64EncryptedString;
}
#End If
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
I'm able to encode to base64 and to decode from. but I really don't understand
how to manage message digest,md5 cypher and bytes operation and "DESde" with key. to encrypt and des encrypt.

need "encrypt for dummies"

Victor
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
working on :
B4X:
Sub desencryptmd5desde (todesencrypt As String, key As String)
 Dim md As MessageDigest
 Dim keybytes() As Byte =key.GetBytes("UTF-8")
 Dim cypher As Cipher
 cypher.Initialize("DESde")
 md.GetMessageDigest(keybytes,"MD5")
 Dim returnbytes As Byte =cypher.Decrypt(todesencrypt.GetBytes("UTF-8"),keybytes,False)
 LogColor(returnbytes,Colors.red)
 
End Sub
 
Upvote 0
Top