本文整理汇总了C#中CryptoStream.ReadByte方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.ReadByte方法的具体用法?C# CryptoStream.ReadByte怎么用?C# CryptoStream.ReadByte使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CryptoStream
的用法示例。
在下文中一共展示了CryptoStream.ReadByte方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
static byte[] Decrypt(byte[] asm)
{
byte[] dat;
byte[] iv;
byte[] key;
using (BinaryReader rdr = new BinaryReader(new MemoryStream(asm)))
{
dat = rdr.ReadBytes(rdr.ReadInt32());
iv = rdr.ReadBytes(rdr.ReadInt32());
key = rdr.ReadBytes(rdr.ReadInt32());
}
int key0 = Mutation.Key0I;
for (int j = 0; j < key.Length; j += 4)
{
key[j + 0] ^= (byte)((key0 & 0x000000ff) >> 0);
key[j + 1] ^= (byte)((key0 & 0x0000ff00) >> 8);
key[j + 2] ^= (byte)((key0 & 0x00ff0000) >> 16);
key[j + 3] ^= (byte)((key0 & 0xff000000) >> 24);
}
RijndaelManaged rijn = new RijndaelManaged();
using (var s = new CryptoStream(new MemoryStream(dat), rijn.CreateDecryptor(key, iv), CryptoStreamMode.Read))
{
byte[] l = new byte[4];
s.Read(l, 0, 4);
uint len = BitConverter.ToUInt32(l, 0);
LzmaDecoder decoder = new LzmaDecoder();
byte[] prop = new byte[5];
s.Read(prop, 0, 5);
decoder.SetDecoderProperties(prop);
long outSize = 0;
for (int i = 0; i < 8; i++)
{
int v = s.ReadByte();
if (v < 0)
throw (new Exception("Can't Read 1"));
outSize |= ((long)(byte)v) << (8 * i);
}
byte[] ret = new byte[outSize];
long compressedSize = len - 13;
decoder.Code(s, new MemoryStream(ret, true), compressedSize, outSize);
return ret;
}
}
示例2: DecryptFile
public static string DecryptFile(string filePath)
{
Debug.Log("Decrypting");
var output = String.Empty;
try
{
using (RijndaelManaged aes = new RijndaelManaged())
{
byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
byte[] IV = ASCIIEncoding.UTF8.GetBytes(vkey);
using (FileStream fsCrypt = new FileStream(filePath + "eSave.dat", FileMode.Open))
{
using (FileStream fsOut = new FileStream(filePath + "uSave.dat", FileMode.Create))
{
using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
{
using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
{
int data;
while ((data = cs.ReadByte()) != -1)
{
fsOut.WriteByte((byte)data);
}
output = fsOut.ToString();
}
}
}
}
}
//File.Delete(filePath + "uSave.dat");
}
catch{} // failed to decrypt file
return output;
}
示例3: Roundtrip
//.........这里部分代码省略.........
Assert.Throws<NotSupportedException>(() => encryptStream.Read(new byte[0], 0, 0));
Assert.Throws<NullReferenceException>(() => encryptStream.Write(null, 0, 0)); // No arg validation on buffer?
Assert.Throws<ArgumentOutOfRangeException>(() => encryptStream.Write(new byte[0], -1, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => encryptStream.Write(new byte[0], 0, -1));
Assert.Throws<ArgumentOutOfRangeException>(() => encryptStream.Write(new byte[0], 0, -1));
Assert.Throws<ArgumentException>(() => encryptStream.Write(new byte[3], 1, 4));
byte[] toWrite = Encoding.UTF8.GetBytes(LoremText);
// Write it all at once
encryptStream.Write(toWrite, 0, toWrite.Length);
Assert.False(encryptStream.HasFlushedFinalBlock);
// Write in chunks
encryptStream.Write(toWrite, 0, toWrite.Length / 2);
encryptStream.Write(toWrite, toWrite.Length / 2, toWrite.Length - (toWrite.Length / 2));
Assert.False(encryptStream.HasFlushedFinalBlock);
// Write one byte at a time
for (int i = 0; i < toWrite.Length; i++)
{
encryptStream.WriteByte(toWrite[i]);
}
Assert.False(encryptStream.HasFlushedFinalBlock);
// Write async
encryptStream.WriteAsync(toWrite, 0, toWrite.Length).GetAwaiter().GetResult();
Assert.False(encryptStream.HasFlushedFinalBlock);
// Flush (nops)
encryptStream.Flush();
encryptStream.FlushAsync().GetAwaiter().GetResult();
encryptStream.FlushFinalBlock();
Assert.Throws<NotSupportedException>(() => encryptStream.FlushFinalBlock());
Assert.True(encryptStream.HasFlushedFinalBlock);
Assert.True(stream.Length > 0);
}
// Read/decrypt using Read
stream = new MemoryStream(stream.ToArray()); // CryptoStream.Dispose disposes the stream
using (CryptoStream decryptStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read))
{
Assert.False(decryptStream.CanWrite);
Assert.True(decryptStream.CanRead);
Assert.False(decryptStream.CanSeek);
Assert.False(decryptStream.HasFlushedFinalBlock);
Assert.Throws<NotSupportedException>(() => decryptStream.SetLength(1));
Assert.Throws<NotSupportedException>(() => decryptStream.Length);
Assert.Throws<NotSupportedException>(() => decryptStream.Position);
Assert.Throws<NotSupportedException>(() => decryptStream.Position = 0);
Assert.Throws<NotSupportedException>(() => decryptStream.Seek(0, SeekOrigin.Begin));
Assert.Throws<NotSupportedException>(() => decryptStream.Write(new byte[0], 0, 0));
Assert.Throws<NullReferenceException>(() => decryptStream.Read(null, 0, 0)); // No arg validation on buffer?
Assert.Throws<ArgumentOutOfRangeException>(() => decryptStream.Read(new byte[0], -1, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => decryptStream.Read(new byte[0], 0, -1));
Assert.Throws<ArgumentOutOfRangeException>(() => decryptStream.Read(new byte[0], 0, -1));
Assert.Throws<ArgumentException>(() => decryptStream.Read(new byte[3], 1, 4));
using (StreamReader reader = new StreamReader(decryptStream))
{
Assert.Equal(
LoremText + LoremText + LoremText + LoremText,
reader.ReadToEnd());
}
}
// Read/decrypt using ReadToEnd
stream = new MemoryStream(stream.ToArray()); // CryptoStream.Dispose disposes the stream
using (CryptoStream decryptStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read))
using (StreamReader reader = new StreamReader(decryptStream))
{
Assert.Equal(
LoremText + LoremText + LoremText + LoremText,
reader.ReadToEndAsync().GetAwaiter().GetResult());
}
// Read/decrypt using a small buffer to force multiple calls to Read
stream = new MemoryStream(stream.ToArray()); // CryptoStream.Dispose disposes the stream
using (CryptoStream decryptStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read))
using (StreamReader reader = new StreamReader(decryptStream, Encoding.UTF8, true, bufferSize: 10))
{
Assert.Equal(
LoremText + LoremText + LoremText + LoremText,
reader.ReadToEndAsync().GetAwaiter().GetResult());
}
// Read/decrypt one byte at a time with ReadByte
stream = new MemoryStream(stream.ToArray()); // CryptoStream.Dispose disposes the stream
using (CryptoStream decryptStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read))
{
string expectedStr = LoremText + LoremText + LoremText + LoremText;
foreach (char c in expectedStr)
{
Assert.Equal(c, decryptStream.ReadByte()); // relies on LoremText being ASCII
}
Assert.Equal(-1, decryptStream.ReadByte());
}
}