public ProcessBlock ( byte inBytes, int inOff, int inLen ) : byte[] | ||
inBytes | byte | |
inOff | int | |
inLen | int | |
return | byte[] |
private void EncDec( string label, RsaKeyParameters pubParameters, RsaKeyParameters privParameters, byte[] seed, byte[] input, byte[] output) { IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine()); cipher.Init(true, new ParametersWithRandom(pubParameters, new VecRand(seed))); byte[] outBytes = cipher.ProcessBlock(input, 0, input.Length); for (int i = 0; i != output.Length; i++) { if (outBytes[i] != output[i]) { Fail(label + " failed encryption"); } } cipher.Init(false, privParameters); outBytes = cipher.ProcessBlock(output, 0, output.Length); for (int i = 0; i != input.Length; i++) { if (outBytes[i] != input[i]) { Fail(label + " failed decoding"); } } }
// signature public static string CreateSignature(string email, string password, RsaKeyParameters key) { byte[] prefix = { 0x00 }; var keyStruct = KeyToStruct(key); var toEncrypt = Encoding.UTF8.GetBytes(email + "\x00" + password); var cipher = new OaepEncoding(new RsaEngine(), new Sha1Digest(), null); cipher.Init(true, key); var encrypted = cipher.ProcessBlock(toEncrypt, 0, toEncrypt.Length); var digest = new Sha1Digest(); var hash = new byte[digest.GetByteLength()]; digest.BlockUpdate(keyStruct, 0, keyStruct.Length); digest.DoFinal(hash, 0); var hashExcerpt = hash.Take(4).ToArray(); return DataTypeUtils.UrlSafeBase64(DataTypeUtils.CombineBytes(prefix, hashExcerpt, encrypted)); }
private static string EncryptId() { var cipher = new OaepEncoding(new RsaEngine()); cipher.Init(true, CreateCipherParameters()); var decryptedId = Encoding.UTF8.GetBytes(Id); var encryptedId = cipher.ProcessBlock(decryptedId, 0, decryptedId.Length); return Convert.ToBase64String(encryptedId); }
private void doTestOaep(RsaKeyParameters pubParameters, RsaKeyParameters privParameters) { // // OAEP - public encrypt, private decrypt // IAsymmetricBlockCipher eng = new OaepEncoding(new RsaBlindedEngine()); byte[] data = Hex.Decode(input); eng.Init(true, pubParameters); try { data = eng.ProcessBlock(data, 0, data.Length); } catch (Exception e) { Fail("failed - exception " + e.ToString(), e); } eng.Init(false, privParameters); try { data = eng.ProcessBlock(data, 0, data.Length); } catch (Exception e) { Fail("failed - exception " + e.ToString(), e); } if (!input.Equals(Hex.ToHexString(data))) { Fail("failed OAEP Test"); } }
public ByteString ProceedMessageEncoding(AsymmetricKeyParameter messageKey, ByteString messageBytes) { Debug.Log("Proceeding with key IsPrivate={0} Message Size={1}.", messageKey.IsPrivate, messageBytes.Length); OaepEncoding cipherEncoder = new OaepEncoding(new RsaBlindedEngine(), new Sha256Digest()); cipherEncoder.Init(!(messageKey.IsPrivate), messageKey); using (MemoryStream outputStream = new MemoryStream()) using (Stream inputStream = new MemoryStream(messageBytes.ToByteArray())) { byte[] inputBlock = new byte[cipherEncoder.GetInputBlockSize()]; int readBytes = 0; while ((readBytes = inputStream.Read(inputBlock, 0, inputBlock.Length)) > 0) { byte[] outputBlock = cipherEncoder.ProcessBlock(inputBlock, 0, readBytes); outputStream.Write(outputBlock, 0, outputBlock.Length); if (readBytes != inputBlock.Length) break; } return ByteString.CopyFrom(outputStream.ToArray(), 0, (int)outputStream.Length); } }