php - (Solved) Encrypt in MySQL, Decrypt in C# -
can me. got data encrypted in mysql, store blob, need decrypt in c#, dont result wanted
it should pd001ky6900430
here's code in c#
string connectionstring = "data source=win-3doecchgfbt;initial catalog=dwh;user id=sa;password=password123;"; using (sqlconnection connection = new sqlconnection(connectionstring)) { string query = "select * tb_investor"; sqldataadapter adapter = new sqldataadapter(); var command = new sqlcommand(query, connection); adapter.selectcommand = command; datatable dtable = new datatable(); adapter.fill(dtable); for(var x =0; x < dtable.rows.count; x++) { var dr = dtable.rows; byte[] accnobyte = (byte[])dr[x].itemarray[1]; byte[] key = mkey("satu"); var rkey = bitconverter.tostring(key).replace("-", ""); var decaccno = decrypt_function(accnobyte, key); } }
here mkey method :
encoding winlatincodepage = encoding.getencoding(1252); byte[] key = encoding.utf8.getbytes(skey); byte[] k = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; (int = 0; < key.length; i++) { k[i % 16] = (byte)(k[i % 16] ^ key[i]); } return k;
here decrypt_function method :
rijndaelmanaged crypto = null; memorystream memstream = null; icryptotransform decryptor = null; cryptostream crypto_stream = null; streamreader stream_read = null; string plain_text; try { crypto = new rijndaelmanaged(); crypto.key = key; crypto.mode = ciphermode.ecb; crypto.padding = paddingmode.none; memstream = new memorystream(cipher_text); crypto.generateiv(); //create decryptor make sure if decrypting here , did not copy paste encryptor. decryptor = crypto.createdecryptor(crypto.key, crypto.iv); //this different encryption @ mode make sure reading stream. crypto_stream = new cryptostream(memstream, decryptor, cryptostreammode.read); //i used stream reader here because readtoend method easy , because return string, easy. stream_read = new streamreader(crypto_stream); plain_text = stream_read.readtoend(); } { if (crypto != null) crypto.clear(); memstream.flush(); memstream.close(); } return plain_text;
please, show me mistake did make....
thx before....
"pd001ky6900430" 14 bytes, aes(rijndaelmanaged default) block size 16-bytes input data needs padded block size multiple, last 2 0x02
bytes of pkcs#7 padding. 2 last bytes of: "pd001ky6900430\u0002\u0002" (where \u0002 represents single byte of 0x02
in utf-16) padding.
this handled (removed) specifying pkcs#7 padding decryption method.
the fix:
change
crypto.padding = paddingmode.none;
to
crypto.padding = paddingmode.pkcs7;
it best specify options.
wiki
Comments
Post a Comment