private void CipherBlocks(List <byte[]> blocks) { for (int i = 0; i < blocks.Count; i++) { BigInteger nonceCounterXor = InitializationVector ^ new BigInteger(i); byte[] nonceCounterXorBytes = rsa.Encrypt(BigIntegerExtensions.UnsignedToBytes(nonceCounterXor)); nonceCounterXor = BigIntegerExtensions.UnsignedFromBytes(nonceCounterXorBytes); BigInteger block = BigIntegerExtensions.UnsignedFromBytes(blocks[i]); block ^= nonceCounterXor; RSAParameters parameters = rsa.ExportParameters(); int keySize = parameters.Modulus.Length; blocks[i] = BlockCipherSupport.PadWithZeros(BigIntegerExtensions.UnsignedToBytes(block), keySize); } }
public void Is_Equals_Result() { //Arrange byte[] message = Encoding.UTF8.GetBytes("message"); string SK = "<RSAKeyValue><Modulus>rQaN8F5SI3HZ0lR3gEY7RoDGFx/hOwYaGH0cTiy1/je41yE2TI" + "RSlEgme22dJ9Sn8Q9C8V9Wg6mZ6IyBBPoED/lcdP6m7fFBAwCVzZ+TYcwdkC8vGw0YfWON0ya31bhgAt" + "q4fDgfV1271RGelYzapx9kDJd+Dj/LxBE0NHXxjAk=</Modulus><Exponent>AQAB</Exponent><P>" + "5rGNQDfyrSaNczuDtV9p+NxJ5/NxQaiqbkdGAViwuvki6ffKCeWTMGwWU+kNlEkZm5jh+pApXBLE4qvx" + "AtrLnw==</P><Q>wAGLDZdcQ4xs3DAYSrW98EKIWPlNBoQ5ZXnLh/xVNbLAUzpqn0IcgAGcoYqQNR51v" + "c/COFpzd+i2RiAiD9sHVw==</Q><DP>DPmTfjcnWQHAFukUAVF6flq1dWxFxHGeFFHB6DV6yylUA2DCZ" + "kgZPTH3F4UWFG8AF7ZDj3ooOVt841rUVVrE9Q==</DP><DQ>BzaHoMUU5Dy8QjFUWEonjoURVjZXXG1P" + "Mq62pK8oDFJgwz+ojb8QDwcAeVkZPcWdKrpJU5CiUdjeMBg471uNFQ==</DQ><InverseQ>arL2Yjq/7" + "3RofGP310n5B0QxrqqiE9zP20AZ1lZCYFgyF++2iZ2kNgq0QO/fLRAO8QrFmSQXTS2Imsa/AdAuIQ==<" + "/InverseQ><D>W6QoE1JxoLJRUxRHwtnv1TSpNmA9M7zUn3nMPx9xOPccYF0H3FkNP9pC/4acAReh54x" + "UwXDkapGuobuhLhiZg1iGuUA0w9NQZwwms+esvE4c7E3AOlBWFJxUL4wDZNd58HDHSSlQxC+HPYu7ukR" + "7tfC3aqAyc5VN2P+2/1AGts0=</D></RSAKeyValue>"; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(SK); //Act var cipherbytes = serviceMock.Encrypt(message, rsa.ExportParameters(false)); var plainbytes = serviceMock.Decrypt(cipherbytes, rsa.ExportParameters(true)); //Assert Assert.IsNotNull(cipherbytes); Assert.IsInstanceOfType(cipherbytes, typeof(byte[])); Assert.IsInstanceOfType(plainbytes, typeof(byte[])); CollectionAssert.AreEqual(message, plainbytes); }
private List <byte[]> CipherBlocks(List <byte[]> blocks) { List <byte[]> cipheredBlocks = new List <byte[]>(blocks.Count); byte[] beforeXor = BigIntegerExtensions.UnsignedToBytes(InitializationVector); for (int i = 0; i < blocks.Count; i++) { cipheredBlocks.Add(rsa.Encrypt(beforeXor.Take(BlockSize).ToArray())); BigInteger beforeXorBlock = BigIntegerExtensions.UnsignedFromBytes(beforeXor); BigInteger block = BigIntegerExtensions.UnsignedFromBytes(blocks[i]); beforeXor = cipheredBlocks[i]; cipheredBlocks[i] = XorWithKeySizePadding(beforeXorBlock, block); } return(cipheredBlocks); }
public byte[] Cipher(byte[] data) { byte[] dataToDivide = BlockCipherSupport.AddPadding(data, BlockSize); List <byte[]> blocks = BlockCipherSupport.DivideIntoBlocks(dataToDivide, BlockSize); for (int i = 0; i < blocks.Count; i++) { blocks[i] = rsa.Encrypt(blocks[i]); } return(BlockCipherSupport.ConcatenateBlocks(blocks)); }
private void CipherBlocks(List <byte[]> blocks) { for (int i = 0; i < blocks.Count; i++) { BigInteger cipheredPreviousBlock = i > 0 ? BigIntegerExtensions.UnsignedFromBytes(blocks[i - 1].Take(BlockSize).ToArray()) : InitializationVector; BigInteger block = BigIntegerExtensions.UnsignedFromBytes(blocks[i]); block ^= cipheredPreviousBlock; blocks[i] = BigIntegerExtensions.UnsignedToBytes(block); blocks[i] = rsa.Encrypt(blocks[i]); } }
private List <byte[]> CipherBlocks(List <byte[]> blocks) { List <byte[]> cipheredBlocks = new List <byte[]>(blocks.Count); for (int i = 0; i < blocks.Count; i++) { var previousXor = CalculatePreviousXor(blocks, i, cipheredBlocks); var block = BigIntegerExtensions.UnsignedFromBytes(blocks[i]); block ^= previousXor; cipheredBlocks.Add(rsa.Encrypt(BigIntegerExtensions.UnsignedToBytes(block).Take(BlockSize).ToArray())); } return(cipheredBlocks); }
private List <byte[]> CipherBlocks(List <byte[]> blocks) { List <byte[]> cipheredBlocks = new List <byte[]>(blocks.Count); for (int i = 0; i < blocks.Count; i++) { byte[] cipheredPreviousBlock = GetCipheredPreviousBlock(cipheredBlocks, i); BigInteger cipheredAgainPreviousBlock = BigIntegerExtensions.UnsignedFromBytes(rsa.Encrypt(cipheredPreviousBlock)); BigInteger block = BigIntegerExtensions.UnsignedFromBytes(blocks[i]); byte[] xorResult = XorKeySizePadding(cipheredAgainPreviousBlock, block); cipheredBlocks.Add(xorResult); } return(cipheredBlocks); }