×
Log in
Twitter
Gmail
Facebook
.NET Fiddle
and
.NET Academy
shared account
Remember me
Log in
Sign up
or
Reset password
Search Tutorials
Log in
Sign up
About
Twitter
.NET Fiddle
Support
Contact Us
Symmetric Cryptography
Exercise: Decrypt
< Previous
Next >
Please create function for decrypt message and output to Console plain text
using System; using System.Linq; using System.Text; public class Program { public const int BLOCK_SIZE = 4; public const UInt16 SECRET = 65535; public const int ROUNDS = 6; public static void Main() { var blocks = new uint[] {1074632793,2393460436,1820682509,53228064,3452521679,3660823688,1296079281,1025549298,19687968,721445875,3202418118,3721585831,78447188,1211095104,422738656,271553040,3111137266,152305785,239110881,3170940915,2657138408,1661319489,1817774531,195557908,1778915809,1447756419,2856347641,3380626671,1875413487,411273861,420747897,897633263,4210034662,2869831457,3532921957,67406433,329509508,420747897,897633263,3068331398,606405456,1035516916,3600889486}; var key = "1234"; var result = Feistel(blocks, key); var cipherText = Encoding.ASCII.GetString(result.SelectMany(r => BitConverter.GetBytes(r).Reverse()).ToArray()); Console.Write(cipherText); } public static uint[] Feistel(uint[] plainText, string password) { var cipherBlocks = new uint[plainText.Count()]; for (int i = 0; i < plainText.Count(); i++) { cipherBlocks[i] = F(plainText[i], password); } return cipherBlocks; } public static uint F(uint originalBlock, string key) { var bytes = GetBytes(originalBlock); UInt16 leftPart = ToUInt16(bytes.Take(2).ToArray()); UInt16 rightPart = ToUInt16(bytes.Skip(2).Take(2).ToArray()); var keyBytes = Encoding.ASCII.GetBytes(key); var key1 = ToUInt16(keyBytes.Take(2).ToArray().ToArray()); var key2 = ToUInt16(keyBytes.Skip(2).Take(2).ToArray().ToArray()); for (int i = 0; i < ROUNDS; i++) { var f = RoundF(leftPart, i % 2 == 0 ? key1 : key2); UInt16 result = (UInt16) (f ^ rightPart); if (i < ROUNDS - 1) { rightPart = leftPart; leftPart = result; } else { rightPart = result; } } var cipherBytes = new byte[BLOCK_SIZE] { BitConverter.GetBytes(leftPart)[1], BitConverter.GetBytes(leftPart)[0], BitConverter.GetBytes(rightPart)[1],BitConverter.GetBytes(rightPart)[0] }; return ToUInt32(cipherBytes); } public static UInt16 RoundF(UInt16 originalBlock, UInt16 key) { UInt16 result = (UInt16) (originalBlock ^ key); result = (UInt16) (result << 5); return result; } public static UInt16 ToUInt16(byte[] bytes) { UInt16 result = 0; result += (ushort) (bytes[0]*256 + bytes[1]); return result; } public static UInt32 ToUInt32(byte[] bytes) { UInt32 result = 0; for (int i = 0; i < 4; i++) { result += (UInt32) Math.Pow(256, 3 - i)*bytes[i]; } return result; } public static uint[] GetBlocks(string text) { var bytes = Encoding.ASCII.GetBytes(text); var blocksCount = (int)Math.Ceiling(bytes.Count() / (double)BLOCK_SIZE); var result = new uint[blocksCount]; for (int i = 0; i < blocksCount; i++) { result[i] = ToUInt32(bytes.Skip(i * BLOCK_SIZE).Take(BLOCK_SIZE).ToArray()); } return result; } public static byte[] GetBytes(UInt32 originalBlock) { return BitConverter.GetBytes(originalBlock).Reverse().ToArray(); } }
Reset
Verify
Page 9 of 11
< Previous
Next >
Loading packages and dependencies