| 1 | using Microsoft.Data.Sqlite; |
| 2 | using System.Security.Cryptography; |
| 3 | using System.Text; |
| 4 | |
| 5 | namespace SimPas2_Windows.Managers |
| 6 | { |
| 7 | internal class CreditcardManager |
| 8 | { |
| 9 | private readonly string mConnectionString; |
| 10 | private readonly byte[] mEncryptionKey; |
| 11 | private readonly string mCulture; |
| 12 | |
| 13 | private string mJpLang; |
| 14 | |
| 15 | public CreditcardManager(string databasePath, byte[] encryptionKey, string culture) |
| 16 | { |
| 17 | mConnectionString = $"Data Source={databasePath}"; |
| 18 | mEncryptionKey = encryptionKey ?? throw new ArgumentNullException(nameof(encryptionKey)); |
| 19 | mCulture = culture; |
| 20 | |
| 21 | mJpLang = "ja-JP"; |
| 22 | } |
| 23 | |
| 24 | private bool AlreadyExists(string cardnumber, int? excludeId = null) |
| 25 | { |
| 26 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 27 | { |
| 28 | conn.Open(); |
| 29 | SqliteCommand com = conn.CreateCommand(); |
| 30 | com.CommandText = @" |
| 31 | SELECT COUNT(*) FROM Cc |
| 32 | WHERE UPPER(Cardnumber) = UPPER(@cardnumber)"; |
| 33 | com.Parameters.AddWithValue("@cardnumber", cardnumber); |
| 34 | |
| 35 | if (excludeId.HasValue) |
| 36 | { |
| 37 | com.CommandText += " AND Id != @excludeId"; |
| 38 | com.Parameters.AddWithValue("@excludeId", excludeId.Value); |
| 39 | } |
| 40 | |
| 41 | return Convert.ToInt32(com.ExecuteScalar()) > 0; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public void AddCc(string brand, string cardnumber, string expiration, string cvc, string holdername, string note) |
| 46 | { |
| 47 | if (string.IsNullOrWhiteSpace(brand) || string.IsNullOrWhiteSpace(cardnumber) || string.IsNullOrWhiteSpace(expiration) || string.IsNullOrWhiteSpace(cvc) || string.IsNullOrWhiteSpace(holdername)) |
| 48 | { |
| 49 | string err = mCulture == mJpLang |
| 50 | ? "ブランド、カード番号、氏名、有効期限及び、CVCを御入力下さい。" |
| 51 | : "Please fill in the brand, card number, full name, expiration, and CVC."; |
| 52 | throw new ArgumentException(err); |
| 53 | } |
| 54 | |
| 55 | string encryptedCvc = EncryptCvc(cvc); |
| 56 | |
| 57 | if (AlreadyExists(cardnumber)) |
| 58 | { |
| 59 | string err = mCulture == mJpLang |
| 60 | ? $"カード「{brand}/{holdername} ({expiration})」は既に存在します。" |
| 61 | : $"The card '{brand}/{holdername} ({expiration})' already exists."; |
| 62 | throw new ArgumentException(err); |
| 63 | } |
| 64 | |
| 65 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 66 | { |
| 67 | conn.Open(); |
| 68 | SqliteCommand com = conn.CreateCommand(); |
| 69 | com.CommandText = @" |
| 70 | INSERT INTO Cc (Brand, Cardnumber, Expiration, Cvc, Holdername, Note) |
| 71 | VALUES ($brand, $cardnumber, $expiration, $cvc, $holdername, $note)"; |
| 72 | com.Parameters.AddWithValue("$brand", brand); |
| 73 | com.Parameters.AddWithValue("$cardnumber", cardnumber); |
| 74 | com.Parameters.AddWithValue("$expiration", expiration); |
| 75 | com.Parameters.AddWithValue("$cvc", encryptedCvc); |
| 76 | com.Parameters.AddWithValue("$holdername", holdername); |
| 77 | com.Parameters.AddWithValue("$note", string.IsNullOrEmpty(note) ? DBNull.Value : note); |
| 78 | com.ExecuteNonQuery(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public bool EditCc(int id, string brand, string cardnumber, string expiration, string cvc, string holdername, string note) |
| 83 | { |
| 84 | if (string.IsNullOrWhiteSpace(brand) || string.IsNullOrWhiteSpace(cardnumber) || string.IsNullOrWhiteSpace(expiration) || string.IsNullOrWhiteSpace(cvc) || string.IsNullOrWhiteSpace(holdername)) |
| 85 | { |
| 86 | string err = mCulture == mJpLang |
| 87 | ? "ブランド、カード番号、氏名、有効期限及び、CVCを御入力下さい。" |
| 88 | : "Please fill in the brand, card number, full name, expiration, and CVC."; |
| 89 | throw new ArgumentException(err); |
| 90 | } |
| 91 | |
| 92 | string encryptedCvc = EncryptCvc(cvc); |
| 93 | |
| 94 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 95 | { |
| 96 | conn.Open(); |
| 97 | SqliteCommand com = conn.CreateCommand(); |
| 98 | com.CommandText = @" |
| 99 | UPDATE Cc |
| 100 | SET Brand = $brand, Cardnumber = $cardnumber, Expiration = $expiration, Cvc = $cvc, Holdername = $holdername, Note = $note |
| 101 | WHERE Id = $id"; |
| 102 | com.Parameters.AddWithValue("$id", id); |
| 103 | com.Parameters.AddWithValue("$brand", brand); |
| 104 | com.Parameters.AddWithValue("$cardnumber", cardnumber); |
| 105 | com.Parameters.AddWithValue("$expiration", expiration); |
| 106 | com.Parameters.AddWithValue("$cvc", encryptedCvc); |
| 107 | com.Parameters.AddWithValue("$holdername", holdername); |
| 108 | com.Parameters.AddWithValue("$note", string.IsNullOrEmpty(note) ? DBNull.Value : note); |
| 109 | return com.ExecuteNonQuery() > 0; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public bool DeleteCc(int id) |
| 114 | { |
| 115 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 116 | { |
| 117 | conn.Open(); |
| 118 | SqliteCommand com = conn.CreateCommand(); |
| 119 | com.CommandText = "DELETE FROM Cc WHERE Id = $id"; |
| 120 | com.Parameters.AddWithValue("$id", id); |
| 121 | return com.ExecuteNonQuery() > 0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | public List<(int Id, string Brand, string Cardnumber, string Expiration, string Cvc, string Holdername, string Note)> GetAll(string keyword = "") |
| 126 | { |
| 127 | var ccs = new List<(int, string, string, string, string, string, string)>(); |
| 128 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 129 | { |
| 130 | conn.Open(); |
| 131 | SqliteCommand com = conn.CreateCommand(); |
| 132 | if (string.IsNullOrWhiteSpace(keyword)) |
| 133 | { |
| 134 | com.CommandText = @" |
| 135 | SELECT Id, Brand, Cardnumber, Expiration, Cvc, Holdername, Note FROM Cc |
| 136 | ORDER BY Brand ASC"; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | com.CommandText = @" |
| 141 | SELECT Id, Brand, Cardnumber, Expiration, Cvc, Holdername, Note FROM Cc |
| 142 | WHERE Brand LIKE @keyword OR Holdername LIKE @keyword OR Cardnumber LIKE @keyword |
| 143 | ORDER BY Brand ASC"; |
| 144 | com.Parameters.AddWithValue("@keyword", $"%{keyword}%"); |
| 145 | } |
| 146 | |
| 147 | using (SqliteDataReader reader = com.ExecuteReader()) |
| 148 | { |
| 149 | while (reader.Read()) |
| 150 | { |
| 151 | string encryptedCvc = reader.GetString(4); |
| 152 | string decryptedCvc = DecryptCvc(encryptedCvc); |
| 153 | ccs.Add(( |
| 154 | reader.GetInt32(0), |
| 155 | reader.GetString(1), |
| 156 | reader.GetString(2), |
| 157 | reader.GetString(3), |
| 158 | decryptedCvc, |
| 159 | reader.GetString(5), |
| 160 | reader.IsDBNull(6) ? string.Empty : reader.GetString(6) |
| 161 | )); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return ccs; |
| 167 | } |
| 168 | |
| 169 | private string EncryptCvc(string answer) |
| 170 | { |
| 171 | using (Aes aes = Aes.Create()) |
| 172 | { |
| 173 | aes.Key = mEncryptionKey; |
| 174 | aes.GenerateIV(); |
| 175 | byte[] iv = aes.IV; |
| 176 | |
| 177 | using (ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, iv)) |
| 178 | { |
| 179 | byte[] plainBytes = Encoding.UTF8.GetBytes(answer); |
| 180 | byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); |
| 181 | byte[] result = new byte[iv.Length + encryptedBytes.Length]; |
| 182 | |
| 183 | Buffer.BlockCopy(iv, 0, result, 0, iv.Length); |
| 184 | Buffer.BlockCopy(encryptedBytes, 0, result, iv.Length, encryptedBytes.Length); |
| 185 | |
| 186 | return Convert.ToBase64String(result); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | private string DecryptCvc(string encryptedAnswer) |
| 192 | { |
| 193 | byte[] combined = Convert.FromBase64String(encryptedAnswer); |
| 194 | byte[] iv = new byte[16]; |
| 195 | byte[] encryptedBytes = new byte[combined.Length - iv.Length]; |
| 196 | Buffer.BlockCopy(combined, 0, iv, 0, iv.Length); |
| 197 | Buffer.BlockCopy(combined, iv.Length, encryptedBytes, 0, encryptedBytes.Length); |
| 198 | |
| 199 | using (Aes aes = Aes.Create()) |
| 200 | { |
| 201 | aes.Key = mEncryptionKey; |
| 202 | aes.IV = iv; |
| 203 | |
| 204 | using (ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV)) |
| 205 | { |
| 206 | byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); |
| 207 | return Encoding.UTF8.GetString(decryptedBytes); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |