/** * Calculate the key to be used for RC4 and AES-128. * * @param objectNumber The data object number. * @param genNumber The data generation number. * @return the calculated key. */ private byte[] CalcFinalKey(long objectNumber, long genNumber) { byte[] newKey = new byte[encryptionKey.Length + 5]; Array.Copy(encryptionKey, 0, newKey, 0, encryptionKey.Length); // PDF 1.4 reference pg 73 // step 1 // we have the reference // step 2 newKey[newKey.Length - 5] = (byte)(objectNumber & 0xff); newKey[newKey.Length - 4] = (byte)(objectNumber >> 8 & 0xff); newKey[newKey.Length - 3] = (byte)(objectNumber >> 16 & 0xff); newKey[newKey.Length - 2] = (byte)(genNumber & 0xff); newKey[newKey.Length - 1] = (byte)(genNumber >> 8 & 0xff); // step 3 using (MD5 md = MD5.Create()) { md.Update(newKey); if (useAES) { md.Update(AES_SALT); } byte[] digestedKey = md.Digest(); // step 4 int Length = Math.Min(newKey.Length, 16); byte[] readonlyKey = new byte[Length]; Array.Copy(digestedKey, 0, readonlyKey, 0, Length); return(readonlyKey); } }
internal static DigitalSignature smethod_0(BinaryReader A_0, byte[] A_1) { bool flag; int num = 8; DigitalSignature signature = new DigitalSignature(DigitalSignatureType.CryptoApi); try { int num2 = A_0.ReadInt32(); int num3 = A_0.ReadInt32(); smethod_1(A_0); uint num4 = A_0.ReadUInt32(); uint num5 = A_0.ReadUInt32(); long fileTime = num4; fileTime = fileTime << 0x20; fileTime |= num5; signature.method_0(DateTime.FromFileTimeUtc(fileTime)); A_0.ReadInt32(); int count = A_0.ReadInt32(); int num8 = A_0.ReadInt32(); A_0.ReadInt32(); int num9 = A_0.ReadInt32(); int num10 = A_0.ReadInt32(); A_0.ReadInt32(); Class1029.smethod_5(A_0, (num2 + 1) * 2); Class1029.smethod_5(A_0, (num3 + 1) * 2); byte[] array = A_0.ReadBytes(count); byte[] rawData = A_0.ReadBytes(num8); A_0.ReadBytes(num9); A_0.ReadBytes(num10); if (num8 == 0) { throw new InvalidOperationException(BookmarkStart.b("稭堯圱䘳匵ᠷ匹伻ḽ⸿ⵁ摃╅ⵇ㡉㡋❍㙏㭑㝓㝕ⱗ㽙籛㭝ൟaţɥ౧ཀྵ乭ᙯᵱٳ噵婹ꪉヒﲑ鍊낝", num)); } X509Certificate2 certificate = new X509Certificate2(rawData); signature.method_3(certificate); RSAParameters parameters = ((RSACryptoServiceProvider)certificate.PublicKey.Key).ExportParameters(false); Array.Reverse(array); byte[] buffer3 = Class1078.smethod_0(array); Rsa rsa = new Rsa(parameters.Modulus, parameters.Exponent); byte[] sourceArray = Class1078.smethod_1(Class1078.smethod_2(rsa, buffer3), rsa.Modulus.method_0() >> 3); byte[] destinationArray = new byte[0x10]; Array.Copy(sourceArray, sourceArray.Length - 0x10, destinationArray, 0, 0x10); MD5 md = new MD5(); md.Update(A_1, A_1.Length); md.Update(BitConverter.GetBytes(num5), 4); md.Update(BitConverter.GetBytes(num4), 4); md.FinalUpdate(); flag = Class85.smethod_1(destinationArray, md.Digest); } catch (Exception) { flag = false; } signature.method_2(flag); return(signature); }
// Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. private static void MDTimeTrial() { Console.WriteLine("MD5 time trial. Digesting {0} {1}-byte blocks ...", TEST_BLOCK_LEN, TEST_BLOCK_COUNT); /* Initialize block */ byte[] block = new byte[TEST_BLOCK_LEN]; for (int i = 0; i < TEST_BLOCK_LEN; i++) { block[i] = (byte)(i & 0xff); } /* Start timer */ DateTime startTime = DateTime.Now; /* Digest blocks */ MD5 md5 = new MD5(); for (int i = 0; i < TEST_BLOCK_COUNT; i++) { md5.Update(block, TEST_BLOCK_LEN); } byte[] digest = md5.Final(); /* Stop timer */ DateTime endTime = DateTime.Now; TimeSpan time = endTime - startTime; Console.WriteLine("done"); Console.Write("Digest = "); MDPrint(digest); Console.WriteLine(); Console.WriteLine("Time = {0} seconds\n", time.TotalSeconds); Console.WriteLine("Speed = {0} bytes/second", (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT / time.TotalSeconds); }
internal static string Digest(string username, string password, string peername) { Encoding encoding = Encoding.GetEncoding("iso-8859-1"); if (encoding == null) { throw new SystemException("Cannot get iso-8859-1 encoding."); } byte[] usernameBytes = encoding.GetBytes(username == null ? "" : username); byte[] passwordBytes = encoding.GetBytes(password == null ? "" : password); byte[] peernameBytes = encoding.GetBytes(peername == null ? "" : peername); MD5 md5 = new MD5(); md5.Update(peernameBytes); md5.Update(usernameBytes); md5.Update(passwordBytes); byte[] digest = md5.Final(); return(encoding.GetString(digest)); }
private static void XX_Encrypt(byte[] thing, int offset, byte[] username) { lock (thePass) { if (thePass[0] == 'x') { String s1 = "7rLrT7iG3kWWLuSDYdS/KIXO8JF86h12KyCTG1Mh0qxWdSZ6ezHRST0UuGl6xkbMgsXj4+eZbXNyYijRmoaaJm+hQCWSOW+0OHGCnYWB4upxi0Fogdu0gb+q4VFzyUFknEpZPg=="; String s2 = "PCuJhpWX5eApg2mRs0bvSIdfwSDUa0kjiSdd76ORgXYyhtLbHm4Uq6afLbfROLi5pDpjKVS9Vr9aZo+F3IpyZ6Zn6m/Xf1PRtq3jdseJht4VSduxHrpocKVdRh3LixXKr6Ue6A=="; byte[] pass1 = Encoding.ASCII.GetBytes(s1); byte[] pass2 = Encoding.ASCII.GetBytes(s2); for (int inx = 0; inx < pass2.Length; inx++) { thePass[inx] = (byte)(pass1[inx] ^ pass2[inx]); if (thePass[inx] == 0) { thePass[inx] = pass1[inx]; } } thePass[136] = 0; } } MD5 md5 = new MD5(); if (username != null && username[0] != 0) { md5.Update(username); } md5.Update(thePass); byte[] md5Bytes = md5.Final(); for (int thingIndex = offset, md5Index = 0; thingIndex < thing.Length; thingIndex++, md5Index++) { thing[thingIndex] = (byte)(thing[thingIndex] ^ md5Bytes[md5Index % md5Bytes.Length]); } }
// Digests a string and prints the result. private static void MDString(string s) { System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("iso-8859-1"); if (encoding == null) { throw new SystemException("Cannot get iso-8859-1 encoding."); } byte[] bytes = encoding.GetBytes(s); MD5 md5 = new MD5(); md5.Update(bytes); byte[] digest = md5.Final(); Console.Write("MD5 (\"{0}\") = ", s); MDPrint(digest); Console.WriteLine(); }
public byte[] CreateKey() { var name_bytes = DatOpener.ToLowerAscii(Name); int name_length = name_bytes.Length; var md5 = new MD5(); Array.Reverse(name_bytes); var key = new byte[1024]; int key_pos = 0; for (int i = 0; i < 64; ++i) { int name_pos = i % name_length; md5.Update(name_bytes, name_pos, name_length - name_pos); md5.Final(); Buffer.BlockCopy(md5.State, 0, key, key_pos, 16); key_pos += 16; } return(key); }
static byte[] Hash(int hashAlgo, byte[] data) { switch (hashAlgo) { case SSL.MD5SHA1: byte[] hv = new byte[36]; MD5 md5 = new MD5(); SHA1 sha1 = new SHA1(); md5.Update(data); md5.DoFinal(hv, 0); sha1.Update(data); sha1.DoFinal(hv, 16); return(hv); case SSL.MD5: return(new MD5().Hash(data)); case SSL.SHA1: return(new SHA1().Hash(data)); case SSL.SHA224: return(new SHA224().Hash(data)); case SSL.SHA256: return(new SHA256().Hash(data)); case SSL.SHA384: return(new SHA384().Hash(data)); case SSL.SHA512: return(new SHA512().Hash(data)); default: throw new Exception("NYI"); } }
byte[] ParseServerKeyExchange(out ECCurve curve, IPublicKey pkey) { byte[] msg = ReadHandshakeMessageExpected( SSL.SERVER_KEY_EXCHANGE); if (msg.Length < 4) { throw new SSLException( "Invalid ServerKeyExchange message"); } if (msg[0] != 0x03) { throw new SSLException("Unsupported unnamed curve"); } curve = SSL.GetCurveByID(IO.Dec16be(msg, 1)); int plen = msg[3]; int off = 4; if (msg.Length - off < plen) { throw new SSLException( "Invalid ServerKeyExchange message"); } byte[] point = new byte[plen]; Array.Copy(msg, off, point, 0, plen); off += plen; int slen = off; int hashId, sigId; if (Version >= SSL.TLS12) { if (msg.Length - off < 2) { throw new SSLException( "Invalid ServerKeyExchange message"); } hashId = msg[off++]; if (hashId == 0) { throw new SSLException( "Invalid hash identifier"); } sigId = msg[off++]; } else { if (pkey is RSAPublicKey) { hashId = 0; sigId = 1; } else if (pkey is ECPublicKey) { hashId = 2; sigId = 3; } else { throw new SSLException( "Unsupported signature key type"); } } if (msg.Length - off < 2) { throw new SSLException( "Invalid ServerKeyExchange message"); } int sigLen = IO.Dec16be(msg, off); off += 2; if (sigLen != msg.Length - off) { throw new SSLException( "Invalid ServerKeyExchange message"); } byte[] sig = new byte[sigLen]; Array.Copy(msg, off, sig, 0, sigLen); byte[] hv; if (hashId == 0) { MD5 md5 = new MD5(); SHA1 sha1 = new SHA1(); md5.Update(clientRandom); md5.Update(serverRandom); md5.Update(msg, 0, slen); sha1.Update(clientRandom); sha1.Update(serverRandom); sha1.Update(msg, 0, slen); hv = new byte[36]; md5.DoFinal(hv, 0); sha1.DoFinal(hv, 16); } else { IDigest h = SSL.GetHashByID(hashId); h.Update(clientRandom); h.Update(serverRandom); h.Update(msg, 0, slen); hv = h.DoFinal(); } bool ok; if (sigId == 1) { RSAPublicKey rpk = pkey as RSAPublicKey; if (rpk == null) { throw new SSLException( "Wrong public key type for RSA"); } if (hashId == 0) { ok = RSA.VerifyND(rpk, hv, sig); } else { byte[] head1, head2; switch (hashId) { case 1: head1 = RSA.PKCS1_MD5; head2 = RSA.PKCS1_MD5_ALT; break; case 2: head1 = RSA.PKCS1_SHA1; head2 = RSA.PKCS1_SHA1_ALT; break; case 3: head1 = RSA.PKCS1_SHA224; head2 = RSA.PKCS1_SHA224_ALT; break; case 4: head1 = RSA.PKCS1_SHA256; head2 = RSA.PKCS1_SHA256_ALT; break; case 5: head1 = RSA.PKCS1_SHA384; head2 = RSA.PKCS1_SHA384_ALT; break; case 6: head1 = RSA.PKCS1_SHA512; head2 = RSA.PKCS1_SHA512_ALT; break; default: throw new SSLException( "Unsupported hash algorithm: " + hashId); } ok = RSA.Verify(rpk, head1, head2, hv, sig); } } else if (sigId == 3) { ECPublicKey epk = pkey as ECPublicKey; if (epk == null) { throw new SSLException( "Wrong public key type for ECDSA"); } ok = ECDSA.Verify(epk, hv, sig); } else { throw new SSLException( "Unsupported signature type: " + sigId); } if (!ok) { throw new SSLException( "Invalid signature on ServerKeyExchange"); } return(point); }
// upload public async Task <InputFile> UploadFile(string filename, Stream stream, FileUploadProcessHandler handler) { TLApi api = await session.GetFileSessionMain(); long fileId = Helpers.GenerateRandomLong(); MD5 hash = new MD5(); if (stream.Length < 128 * 1024) { handler(0.0f); byte[] data = new byte[stream.Length]; stream.Read(data, 0, (int)stream.Length); bool result = await api.upload_saveFilePart(fileId, 0, data); //while(result != true) { // result = await api.upload_saveFilePart(fileId, 0, data); //} hash.Update(data); handler(1.0f); return(TL.inputFile(fileId, 1, filename, hash.FinalString())); } bool big = stream.Length > 10 * 1024 * 1024; float allStreamLength = stream.Length; int chunkSize = 128 * 1024; int chunkCount = (int)(stream.Length / chunkSize); int lastChunkSize = (int)(stream.Length - chunkSize * chunkCount); int allChunksCount = chunkCount + (lastChunkSize != 0 ? 1 : 0); for (int i = 0; i < chunkCount; i++) { handler((float)i * (float)chunkSize / allStreamLength); byte[] data = new byte[chunkSize]; stream.Read(data, 0, chunkSize); bool result = big ? await api.upload_saveBigFilePart(fileId, i, allChunksCount, data) : await api.upload_saveFilePart(fileId, i, data); //while(result != true) { // result = await api.upload_saveFilePart(fileId, i, data); //} hash.Update(data); } if (lastChunkSize != 0) { handler((float)chunkCount * (float)chunkSize / allStreamLength); byte[] lastChunkData = new byte[lastChunkSize]; stream.Read(lastChunkData, 0, lastChunkSize); bool lastChunkResult = big ? await api.upload_saveBigFilePart(fileId, chunkCount, allChunksCount, lastChunkData) : await api.upload_saveFilePart(fileId, chunkCount, lastChunkData); //while(lastChunkResult != true) { // lastChunkResult = await api.upload_saveFilePart(fileId, chunkCount, lastChunkData); //} hash.Update(lastChunkData); } handler(1.0f); return(TL.inputFile(fileId, allChunksCount, filename, hash.FinalString())); }
// Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. private static void MDTimeTrial () { Console.WriteLine ("MD5 time trial. Digesting {0} {1}-byte blocks ...", TEST_BLOCK_LEN, TEST_BLOCK_COUNT); /* Initialize block */ byte[] block = new byte[TEST_BLOCK_LEN]; for (int i = 0; i < TEST_BLOCK_LEN; i++) block[i] = (byte) (i & 0xff); /* Start timer */ DateTime startTime = DateTime.Now; /* Digest blocks */ MD5 md5 = new MD5 (); for (int i = 0; i < TEST_BLOCK_COUNT; i++) md5.Update (block, TEST_BLOCK_LEN); byte[] digest = md5.Final (); /* Stop timer */ DateTime endTime = DateTime.Now; TimeSpan time = endTime - startTime; Console.WriteLine ("done"); Console.Write ("Digest = "); MDPrint (digest); Console.WriteLine (); Console.WriteLine ("Time = {0} seconds\n", time.TotalSeconds); Console.WriteLine ("Speed = {0} bytes/second", (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / time.TotalSeconds); }
// Digests a string and prints the result. private static void MDString (string s) { System.Text.Encoding encoding = System.Text.Encoding.GetEncoding ("iso-8859-1"); if (encoding == null) throw new SystemException ("Cannot get iso-8859-1 encoding."); byte[] bytes = encoding.GetBytes (s); MD5 md5 = new MD5 (); md5.Update (bytes); byte[] digest = md5.Final (); Console.Write ("MD5 (\"{0}\") = ", s); MDPrint (digest); Console.WriteLine (); }