Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simple 3DES - .NET vs JAVA problem

843811Mar 16 2008 — edited Mar 26 2008
Hello guys, a veri simple problem. I've been using a .NET class to encrypt a password, using this code:

With the input being cherry1559 the encoded result should be : ThwF22Izk/xfOjt6T+GYEQ==

I've been trying all google-available code but the result does not even come close.
Shared Function TripleDESEncode(ByVal value As String) As String
	    Dim key as String = "HG58YZ3CR9"
            Dim des As New System.Security.Cryptography.TripleDESCryptoServiceProvider
            des.IV = New Byte(7) {}
            Dim pdb As New System.Security.Cryptography.PasswordDeriveBytes(key, New Byte(-1) {})
            des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
            Dim ms As New IO.MemoryStream((value.Length * 2) - 1)
            Dim encStream As New System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
            Dim plainBytes As Byte() = Text.Encoding.UTF8.GetBytes(value)
            encStream.Write(plainBytes, 0, plainBytes.Length)
            encStream.FlushFinalBlock()
            Dim encryptedBytes(CInt(ms.Length - 1)) As Byte
            ms.Position = 0
            ms.Read(encryptedBytes, 0, CInt(ms.Length))
            encStream.Close()
            Return Convert.ToBase64String(encryptedBytes)
End Function
This is the java code I'm using:
private String TestDES(String pass,int iteracion)
	{
	    Cipher ecipher;
        Cipher dcipher;
        
		try
		{
	        int iterationCount = iteracion;
	        String passPhrase = "HG58YZ3CR9";
   	        byte[] salt = {
	   	            (byte)0x0, (byte)0x0, (byte)0x33, (byte)0x34,
	   	            (byte)0x0, (byte)0x0, (byte)0x37, (byte)0x38
	   	        };
        	KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
        	SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
            ecipher = Cipher.getInstance(key.getAlgorithm());

    
            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
            
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            
            byte[] utf8 = pass.getBytes("UTF8");


            byte[] enc = ecipher.doFinal(utf8);
            String paswordDes="";
            
            paswordDes=new sun.misc.BASE64Encoder().encode(enc);
            return paswordDes;
            
        }
        catch (java.security.InvalidAlgorithmParameterException e) { System.out.println("Invalid Algorithm"); }
        catch (java.security.spec.InvalidKeySpecException e) { System.out.println("Invalid Key Spec"); }
        catch (javax.crypto.NoSuchPaddingException e) { System.out.println("No Such Padding"); }
        catch (java.security.NoSuchAlgorithmException e) { System.out.println("No Such Algorithm"); }
        catch (java.security.InvalidKeyException e) { System.out.println("Invalid Key"); }
        catch (BadPaddingException e) { System.out.println("Invalid Key");}
        catch (IllegalBlockSizeException e) { System.out.println("Invalid Key");}
        catch (UnsupportedEncodingException e) { System.out.println("Invalid Key");}
        
       
        return null;
	}
Any help fully appreciated!!

Victor
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 23 2008
Added on Mar 16 2008
19 comments
745 views