相当于Java中的CryptoStream.NET?

相当于Java中的CryptoStream.NET?,java,.net,android,visual-studio-2008,encryption,Java,.net,Android,Visual Studio 2008,Encryption,我在visual basic中有一个加密字符串。NET 2008,用于加密和解密的函数如下所示: 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

我在visual basic中有一个加密字符串。NET 2008,用于加密和解密的函数如下所示:

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
例如,用这个函数加密的单词“android”给了我结果“B3xogi/Qfsc=”

现在我需要用相同的密钥,即“key12345”,从java解密字符串“B3xogi/Qfsc=”,结果应该是“android”…有人知道怎么做吗


提前感谢。

您正在使用DES加密

下面是一个关于如何使用的示例

主要的一点是使用它创建一个,并用它解密字符串


我发现这可能最适合您的问题,因为它使用IVBytes:)

使用Apache Commons编解码器进行十六进制和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"));

与.NET的
CryptoStream
类最接近的Java类是and类。

编辑我的文章,阅读第二篇文章,可能对您更有用,当查看您的问题时,它的示例要好得多:)谢谢,我下载了类“Apache Commons Codec”,但在Base64.decodeBase64(B3xogi/Qfsc=“)抛出InvocationTargetException错误“java.lang.NoSuchMethodError:org.apache.commons.codec.binary.Base64”。在尝试更困难的事情之前,也许你应该先学习一些Java基础知识?显然,您正在使用不同版本的Commons Codec编译和运行代码(使用旧版本运行,其中尚未实现decodeBase64方法)。我下载了Commons Codec 1.4的最新版本,链接是:它可以工作!!一定要放。getBytes(),那么最后的代码是:VPassword String=“B3xogi/Qfsc=”;byte[]decoded=cipher.doFinal(Base64.decodeBase64(vPassword.getBytes());
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);
}