private static AsymmetricCipherKeyPair GenerateKeyPair(int bits) { KeyGenerationParameters keyParameters = new KeyGenerationParameters(BouncyCastleRandomGenerator.CreateSecureRandom(), bits); RsaKeyPairGenerator rsaGenerator = new RsaKeyPairGenerator(); rsaGenerator.Init(keyParameters); AsymmetricCipherKeyPair keyPair = rsaGenerator.GenerateKeyPair(); return(keyPair); }
public byte[] Transform(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException("buffer"); } int rsaKeyBitLength = ((RsaKeyParameters)Key).Modulus.BitLength; IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaBlindedEngine(), new BouncyCastleDigest(New <IAsymmetricFactory>().CreatePaddingHash(rsaKeyBitLength))); cipher.Init(false, new ParametersWithRandom(Key, BouncyCastleRandomGenerator.CreateSecureRandom())); return(TransformInternal(buffer, cipher)); }
public byte[] Transform(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException("buffer"); } int rsaKeyBitSize = ((RsaKeyParameters)Key).Modulus.BitLength; ICryptoHash paddingHash = New <IAsymmetricFactory>().CreatePaddingHash(rsaKeyBitSize); int requiredBits = (paddingHash.HashSize * 2 + buffer.Length + 1) * 8; if (requiredBits > rsaKeyBitSize) { throw new InvalidOperationException("The RSA Key size is too small to fit the data + 1 + 2 times the padding hash size."); } IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaBlindedEngine(), new BouncyCastleDigest(paddingHash)); cipher.Init(true, new ParametersWithRandom(Key, BouncyCastleRandomGenerator.CreateSecureRandom())); return(TransformInternal(buffer, cipher)); }