public static void Encrypt(byte[] source) { if (!GetPublicKey()) { return; } StringBuilder bits = new StringBuilder(source.ToBitsString()); StringBuilder result = new StringBuilder(); int rsaBlockSize = blockSize; int addBitsCount = rsaBlockSize - (bits.Length % rsaBlockSize); bits.Append(Extensions.GetRandomString(addBitsCount)); string add = Convert.ToString(addBitsCount, 2); bits.Append(add.PadLeftRandom(blockSize)); for (int i = 0; i < bits.Length; i += rsaBlockSize) { string blockMessage = bits.ToString(i, rsaBlockSize); result.Append(EncryptBlock(blockMessage)); } byte[] encrypted = result.ToString().ToBytes().ToArray(); CustomFile.WriteAllBytes(encrypted, "C:\\Users\\vlady\\Desktop\\result.txt"); }
public static void Decrypt(byte[] source) { if (source.Length <= TRASH_BLOCK) { ShowErrorMessage(); return; } if (!GetPrivateKey()) { return; } StringBuilder result = new StringBuilder(); int rsaBlockSize = Generate.KEY_LENGT_BITS; StringBuilder bits = new StringBuilder(source.ToBitsString()); if (bits.Length % rsaBlockSize != 0) { ShowErrorMessage(); return; } for (int i = 0; i < bits.Length; i += rsaBlockSize) { string blockMessage = bits.ToString(i, rsaBlockSize); result.Append(DecryptBlock(blockMessage)); } int tempSize = (int)(result.ToString().Substring(result.Length - 16).ToBigInteger()); int addBitsCount = tempSize + blockSize; string resultString = result.ToString().Substring(0, result.Length - addBitsCount); CustomFile.WriteAllBytes(resultString.ToBytes()); }