【发布时间】:2019-10-09 19:35:36
【问题描述】:
我有一个小型 Java 加密程序,它读取 csv 并生成要上传到 VB 应用程序的加密 csv 文件,但解密后的 VB 应用程序无法正确显示字符。
我在 Notepad++ 中打开文件并在 notepad++ 中将其转换为 UTF-8,VB 应用程序能够解密并显示所有正确的字符。 然后我继续修改我的代码以使用 UTF-8 从 csv 中读取行,但我没有工作。我尝试读取文件将其转换为字节并使用 UTF-8 编码创建一个新字符串,但这也不起作用。为什么当我使用 notepad++ 转换为 UTF-8 时它可以工作,但当我尝试在我的代码中转换时却不行。
VB函数
//each line from the encrypted file is processed by this VB RC4 function
Private Function RC4(ByVal Str As String, ByVal Pwd As String) As String
Dim strReturnValue As String = ""
Dim Sbox(0 To 255) As Integer
Dim A, B, c
Dim Key() As Byte, ByteArray() As Byte, Tmp As Byte
If Len(Pwd) = 0 Or Len(Str) = 0 Then GoTo Exit_Function
If Len(Pwd) > 256 Then
Key = System.Text.Encoding.Default.GetBytes(Microsoft.VisualBasic.Left(Pwd, 256)) ''StrConv(Left$(Pwd, 256), vbFromUnicode)
Else
Key = System.Text.Encoding.Default.GetBytes(Pwd) ''StrConv(Pwd, vbFromUnicode)
End If
For A = 0 To 255
Sbox(A) = A
Next A
A = 0 : B = 0 : c = 0
For A = 0 To 255
B = (B + Sbox(A) + Key(A Mod Len(Pwd))) Mod 256
Tmp = Sbox(A)
Sbox(A) = Sbox(B)
Sbox(B) = Tmp
Next A
A = 0 : B = 0 : c = 0
ByteArray = System.Text.Encoding.Default.GetBytes(Str) '' StrConv(Str, vbFromUnicode)
For A = 0 To Len(Str) - 1
B = (B + 1) Mod 256
c = (c + Sbox(B)) Mod 256
Tmp = Sbox(B)
Sbox(B) = Sbox(c)
Sbox(c) = Tmp
ByteArray(A) = ByteArray(A) Xor (Sbox((Sbox(B) + Sbox(c)) Mod 256))
Next A
strReturnValue = System.Text.Encoding.Default.GetString(ByteArray) ''StrConv(ByteArray, vbUnicode)
Exit_Function:
Return strReturnValue
End Function
Java
//processing line from the input file and writing to file
try
{
FileOutputStream fos = newFileOutputStream("stackOverFlow\\EncryptedFile.csv",true);
byte [] encrypt = EncryptFile.RC4(line.getBytes(), pwd);
fos.write(encrypt);
fos.close();
}
catch (Exception e1)
{
e1.printStackTrace();
}
【问题讨论】:
-
删除
On Error Resume Next这一行。它可以防止计算机告诉您是否有问题。 -
我有但没有错误。
标签: java vb.net encryption encoding notepad++