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

问题描述

您好我有一个包含各种字符的流。当我使用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!

推荐答案










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!




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

09-18 10:48