相当于CryptoStream的.NET中的Java?CryptoStream、NET、Java

2023-09-04 00:47:48 作者:暗恋未遂

我在Visual Basic中的加密字符串。 NET 2008,则各功能来加密和解密如下:

 进口System.Security.Cryptography

 公共共享功能Encriptar(BYVAL strValor作为字符串)作为字符串
    昏暗strEncrKey作为字符串=key12345
    昏暗byKey()作为字节= {}
    昏暗IV()作为字节= {&功放; H12,和放大器; H34,和放大器; H56,和放大器; H78,和放大器; H90,和放大器;民政事务局,和放大器; HCD,和放大器; HEF}
    尝试
        byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey)
        昏暗DES作为新DESCryptoServiceProvider
        昏暗inputByteArray()作为字节= Encoding.UTF8.GetBytes(strValor)
        昏暗的MS作为新的MemoryStream
        昏暗的CS作为新的CryptoStream(MS,des.CreateEncryptor(byKey,IV),CryptoStreamMode.Write)
        cs.Write(inputByteArray,0,inputByteArray.Length)
        cs.FlushFinalBlock()
        返回Convert.ToBase64String(ms.ToArray())
    抓住EX为例外
        返回 
    结束尝试
端功能

公共共享功能Desencriptar(BYVAL strValor作为字符串)作为字符串
    昏暗sDecrKey作为字符串=key12345
    昏暗byKey()作为字节= {}
    昏暗IV()作为字节= {&功放; H12,和放大器; H34,和放大器; H56,和放大器; H78,和放大器; H90,和放大器;民政事务局,和放大器; HCD,和放大器; HEF}
    昏暗inputByteArray(strValor.Length)为字节
    尝试
        byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey)
        昏暗DES作为新DESCryptoServiceProvider
        如果修剪(strValor).Length = 0然后
            抛出新的异常(密码不德贝易达讯EN布兰科)
        结束如果
        inputByteArray = Convert.FromBase64String(strValor)
        昏暗的MS作为新的MemoryStream
        昏暗的CS作为新的CryptoStream(MS,des.CreateDecryptor(byKey,IV),CryptoStreamMode.Write)
        cs.Write(inputByteArray,0,inputByteArray.Length)
        cs.FlushFinalBlock()
        昏暗的编码作为System.Text.Encoding = System.Text.Encoding.UTF8
        返回encoding.GetString(ms.ToArray(),0,ms.ToArray.Count)
    抓住EX为例外
        返回 
    结束尝试
端功能
 

例如单词机器人加密使用此功能给我结果B3xogi / Qfsc =

现在我需要字符串B3xogi / Qfsc =它与Java,通过相同的密钥,这是key12345,其结果应该是机器人......任何人都知道如何做到这一点解密

在此先感谢。

解决方案

使用Apache共享codeC的十六进制和base64编码/解码,可以使用下面的code:

  KeySpec KS =新DESKeySpec(key12345.getBytes(UTF-8));
SecretKey的关键= SecretKeyFactory.getInstance(DES)generateSecret(KS)。

IvParameterSpec IV =新IvParameterSpec(
        Hex.de $ C $环己烷(1234567890ABCDEF.toCharArray()));

密密码= Cipher.getInstance(DES / CBC / PKCS5Padding);
cipher.init(Cipher.DECRYPT_MODE,钥匙,四);

byte []的德codeD = cipher.doFinal(Base64.de codeBase64(B3xogi / Qfsc =));

的System.out.println(德codeD:+新的String(德codeD,UTF-8));
 

Java 开发新选择 Apache NetBeans IDE 9.0 正式发布

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.

解决方案

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