c# - Having wired outputs while Rfc2898DeriveBytes implementation in TripleDES -
this simple code taking hours find out why getting wrong output. can't seem find problem. okey specially rfc2898derivebytes
implementation? need find out whats causing wrong output, , how solve make code work.
static void main(string[] args) { string data = "welcome jungle"; string pass = "monkey"; string salt = "12345678"; byte[] textdata = encoding.utf8.getbytes(data); byte[] password = encoding.utf8.getbytes(pass); byte[] saltbyte = encoding.utf8.getbytes(salt); rfc2898derivebytes keygenerate = new rfc2898derivebytes(password ,saltbyte ,1000); rfc2898derivebytes keygenerate1 = new rfc2898derivebytes(password, saltbyte, 1000); byte[] key1 = keygenerate.getbytes(16); byte[] key2 = keygenerate1.getbytes(16); console.writeline("plaintext: " + data); tripledescryptoserviceprovider tdes = new tripledescryptoserviceprovider(); tdes.blocksize = 64; tdes.keysize = 128; tdes.key = key1; tdes.iv = saltbyte; tdes.mode = ciphermode.cbc; tdes.padding = paddingmode.pkcs7; icryptotransform tripledes = tdes.createencryptor(); byte[] een = tripledes.transformfinalblock(textdata, 0, textdata.length); string een1 = convert.tobase64string(een); console.writeline("encrypted data: " +een1); tripledescryptoserviceprovider tdes1 = new tripledescryptoserviceprovider(); tdes1.blocksize = 64; tdes1.keysize = 128; tdes1.key = key2; tdes1.iv = saltbyte; tdes1.mode = ciphermode.cbc; tdes1.padding = paddingmode.pkcs7; icryptotransform tripledes1 = tdes1.createdecryptor(); byte[] en1 = tripledes.transformfinalblock(een, 0, een.length); string en11 = encoding.utf8.getstring(en1); ; console.writeline("decrypted data: " +en11); console.readline(); }
this in output:
and 1 more thing want know, difference between rfc2898derivebytes(string a, byte[] salt, int iteration)
, rfc2898derivebytes( byte[] , byte[] salt, int iteration)
? , 1 best use? give same output?
you're using original (encryptor) tripledes
instance, rather decryptor instance tripledes1
decrypt.
Comments
Post a Comment