fork(1) download
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.IO;
  4.  
  5. public class Test
  6. {
  7. static Int64 Key;
  8.  
  9. static Random Rand = new Random();
  10.  
  11. public static byte[] Encrypt(byte[] ClearData, byte[] Key)
  12. {
  13. MemoryStream ms = new MemoryStream();
  14.  
  15. Rijndael alg = Rijndael.Create();
  16. alg.KeySize = 128;
  17. alg.BlockSize = 128;
  18. alg.Mode = CipherMode.ECB;
  19.  
  20. alg.Key = Key;
  21.  
  22. CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
  23.  
  24. cs.Write(ClearData, 0, ClearData.Length);
  25.  
  26. cs.Close();
  27.  
  28. byte[] encryptedData = ms.ToArray();
  29.  
  30. return encryptedData;
  31. }
  32.  
  33. public static int RandomValue(int Max)
  34. {
  35. byte[] FirstHalf = BitConverter.GetBytes(Key);
  36. byte[] SecondHalf = BitConverter.GetBytes(Key);
  37.  
  38. byte[] KeyBytes = new Byte[FirstHalf.Length + SecondHalf.Length];
  39. FirstHalf.CopyTo(KeyBytes, 0);
  40. SecondHalf.CopyTo(KeyBytes, FirstHalf.Length);
  41.  
  42. byte[] RandValue = Encrypt(new byte[0], KeyBytes);
  43.  
  44. int Rand = Math.Abs(BitConverter.ToInt32(RandValue, 0));
  45.  
  46. return Rand % Max;
  47. }
  48.  
  49. public static int NextValue(int Max)
  50. {
  51. ++Key;
  52. return RandomValue(Max);
  53. }
  54.  
  55. public static int PreviousValue(int Max)
  56. {
  57. --Key;
  58. return RandomValue(Max);
  59. }
  60.  
  61. public static void Main()
  62. {
  63. //Init key
  64. byte[] buffer = new byte[sizeof(Int64)];
  65. Rand.NextBytes(buffer);
  66. Key = BitConverter.ToInt64(buffer, 0);
  67.  
  68. for(int i = 0 ; i < 4 ; ++i)
  69. {
  70. Console.WriteLine(NextValue(10));
  71. }
  72.  
  73. Console.WriteLine();
  74.  
  75. for(int i = 0 ; i < 4 ; ++i)
  76. {
  77. Console.WriteLine(PreviousValue(10));
  78. }
  79. }
  80. }
Success #stdin #stdout 0.08s 33944KB
stdin
Standard input is empty
stdout
8
1
0
8

0
1
8
7