c# - Error: "Padding is invalid and cannot be removed" using asymmetric algorithm -
i want encrypt , decrypt string using asymmetric cryptographic algorithm want pass different key in encrypt , decrypt function.
my code follows:
public actionresult encrypt(string cleartext) { string encryptionkey = "abkv2spbni99212"; byte[] clearbytes = encoding.unicode.getbytes(cleartext); using (aes encryptor = aes.create()) { rfc2898derivebytes pdb = new rfc2898derivebytes(encryptionkey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.key = pdb.getbytes(32); encryptor.iv = pdb.getbytes(16); using (memorystream ms = new memorystream()) { using (cryptostream cs = new cryptostream(ms, encryptor.createencryptor(), cryptostreammode.write)) { cs.write(clearbytes, 0, clearbytes.length); cs.close(); } cleartext = convert.tobase64string(ms.toarray()); } } decrypt(cleartext); return view(cleartext); } public string decrypt(string ciphertext) { string encryptionkey = "makv2spbni99212"; byte[] cipherbytes = convert.frombase64string(ciphertext); using (aes encryptor = aes.create()) { rfc2898derivebytes pdb = new rfc2898derivebytes(encryptionkey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.key = pdb.getbytes(32); encryptor.iv = pdb.getbytes(16); using (memorystream ms = new memorystream()) { using (cryptostream cs = new cryptostream(ms, encryptor.createdecryptor(), cryptostreammode.write)) { cs.write(cipherbytes, 0, cipherbytes.length); cs.close(); } ciphertext = encoding.unicode.getstring(ms.toarray()); } } return ciphertext ; }
here encrypt function sending value using link follows
<a href="@url.action("encrypt", "home", new {@cleartext="5"})">bid buddy</a>
here want send different key values shown.
aes symmetric block cipher. decryption only works if same key presented during encryption. there no way around that.
what additionally have cryptographic hash function. hash functions have collisions, exploitation of negligible cryptographic hash function such yours. so, costly find 2 passprases map same key (which make technically asymmetric).
you need generate public-private key pair. option example rsa. encrypt data random aes key , encrypt aes key rsa public key. called hybrid encryption.
Comments
Post a Comment