|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | +using System.Security.Cryptography; |
| 4 | + |
| 5 | +namespace SafeCrypt |
| 6 | +{ |
| 7 | + public class Encrypt : BaseAesEncryption |
| 8 | + { |
| 9 | + public byte[] AesEncrypt(byte[] data, byte[] secretKey, byte[] iv) |
| 10 | + => EncryptAES(data, secretKey, iv); |
| 11 | + |
| 12 | + public byte[] AesEncrypt(string data, string secretKey, string iv) |
| 13 | + { |
| 14 | + NullChecks(data, secretKey, iv); |
| 15 | + |
| 16 | + var aesKey = Encoding.UTF8.GetBytes(secretKey); |
| 17 | + var aesIv = Encoding.UTF8.GetBytes(iv); |
| 18 | + var aesData = data.HexadecimalStringToByteArray(); |
| 19 | + |
| 20 | + return EncryptAES(aesData, aesKey, aesIv); |
| 21 | + } |
| 22 | + |
| 23 | + public string AesEncryptByteToHexString(byte[] data, byte[] secretKey, byte[] iv) |
| 24 | + { |
| 25 | + var cipherText = EncryptAES(data, secretKey, iv); |
| 26 | + |
| 27 | + return cipherText.ByteArrayToHexString(); |
| 28 | + } |
| 29 | + |
| 30 | + public string AesEncryptByteToBase64String(byte[] data, byte[] secretKey, byte[] iv) |
| 31 | + { |
| 32 | + var cipherText = EncryptAES(data, secretKey, iv); |
| 33 | + |
| 34 | + return Convert.ToBase64String(cipherText); |
| 35 | + } |
| 36 | + |
| 37 | + public string AesEncryptByteToString(byte[] data, byte[] secretKey, byte[] iv) |
| 38 | + { |
| 39 | + var cipherText = EncryptAES(data, secretKey, iv); |
| 40 | + |
| 41 | + return cipherText.BytesToString(); |
| 42 | + } |
| 43 | + |
| 44 | + private void NullChecks(string data, string secretKey, string iv) |
| 45 | + { |
| 46 | + if (data == null || data.Length <= 0) |
| 47 | + throw new ArgumentNullException("plainText"); |
| 48 | + |
| 49 | + if (secretKey == null || secretKey.Length <= 0) |
| 50 | + throw new ArgumentNullException("Key"); |
| 51 | + |
| 52 | + if (iv == null || iv.Length <= 0) |
| 53 | + throw new ArgumentNullException("IV"); |
| 54 | + } |
| 55 | + |
| 56 | + //public byte[] AesEncrypt(byte[] data, byte[] key, byte[] iv, ReturnType returnType) |
| 57 | + //{ |
| 58 | + |
| 59 | + //} |
| 60 | + } |
| 61 | +} |
0 commit comments