Exemple #1
0
        public static byte[] RemovePadding(PaddingMode mode, byte[] bytes)
        {
            switch (mode)
            {
            case PaddingMode.None:
            case PaddingMode.Zeros:
                return(ByteUtilities.Clone(bytes));

            case PaddingMode.ANSIX923:
                return(RemoveAnsiX923Padding(bytes));

            case PaddingMode.ISO10126:
                return(RemoveIso10126Padding(bytes));

            case PaddingMode.PKCS7:
                return(RemovePkcs7Padding(bytes));

            default:
                throw new NotImplementedException("Padding mode not implemented");
            }
        }
Exemple #2
0
        public static byte[] ApplyPadding(PaddingMode mode, byte[] bytes, int blockSizeInBytes)
        {
            int paddingBytesNeeded = GetPaddingBytesNeeded(mode, bytes.Length, blockSizeInBytes);

            if (paddingBytesNeeded == 0)
            {
                // sanity check
                return(ByteUtilities.Clone(bytes));
            }

            byte[] output = new byte[blockSizeInBytes];
            Buffer.BlockCopy(bytes, 0, output, 0, bytes.Length);

            switch (mode)
            {
            case PaddingMode.ANSIX923:
                ApplyAnsiX923Padding(output, paddingBytesNeeded);
                break;

            case PaddingMode.ISO10126:
                ApplyIso10126Padding(output, paddingBytesNeeded);
                break;

            case PaddingMode.PKCS7:
                ApplyPkcs7Padding(output, paddingBytesNeeded);
                break;

            case PaddingMode.Zeros:
                // nop
                break;

            default:
                throw new NotImplementedException("Padding mode not implemented");
            }

            return(output);
        }