3

I have an encrypted string in visual basic. NET 2008, the functions to encrypt and decrypt are the following:

Imports System.Security.Cryptography

 Public Shared Function Encriptar(ByVal strValor As String) As String
    Dim strEncrKey As String = "key12345" 
    Dim byKey() As Byte = {}
    Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
    Try
        byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey)
        Dim des As New DESCryptoServiceProvider
        Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strValor)
        Dim ms As New MemoryStream
        Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
        cs.Write(inputByteArray, 0, inputByteArray.Length)
        cs.FlushFinalBlock()
        Return Convert.ToBase64String(ms.ToArray())
    Catch ex As Exception
        Return ""
    End Try
End Function

Public Shared Function Desencriptar(ByVal strValor As String) As String
    Dim sDecrKey As String = "key12345" 
    Dim byKey() As Byte = {}
    Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
    Dim inputByteArray(strValor.Length) As Byte
    Try
        byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey)
        Dim des As New DESCryptoServiceProvider
        If Trim(strValor).Length = 0 Then
            Throw New Exception("Password No debe estar en Blanco")
        End If
        inputByteArray = Convert.FromBase64String(strValor)
        Dim ms As New MemoryStream
        Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
        cs.Write(inputByteArray, 0, inputByteArray.Length)
        cs.FlushFinalBlock()
        Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
        Return encoding.GetString(ms.ToArray(), 0, ms.ToArray.Count)
    Catch ex As Exception
        Return ""
    End Try
End Function

for example the word "android" encrypted with this function gives me the result "B3xogi/Qfsc="

now I need to decrypt the string "B3xogi/Qfsc=" it from java, by the same key, which is "key12345", and the result should be "android"...anyone know how to do this?

Thanks in advance.

seba123neo
  • 4,688
  • 10
  • 35
  • 53
  • Edited my post, read the 2nd article, may be more useful to you, its example is a lot better when looking at your question :) – LaGrandMere Dec 20 '10 at 13:30

4 Answers4

5

Using Apache Commons Codec for hex and base64 encoding/decoding, you can use the following code:

KeySpec ks = new DESKeySpec("key12345".getBytes("UTF-8"));
SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(ks);

IvParameterSpec iv = new IvParameterSpec(
        Hex.decodeHex("1234567890ABCDEF".toCharArray()));

Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, iv);

byte[] decoded = cipher.doFinal(Base64.decodeBase64("B3xogi/Qfsc="));

System.out.println("Decoded: " + new String(decoded, "UTF-8"));
jarnbjo
  • 33,923
  • 7
  • 70
  • 94
  • thanks, I downloaded the classes "Apache Commons Codec", but in line Base64.decodeBase64(B3xogi/Qfsc=") throws an InvocationTargetException error" java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64. decodeBase64 – seba123neo Dec 20 '10 at 19:21
  • Perhaps you should learn some Java basics before trying more difficult things? You are obviously compiling and running the code with different versions of Commons Codec (running with an older version, in which the method decodeBase64 is not yet implemented). – jarnbjo Dec 20 '10 at 22:12
  • I downloaded the latest version of Commons Codec 1.4, the link is: http://commons.apache.org/codec/download_codec.cgi – seba123neo Dec 21 '10 at 00:51
  • it works!! must be put. getBytes(), then the code final is: VPassword String = "B3xogi/Qfsc="; byte [] decoded = cipher.doFinal(Base64.decodeBase64 (vPassword.getBytes())); – seba123neo Dec 21 '10 at 01:12
2

You're using DES encryption.

Here is an example on how to encrypt and decrypt with DES.

The main point is to create a SecretKey, a Cipher using it, and decrypt your String with it.

Mmmh...

I found another article that may best suit your question, because it uses IVBytes :)

LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
2
public String encryptText(String cipherText) throws Exception {

    String plainKey = "key12345";
    String plainIV = "1234567890ABCDEF";

    KeySpec ks = new  DESKeySpec(plainKey.getBytes(encodingType));
    SecretKey key = SecretKeyFactory.getInstance(keyDes).generateSecret(ks);

    IvParameterSpec iv = new IvParameterSpec(
            org.apache.commons.codec.binary.Hex.decodeHex(plainIV.toCharArray()));

    Cipher cipher = Cipher.getInstance(encryptAlgo);
    cipher.init(Cipher.ENCRYPT_MODE, key, iv);

    byte[] decoded = cipher.doFinal(cipherText.getBytes(encodingType));

    return new Base64().encodeToString(decoded);
}
praveen
  • 21
  • 1
0

The closest Java classes to .NET's CryptoStream class are the CipherInputStream and CipherOutputStream classes.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125