public static byte[] Pkcs1Unpad(byte[] data, int length, PKCS1PadType type) { int offset = 0; while (offset < data.Length && data[offset] == 0) { ++offset; } if (data.Length - offset != length - 1 || data[offset] != ((byte)type + 1)) { throw new Exception(string.Format("PKCS#1 UNPAD: Offset={0}, expected Data[Offset]==[{1}]; Got Data[Offset]={2}", offset, type + 1, data[offset])); } ++offset; while (data[offset] != 0) { if (++offset >= data.Length) { throw new Exception(string.Format("PKCS#1 UNPAD: Offset={0}, Data[Offset - 1] != 0 (={1:X})", offset, data[offset - 1])); } } var outCome = new byte[(data.Length - offset) - 1]; for (int j = 0; ++offset < data.Length; j++) { outCome[j] = data[offset]; } return(outCome); }
public static byte[] Pkcs1Pad(byte[] data, int length, PKCS1PadType type) { var outCome = new byte[length]; for (int i = data.Length - 1; (i >= 0 && length > 11);) { outCome[--length] = data[i--]; } outCome[--length] = 0; while (length > 2) { byte x = (type == PKCS1PadType.RandomByte) ? (byte)ByteGen.Next(1, 256) : byte.MaxValue; outCome[--length] = x; } outCome[--length] = (byte)(type + 1); outCome[--length] = 0; return(outCome); }
private void _Encrypt(Func <BigInteger, BigInteger> doFunc, ref byte[] data, PKCS1PadType type) { int blockSize = GetBlockSize(); data = doFunc(new BigInteger(Pkcs1Pad(data, blockSize, type))).ToBytes(); }