c# - How to decrypt with WinRT without changing existing encryption code in .NET Desktop App? -
i have little problem whole encrypt/decrypt cr*p ;-)
is possible decrypt data in winrt (windows store app) encrypted in .net desktop application , vice versa? cannot change code of desktop app because in use.
i tried few tutorials cryptographicengine in winrt never results match ones desktop app.
maybe me? i'm new .net development , never did encryption have no idea i'm doing ;-)
here of code used in desktop app - can't change code!
private string pwd = "password"; private string salt = "salt"; public byte[] encrypt(byte[] data) { passwordderivebytes derivedpassword = new passwordderivebytes(pwd, encoding.ascii.getbytes(salt)); byte[] key = derivedpassword.getbytes(16); byte[] iv = encoding.ascii.getbytes("1234567891234567"); rijndaelmanaged symmetrickey = new rijndaelmanaged(); symmetrickey.mode = ciphermode.cbc; byte[] cipherbytes = null; using (icryptotransform encryptor = symmetrickey.createencryptor(key, iv)) { using (memorystream ms = new memorystream()) { using (cryptostream cryptostream = new cryptostream(ms, encryptor, cryptostreammode.write)) { cryptostream.write(data, 0, data.length); cryptostream.flushfinalblock(); cryptostream.close(); cipherbytes = ms.toarray(); ms.close(); } } } symmetrickey.clear(); return cipherbytes; }
here example method tried in winrt - result different desktop app (most of code http://blog.lordinaire.fr/2012/12/winrt-encryption-and-decryption-with-aes-algorithm/ )
non functioning code removed - see edit
i apreciate help
greetings austria
edit tried few things, still not work right :( can't right key. test encryption nonetheless, hardcoded key - works.
like nate diamond suggested, used keyderivationalogrithm empty salt , salted password. 1 problem is, don't know how "to salt". tried putting salt in front, @ end, in middle , alternating every symbol - still not right key :( here code using:
// password = 11112222333344445555 // salt = aaaabbbbccccddddeeee private string password = "11112222333344445555aaaabbbbccccddddeeee"; private byte[] salt = new byte[20]; private uint iterationcount = 100; private static byte[] keybytes = null; public static byte[] keybytes { { //for (int = 0; < salt.length; i++) //{ // salt[i] = 0; //} // setup kdf parameters desired salt , iteration count keyderivationparameters kdfparameters = keyderivationparameters.buildforpbkdf2(cryptographicbuffer.createfrombytearray(salt), iterationcount); // kdf provider pbkdf2, , store source password in cryptographic key keyderivationalgorithmprovider kdf = keyderivationalgorithmprovider.openalgorithm(keyderivationalgorithmnames.pbkdf2sha1); ibuffer passwordbuffer = cryptographicbuffer.convertstringtobinary(password, binarystringencoding.utf8); cryptographickey passwordsourcekey = kdf.createkey(passwordbuffer); // generate key material source password, salt, , iteration count. call derivekeymaterial once, // since calling twice generate same data key , iv. uint totaldataneeded = 16; ibuffer keyandiv = cryptographicengine.derivekeymaterial(passwordsourcekey, kdfparameters, totaldataneeded); // split derived bytes seperate key , iv keybytes = keyandiv.toarray(); return keybytes; } }
here code of encrypt method - yields same result 1 in .net desktop app :)
private byte[] btvector = encoding.utf8.getbytes("1234567891234567"); private byte[] keybytes = encoding.utf8.getbytes("123456789123456789123456"); public byte[] encrypt(byte[] data) { // key , iv , put ibuffers ibuffer keybuffer = windowsruntimebuffer.create(keybytes, 0, 16, 16); ; ibuffer iv = windowsruntimebuffer.create(initialvectorbytes, 0, 16, 16); ibuffer plaintext = cryptographicbuffer.createfrombytearray(data); byte[] encrypteddata; // setup aes key, using aes in cbc mode , applying pkcs#7 padding on input symmetrickeyalgorithmprovider aesprovider = symmetrickeyalgorithmprovider.openalgorithm(symmetricalgorithmnames.aescbcpkcs7); cryptographickey aeskeysymm = aesprovider.createsymmetrickey(keybuffer); // encrypt data , convert byte array ibuffer encrypted = cryptographicengine.encrypt(aeskeysymm, plaintext, iv); cryptographicbuffer.copytobytearray(encrypted, out encrypteddata); return encrypteddata; }
pbkdf[1/2] this:
1. take password. 2. add salt. 3. hash combined password , salt. store in `result` 4. (number of iterations) 1. hash `result`, store in `result`.
the big difference between pbkdf1 , pbkdf2 first part of step 4. in pbkdf1, printed. in pbkdf2, changes this:
4. (number of iterations) 1. combine `result` , `salt`. store in `result` 2. hash `result`, store in `result`.
so, have few options.
option 1:
create custom implementation of pbkdf1. hashalgorithmprovider
makes easy repeatedly hash result.
option 2:
use pbkdf2 empty salt
byte
array , combined salt , password secret
. should have same effect pbkdf1.
it should noted if @ possible, should switch using pbkdf2.
hope helps , happy coding!
Comments
Post a Comment