Note that there are some explanatory texts on larger screens.

plurals
  1. POTripleDES algorithm: from .NET to Java
    text
    copied!<p>I have this vb.net code ( but I think meaning code is equivalent for c# too) that I have to replicate in Java and <strong>I can't modify</strong> it in anyway (just replicate):</p> <pre><code>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 </code></pre> <p>Exactly, this is a .NET Utility Class function.</p> <p>Now,</p> <p>I don't know how to replicate</p> <pre><code>TripleDESCryptoServiceProvider() </code></pre> <p>In java code I began to write some code, but I don't know how to continue:</p> <pre><code>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 ""; } </code></pre> <p><strong>UPDATE:</strong></p> <p>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</p> <p><strong>UPDATE 2</strong></p> <p>Unfortunally c# hash is always different from Java hash!! How to replicate this in java ??</p> <pre><code> 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)) </code></pre> <p>I tried in this way but it doesn't work:</p> <p><strong>HASH</strong></p> <pre><code>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 ""; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload