Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8128861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:04:11+00:00 2026-06-06T08:04:11+00:00

I have this vb.net code ( but I think meaning code is equivalent for

  • 0

I have this vb.net code ( but I think meaning code is equivalent for c# too) that I have to replicate in Java and I can’t modify it in anyway (just replicate):

Public Shared Function Encrypt(ByVal plainText As String, Optional key As String = "") As String

    If String.IsNullOrEmpty(key) Then key = "sfdjf48mdfdf3054"

    Dim encrypted As String = Nothing
    Try

        Dim inputBytes As Byte() = ASCIIEncoding.ASCII.GetBytes(plainText)
        Dim pwdhash As Byte() = Nothing

        'generate an MD5 hash from the password.
        'a hash is a one way encryption meaning once you generate
        'the hash, you cant derive the password back from it.
        Dim hashmd5 As New MD5CryptoServiceProvider()
        pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key))
        hashmd5 = Nothing

        ' Create a new TripleDES service provider
        Dim tdesProvider As New TripleDESCryptoServiceProvider()
        tdesProvider.Key = pwdhash
        tdesProvider.Mode = CipherMode.ECB

        encrypted = Convert.ToBase64String(tdesProvider.CreateEncryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length))

    Catch e As Exception
        Dim str As String = e.Message
        Throw
    End Try
    Return encrypted
End Function

Exactly, this is a .NET Utility Class function.

Now,

I don’t know how to replicate

TripleDESCryptoServiceProvider()

In java code I began to write some code, but I don’t know how to continue:

public static String encrypt(String plaintext, String enctoken){

    if(enctoken == null)
        enctoken = "sfdjf48mdfdf3054";

    String encrypted = null;

    byte[] plaintextByte = EncodingUtils.getAsciiBytes(plaintext);

    //nel caso non funzionasse provare getBytes semplicemente
    byte[] pwd = EncodingUtils.getAsciiBytes(Connessione.md5(enctoken));        

    try {
        Cipher cipher = Cipher.getInstance("DESEDE/ECB/NoPadding");
        SecretKeySpec myKey = new SecretKeySpec(pwd,"DESede");

        cipher.init( Cipher.ENCRYPT_MODE, myKey);

        try {
            byte[] encryptedPlainText= cipher.doFinal(plaintextByte);

            encrypted = Base64.encodeToString(encryptedPlainText, 0); 
            return encrypted;

        } catch (IllegalBlockSizeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       

    return "";
}

UPDATE:

I’ve just updated code. Now should be good. Enctoken String will be a string data rappresentation (just known). In this way, only if second parameter is NULL then fixed key is setted

UPDATE 2

Unfortunally c# hash is always different from Java hash!!
How to replicate this in java ??

 Dim hashmd5 As New MD5CryptoServiceProvider()
    pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key))
    hashmd5 = Nothing

    ' Create a new TripleDES service provider
    Dim tdesProvider As New TripleDESCryptoServiceProvider()
    tdesProvider.Key = pwdhash
    tdesProvider.Mode = CipherMode.ECB

    encrypted = Convert.ToBase64String(tdesProvider.CreateEncryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length))

I tried in this way but it doesn’t work:

HASH

public static final String md5(byte[] s) { 
    try { 

        MessageDigest m = MessageDigest.getInstance("MD5");
        byte[] digest = m.digest(s);
        String hash = EncodingUtils.getAsciiString(digest, 0, 16);
        Log.i("MD5", "Hash: "+hash);

        return hash;

    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    }
    return "";
}   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:04:12+00:00Added an answer on June 6, 2026 at 8:04 am

    Fixed by myself! Following function gives same MD5 HASH result:

    JAVA HASH MD5

    public static final byte[] md5(String s) { 
        try { 
    
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(s.getBytes("UTF-8"));
    
            String md5 = EncodingUtils.getString(messageDigest, "UTF-8");
    
            Log.i("Function MD5", md5);
            Log.i("Function MD5 Length","Length: "+ md5.length());
    
            return messageDigest;
    
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        return null;
    }   
    

    VB.NET HASH MD5

    Dim hashmd5 As New MD5CryptoServiceProvider()
    pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key))
    

    And, TRIPLE-DES ECB JAVA is

    try {
            Cipher cipher = Cipher.getInstance("DESEDE/ECB/PKCS5Padding");
            SecretKeySpec myKey = new SecretKeySpec(hash,"DESede");
    
            cipher.init(Cipher.ENCRYPT_MODE, myKey);
    
            try {
                byte[] encryptedPlainText = cipher.doFinal(plaintextByte);
    
                encrypted = Base64.encodeToString(encryptedPlainText, 0); 
                Log.i("ENCRYPT", "Pwd encrypted: "+encrypted);
                return encrypted;
    
            } catch (IllegalBlockSizeException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (BadPaddingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    

    TRIPLE-DES VB.NET is

    ' Create a new TripleDES service provider
        Dim tdesProvider As New TripleDESCryptoServiceProvider()
        tdesProvider.Key = pwdhash
        tdesProvider.Mode = CipherMode.ECB
    
        encrypted = Convert.ToBase64String(tdesProvider.CreateEncryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to download a file, but on sourceforge.net sever there is
I have this code in my ASP.NET application written in C# that is trying
I have this javascript code that creates a slider: http://jsfiddle.net/samccone/ZMkkd/ Now, i want to
I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
I have this code : http://jsfiddle.net/VAkLn/6/ When i add a table : http://jsfiddle.net/VAkLn/7/ this
I have this webdav code thats creating folders in SharePoint: HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(folderAddress);
I have this code in a .html.erb file: <script src=http://connect.facebook.net/en_US/all.js#xfbml=1></script> <script> FB.Event.subscribe('edge.create', function(response) {
I have c#.net code which calls a method from another external/referenced .net assembly. This
In code-behind of an ASP.NET page I have this method: public string TestFunc() {
With ASP .NET MVC3. In my controller I have this portion of code MasterMindDnetEntities

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.