2010-12-20 98 views
2

我在Visual Basic中有一個加密的字符串。 NET 2008年,加密和解密功能如下:等價於Java中的CryptoStream .NET?

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 

例如單詞「機器人」這個函數加密給我結果「B3xogi/Qfsc =」

現在我需要解密字符串「B3xogi/Qfsc =」它來自java,由相同的鍵,這是「key12345」,結果應該是「android」...任何人都知道如何做到這一點?

在此先感謝。

+0

編輯我的帖子,閱讀第二一篇文章,可能對你更有用,它的例子是好了很多,看着你的問題:) – LaGrandMere 2010-12-20 13:30:53

回答

4

使用Apache共享編解碼器十六進制和base64編碼/解碼,可以使用下面的代碼:

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")); 
+0

感謝時,我下載了類「阿帕奇共享編解碼器」,但在line Base64.decodeBase64(B3xogi/Qfsc =「)會引發InvocationTargetException錯誤」java.lang.NoSuchMethodError:org.apache.commons.codec.binary.Base64。 decodeBase64 – seba123neo 2010-12-20 19:21:56

+0

也許你應該在嘗試更困難的事情之前學習一些Java基礎知識?你顯然是用不同版本的Commons Codec編譯和運行代碼(使用舊版本運行,其中decodeBase64方法尚未實現)。 – jarnbjo 2010-12-20 22:12:28

+0

我下載了最新版本的Commons Codec 1.4,鏈接是: http://commons.apache.org/codec/download_codec.cgi – seba123neo 2010-12-21 00:51:10

1
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); 
}