将流数据写入文件会将字符转换为? [英] Writing Stream data to file turns a character to ?

查看:63
本文介绍了将流数据写入文件会将字符转换为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个包含各种字符的流。当我使用StreamWriters将其写入文件(尝试了3个不同的类)时,其中一个有点垂直矩形的字符被写为此字符 。这在c#中解密时会产生异常。



问题是这些字符是AES加密的结果,我想用C#解密这个文件。当我在c#中加密相同的文本时,它不会产生这些问号但会生成矩形..它在java中完全解密。



但是当我在C#中加密并解密时在JAVA中,它工作得非常好......那就是C#使矩形和java转换成功加密文档成为纯文本。



请帮忙!



Hi I have a stream that contains all sorts of characters. When I write it to file using StreamWriters (tried 3 different classes) one of the char that is kinda vertical rectangle gets written as this character �. This gives exception while being decrypted in c#

The problem is that these characters are result of AES encryption and I want to decrypt this file in C#. When i encrypt the same text in c# it doesnt make these question marks but makes rectangles.. which gets decrypted in java perfectly.

However when i encrypt in C# and decrypt in JAVA, it works perfectly fine... That is C# makes rectangles and java converts that encrypted document successfully into plain text.

Please help!

引用:

Ç>ÓȺ?Ÿ¾£$#·g-£|  ÊÙ9

Ç›ÓȺŸ¾£$#·g–£¦ �ÊÙ9










Quote:



byte [] IV = {65,1,2,23,4,5,6,7,32,21,10,11,12,13,84,45};

byte [] KEY = {0,42,2,54,4,45,6,7,65,9,54,11,12,13,60,15};



int iRead = 0;



SecretKeySpec key = new SecretKeySpec(KEY,AES);

Cipher cipher = Cipher.getInstance(AES / CBC / NoPadding);

cipher.init( javax.crypto.Cipher.ENCRYPT_MODE,key,new IvParameterSpec(IV));



String a =

byte baData [] = a.getBytes();



String strResult = new String(cipher.doFinal(baData));



FileOutputStream out = new FileOutputStream(C:\\Documents and Settings \\Umar \\Desktop\\testOut.xml);

out.write( strResult.getBytes());

out.close();


byte[] IV = { 65, 1, 2, 23, 4, 5, 6, 7, 32, 21, 10, 11, 12, 13, 84, 45 };
byte[] KEY = { 0, 42, 2, 54, 4, 45, 6, 7, 65, 9, 54, 11, 12, 13, 60, 15 };

int iRead = 0;

SecretKeySpec key = new SecretKeySpec(KEY, "AES");
Cipher cipher = Cipher.getInstance ("AES/CBC/NoPadding");
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE,key,new IvParameterSpec(IV));

String a = "
byte baData[] = a.getBytes();

String strResult = new String(cipher.doFinal(baData));

FileOutputStream out = new FileOutputStream("C:\\Documents and Settings\\Umar\\Desktop\\testOut.xml");
out.write(strResult.getBytes());
out.close();







比较调试中的strResult与输出文件!




compare the strResult in debug with the output file!

推荐答案

#·g-£|  ÊÙ9
#·g–£¦ �ÊÙ9










Quote:



byte [] IV = { 65,1,2,23,4,5,6,7,32,21,10,11,12,13,84,45};

byte [] KEY = {0,42 ,2,54,4,45,6,7,65,9,54,11,12,13,60,15};



int iRead = 0 ;



SecretKeySpec key = new SecretKeySpec(KEY,AES);

Cipher cipher = Cipher.getInstance(AES / CBC / NoPadding);

cipher.init(javax.crypto.Cipher.ENCRYPT_MODE,key,new IvParameterSpec(IV));



String a =

byte baData [] = a.getBytes();



String strResult = new String(cipher.doFinal(baData));



FileOutputStream out = new FileOutputStream(C:\\Documents and Settings \\Umar \\Desktop\\testOut.xml);

out.write(strResult.getBytes());

out.close();


byte[] IV = { 65, 1, 2, 23, 4, 5, 6, 7, 32, 21, 10, 11, 12, 13, 84, 45 };
byte[] KEY = { 0, 42, 2, 54, 4, 45, 6, 7, 65, 9, 54, 11, 12, 13, 60, 15 };

int iRead = 0;

SecretKeySpec key = new SecretKeySpec(KEY, "AES");
Cipher cipher = Cipher.getInstance ("AES/CBC/NoPadding");
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE,key,new IvParameterSpec(IV));

String a = "
byte baData[] = a.getBytes();

String strResult = new String(cipher.doFinal(baData));

FileOutputStream out = new FileOutputStream("C:\\Documents and Settings\\Umar\\Desktop\\testOut.xml");
out.write(strResult.getBytes());
out.close();







将debug中的strResult与输出文件进行比较!




compare the strResult in debug with the output file!


在实际解密之前,消息由 bytes (不是字符),也就是你应该查看消息字节值,没有试图将其解释为字符串。如果两个不同系统加密的消息中的字节内容相同,则加密算法在两个系统中的工作方式相同。
Until you actually decrypt, the message is composed of bytes (not characters), that is you should look at message byte values, without trying to interpretate it as a string. If you have the same byte content in the messages encrypted by the two different systems, then the encryption algorithm is working the same way in both systems.


AES加密将所有数据转换为字节流。您不应使用任何特定编码或任何字符。您不能使用 StreamWriter StreamReader 。你没有显示任何代码,所以我看不出你搞砸了什么东西,但看起来整个想法都错了。



相反,你可以使用 System.IO.BinaryWriter System.IO.BinaryReader

http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx [ ^ ],

http://msdn.microsoft.com/en- us / library / system.io.binaryreader.aspx [ ^ ]。



-SA
AES encryption converts all data into stream of bytes. You should not use any certain encoding or any characters. You cannot use StreamWriter or StreamReader. You did not show any code, so I cannot see where you screw up things, but it looks like the whole idea is wrong.

Instead, you can use System.IO.BinaryWriter and System.IO.BinaryReader:
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^].

—SA


这篇关于将流数据写入文件会将字符转换为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆