} //private byte[] encryptDesEde(byte[] plain) private byte[] encryptAES(byte[] plain, KeyChaining chaining = KeyChaining.CBC, bool doEncrypt = true, byte[] icv = null) { BufferedBlockCipher cipher = chaining == KeyChaining.CBC ? new BufferedBlockCipher(new CbcBlockCipher(new AesEngine())) //CBC chaining : new BufferedBlockCipher(new AesEngine()); //ECB chaining if (icv != null) { cipher.Init(doEncrypt, new ParametersWithIV(new KeyParameter(theKey), icv)); } else { cipher.Init(doEncrypt, new KeyParameter(theKey)); } MemoryStream dst = new MemoryStream(); byte[] bin = padded(plain, 24); byte[] result = new byte[bin.Length]; int outL = cipher.ProcessBytes(bin, result, 0); if (outL > 0) { dst.Write(result, 0, outL); } if (outL < plain.Length) { outL = cipher.DoFinal(result, 0); if (outL > 0) { dst.Write(result, 0, outL); } } //if (outL < plain.Length) dst.Position = 0; result = dst.ToArray(); dst.Close(); if (result.Length > plain.Length) { byte[] res = new byte[plain.Length]; System.Array.Copy(result, res, plain.Length); return(res); } //if (result.Length > plain.Length) return(result); } //private byte[] encryptAES(byte[] plain)
} //byte[] IKey.Decrypt( ... byte[] IKey.Decrypt(byte[] crypto, byte[] icv, KeyChaining chaining /* = KeyChaining.CBC*/) { byte[] result = new byte[0]; if (Initialized) { switch (_KeyType) { case keyType.DESede: result = encryptDesEde(crypto, chaining, false, icv); break; case keyType.AES: result = encryptAES(crypto, chaining, false, icv); break; } //switch (_KeyType) } return(result); }
IKey IKey.DeriveKey(byte[] derivationData, KeyChaining chaining) { IKey result = null; if (Initialized) { byte[] derivedKey = null; switch (_KeyType) { case keyType.DESede: derivedKey = encryptDesEde(derivationData, chaining); break; case keyType.AES: derivedKey = encryptAES(derivationData, chaining); break; } //switch(_KeyType) if (derivedKey != null && derivedKey.Length == _Length) { result = new IKeyImpl(); result.Name = "Derived from " + _Name; result.Purpose = _Purpose; result.Subject = _Subject; result.Scope = _Scope; result.keyType = _KeyType; result.Length = _Length; result.KeyChaining = _KeyChaining; result.keyValue = Hex.ToHexString(derivedKey); result.kcv = Hex.ToHexString(result.Encrypt(Hex.Decode("000000"))); } //if (derivedKey != null && derivedKey.Length == theKey.Length) } //if (Initialized) return(result); } //IKey IKey.DeriveKey( ...
byte[] IKey.Decrypt(byte[] crypto, KeyChaining chaining) { return((this as IKey).Decrypt(crypto, null, _KeyChaining)); } //byte[] IKey.Decrypt( ...
byte[] IKey.Encrypt(byte[] plain, KeyChaining chaining) { return((this as IKey).Encrypt(plain, null, chaining)); } //byte[] IKey.Encrypt(byte[] plain)