我正在读取 MS Office Word 2010 文件 (.docx),然后对其进行处理,然后将其写入新文件。我添加的唯一字符是大多数键盘上的字符(字母、数字、标点符号……),而且我还在稍微移动现有字符。
StreamReader sr = new StreamReader(File.OpenRead("fs.docx"));
string foo = sr.ReadToEnd();
sr.Close();
string foo2 = EncryptFile(foo);
StreamWriter sw = new StreamWriter(File.Create("sal.docx"));
sw.Write(foo2); // THIS IS WHERE THE EXCEPTION HAPPENS
sw.Close();
foo = DecryptFile(foo2);
StreamWriter sww = new StreamWriter(File.Create("sal2.docx"));
sww.Write(foo);
sww.Close();
public static string Salt(string Input)
{
Random rand = new Random();
string Output = "";
string BigSalt = "";
int SaltIncrement = rand.Next(4, 8);
for (int i = 0; i < 10; i++) {
BigSalt += FindCipherPlainText.Substring(rand.Next(0, FindCipherPlainText.Length), 1);
}
Input = BigSalt + Input;
for (int i = Input.Length; i >= 0; i--) {
if ((decimal)i % SaltIncrement == 0) {
Input = Input.Insert(i, FindCipherPlainText.Substring(rand.Next(0, FindCipherPlainText.Length), 1));
}
}
Input += FindCipherPlainText.Substring(rand.Next(0, FindCipherPlainText.Length), 1);
Input = ((SaltIncrement + 2) * 8).ToString().Substring(1, 1) + Input + ((SaltIncrement + 2) * 8).ToString().Substring(0, 1) + rand.Next(0, 10).ToString();
return Input;
}
public static string Mix(string Input) {
string Output = "";
if (Input.Length > 1)
{
if (Input.Length % 2 == 0)
{
Output = Input.Substring(Input.Length / 2);
Output += Input.Substring(0, Input.Length / 2);
}
else
{
Output = Input.Substring((Input.Length - 1) / 2);
Output += Input.Substring(0, (Input.Length - 1) / 2);
}
}
else {
return Input;
}
return Output;
}
public static string Shift(string Input) {
string Output = "";
bool Found = false;
for (int i = 0; i < Input.Length; i++) {
Found = false;
for (int ii = 0; ii < FindCipherPlainText.Length; ii++) {
if (Input.Substring(i, 1) == FindCipherPlainText.Substring(ii, 1)) {
Output = Output.Insert(0, ReplaceCipherPlainText.Substring(ii, 1));
Found = true;
break;
}
}
if (!Found) {
Output = Output.Insert(0, Input.Substring(i, 1));
}
}
return Output;
}
public static string EncryptFile(string Input) {
return Mix( Salt( Shift( Mix( Input))));
}
System.Text.EncoderFallbackException was unhandled
Message=Unable to translate Unicode character \uDF23 at index 428 to specified code page.
Source=mscorlib
Index=428
这是我的代码,以及一些异常细节,我在上面描述了 EncryptFile() 和 DecryptFile() 的作用,添加字符,移动它们......有人知道为什么会这样吗?