c# - Decoding And Encoding Strings - HardCoded KEY for Symmetric Algorithms -
c# - Decoding And Encoding Strings - HardCoded KEY for Symmetric Algorithms -
i wrote below class encoding , decoding string info (symmetric algorithm 1 key):
using system; using system.collections.generic; using system.linq; using system.web; using system.text; using system.security.cryptography; using system.io; namespace myproject.classes { public static class symmetricencryption { private const string mykey = "bla bla bla"; private static string _algorithmname; public static string algorithmname { { homecoming _algorithmname; } set { _algorithmname = value; } } public static string encryptdata(string cleardata) { // convert string cleardata byte array byte[] cleardata_byte_array = encoding.utf8.getbytes(cleardata); // create algorithm symmetricalgorithm algorithm = symmetricalgorithm.create(algorithmname); algorithm.key = encoding.utf8.getbytes(mykey); // encrypt info memorystream target = new memorystream(); // append iv algorithm.generateiv(); target.write(algorithm.iv, 0, algorithm.iv.length); // encrypt clear info cryptostream cs = new cryptostream(target, algorithm.createencryptor(), cryptostreammode.write); cs.write(cleardata_byte_array, 0, cleardata_byte_array.length); cs.flushfinalblock(); // output byte[] target_byte_array = target.toarray(); string target_string = convert.tobase64string(target_byte_array); homecoming target_string; } public static string decryptdata(string encrypteddata) { byte[] encrypteddata_byte_array = convert.frombase64string(encrypteddata); // create algorithm symmetricalgorithm algorithm = symmetricalgorithm.create(algorithmname); algorithm.key = encoding.utf8.getbytes(mykey); // decrypt info memorystream target = new memorystream(); // read iv int readpos = 0; byte[] iv = new byte[algorithm.iv.length]; array.copy(encrypteddata_byte_array, iv, iv.length); algorithm.iv = iv; readpos += algorithm.iv.length; // decrypt encrypted info cryptostream cs = new cryptostream(target, algorithm.createdecryptor(), cryptostreammode.write); cs.write(encrypteddata_byte_array, readpos, encrypteddata_byte_array.length - readpos); cs.flushfinalblock(); // output byte[] target_byte_array = target.toarray(); string target_string = encoding.utf8.getstring(target_byte_array); homecoming target_string; } } }
and usage below :
protected void page_load(object sender, eventargs e) { symmetricencryptionutility.algorithmname = "tripledes"; response.write(symmetricencryptionutility.encryptdata("1234-4567-8910-2345")); }
i have problem mykey -> how can have hard coded key symmetric algorithms , utilize in upper class ?
the upper codes error below :
server error in '/' application.
specified key not valid size algorithm. description: unhandled exception occurred during
execution of current web request. please review stack trace more info error , originated in code.
exception details:
system.security.cryptography.cryptographicexception: specified key not valid size algorithm.
how can prepare error ?
thanks in advance
you can utilize system.security.cryptography.rfc2898derivebytes
securely generate right number of bytes key based on string
password , byte[]
salt:
var helper = new rfc2898derivebytes(password, salt); algorithm.key = helper.getbytes(algorithm.keysize / 8);
for more info rfc2898derivebytes
, how utilize it, check out page on msdn.
c# asp.net key encryption-symmetric
Comments
Post a Comment