I'm trying to encrypt a string in C#:
static public string Encrypt(char[] a)
{
for (int i = 0; i < a.Length; i++)
{
a[i] -= (char)(i + 1);
if (a[i] < '!')
{
a[i] += (char)(i + 20);
}
}
return new string(a);
}
Now, when I put in this string:
"Qui habite dans un ananas sous la mer?".
The encryption comes out as:
`Psf3c[[ak[3XT`d3d\3MYKWIZ3XSXU3L@?JAMR`
There's an unrecognizable character in there, after the @. I don't know how it got there, and I don't know why.
If I try to decrypt it (using this method:)
static public string Decrypt(char[] a)
{
for (int i = 0; i < a.Length; i++)
{
a[i] += (char)(i + 1);
if ((a[i] - 20) - i <= '!')
{
a[i] -= (char)(i + 20);
}
}
return new string(a);
}
This is the (incorrect) output:
Qui habite dans un ananas sous laamerx.
How do I allow the encryption routine to access unicode characters?
That's a pretty week encryption, your problem is that the encryption algorithm output's ASCII values that is not possible to print out in a viewable format.
A solution is to encode the data in some way, either print them out as a list of decimals with separators or use some sort of encodings algorithms like base64 or radix64.
Just a tip, most of the modern encryption algorithms uses XOR operator to encrypt the data. I wrote a easy xor cipher with CBC chaning mode to you, just to point out this is far way from a secure algorithm, but it's much more secure than your project.
This is a block-cipher that means it encrypt a block (n-bytes) for every loop, in this example it encrypts 8-bytes. So the
iv(Initialize Vector - Random data) need's to be 8-bytes long, thepasswordneeds also to be 8-bytes long. And the text you are encrypted must be splitet up in blocks of 8-bytes. And then loop the function until all data is encrypted, example if you have 32-byte of data that needs to be encrypted, then it will take 4 loops to complete the encryption.EDIT: Forgot to tell you that you input random data as
ivfor the first loop you do, and then input the result of the previous loop asivfor the next loop and so on.