Hi i created Rijndael class to ecoding/decoding string. I wonna do in that class , return int as output decoded string.Rijndael examples usualy is used byte64 as encoded string i dont wonna base64 output but number output(int).
I wrote that:
public static string Encrypt(string inputText,string ENCRYPTION_KEY)
{
byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString());
RijndaelManaged rijndaelCipher = new RijndaelManaged();
byte[] plainText = Encoding.Unicode.GetBytes(inputText);
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);
using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)))
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainText, 0, plainText.Length);
cryptoStream.FlushFinalBlock();
return BitConverter.ToInt32(memoryStream.ToArray(),0).ToString();
}
}
}
}
public static string Decrypt(Int32 inputText,string ENCRYPTION_KEY)
{
byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString());
RijndaelManaged rijndaelCipher = new RijndaelManaged();
byte[] encryptedData = BitConverter.GetBytes(inputText);
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);
using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
{
using (MemoryStream memoryStream = new MemoryStream(encryptedData))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainText = new byte[encryptedData.Length];
int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
}
}
}
}
Encripting work great but decrypting are not .I error in this line:
byte[] plainText = new byte[encryptedData.Length];
int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
plaintext get 0 value
and plaintext.Lenght 0 too.
I get error: Length of the data to decrypt is invalid.
The mystic output you are showing might be output from Encrypt method. Are you saying, you wanted numeric output from encrypt method??
Yes.
I wonna encrypt thats sample text: "It is my string" to output for example 12345673545.
And next using decode method and decode 12345673545 to string "It is my string".Its possible to do this?If yes how?I trying do this, but i dunno is it possible.
Ok. If at all you want only numeric output from your encrypt method then you should go for different type of encryption.
But remember it's easy to break your output to actual input if you have only numbers in your output. It's more vulnerable and risky. May be you should look at the link provided by MahadTECH above(I'm
pasting the link to the particular post below) where you'll get UTF8Encoding data which might be looking good for you.
I am saying even if you use such one it's vulnerable and I don't see any significance of using it. Only dummies can't hack it. Experts can easily figure out your data hiding there. It's very simple to crack numeric data.
You can use polybius cipher which gives you numeric encrypted output that I have learned during my college days, which is very simple to use. Ofcourse there are lot more of algorithms you can look for. Here you can re arrange the characters
order in the array to make it better.
public class PolyBius
{
private char[,] arr = {{'a','b','c','d','e'},{'f','g','h','i','j'},{'k','l','m','n','o'},{'p','q','r','s','t'},
{'u','v','w','x','y'}};
public PolyBius()
{
}
public string Encrypt(string input)
{
StringBuilder sb2 = new StringBuilder();
foreach (char ch in input)
{
sb2 = sb2.Append(GetIndex(ch).ToString());
}
return sb2.ToString();
}
public string Decrypt(string encryptedData)
{
StringBuilder sb3 = new StringBuilder();
for (int i = 0; i < encryptedData.Length; i = i + 2)
{
String s3 = encryptedData.Substring(i, 2);
int x = Int32.Parse(s3);
sb3.Append(GetCharVal(x));
}
return sb3.ToString();
}
private int GetIndex(char c)
{
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (arr[i, j] == c)
return (10 * (i + 1) + (j + 1));
if (c == 'z')
return 55;
else
return 0;
}
private char GetCharVal(int x)
{
int i = x / 10;
int j = x % 10;
return arr[i - 1, j - 1];
}
}
Thanx a lot ,this will be enough for me. I know it easy to decode but will using as hibrid with Rijndael encrypting method -it will be great,and hard to decode anyway.It convert non numeric value type into numeric but every numeric value type which is in
string will change as 0. I must create an exception which numeric values will be skip.
xRidx
Member
573 Points
235 Posts
Rijndael decode problem
Jun 30, 2012 02:59 PM|LINK
Hi i created Rijndael class to ecoding/decoding string. I wonna do in that class , return int as output decoded string.Rijndael examples usualy is used byte64 as encoded string i dont wonna base64 output but number output(int).
I wrote that:
public static string Encrypt(string inputText,string ENCRYPTION_KEY) { byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString()); RijndaelManaged rijndaelCipher = new RijndaelManaged(); byte[] plainText = Encoding.Unicode.GetBytes(inputText); PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT); using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16))) { using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainText, 0, plainText.Length); cryptoStream.FlushFinalBlock(); return BitConverter.ToInt32(memoryStream.ToArray(),0).ToString(); } } } } public static string Decrypt(Int32 inputText,string ENCRYPTION_KEY) { byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString()); RijndaelManaged rijndaelCipher = new RijndaelManaged(); byte[] encryptedData = BitConverter.GetBytes(inputText); PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT); using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16))) { using (MemoryStream memoryStream = new MemoryStream(encryptedData)) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) { byte[] plainText = new byte[encryptedData.Length]; int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length); return Encoding.Unicode.GetString(plainText, 0, decryptedCount); } } } }Encripting work great but decrypting are not .I error in this line:
xRidx
Member
573 Points
235 Posts
Re: Rijndael decode problem
Jun 30, 2012 05:31 PM|LINK
public static string Encrypt(string inputText,string ENCRYPTION_KEY) { byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString()); RijndaelManaged rijndaelCipher = new RijndaelManaged(); byte[] plainText = System.Text.Encoding.Default.GetBytes(inputText); PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT); using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16))) { using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainText, 0, plainText.Length); cryptoStream.FlushFinalBlock(); return System.Text.Encoding.Default.GetString(memoryStream.ToArray()); } } } } public static string Decrypt(string inputText, string ENCRYPTION_KEY) { byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString()); RijndaelManaged rijndaelCipher = new RijndaelManaged(); byte[] encryptedData = System.Text.Encoding.Default.GetBytes(inputText); PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT); using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16))) { using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write)) { cryptoStream.Write(encryptedData, 0, encryptedData.Length); cryptoStream.FlushFinalBlock(); return System.Text.Encoding.Default.GetString(memoryStream.ToArray()); ; } } }buddalasunil...
Member
516 Points
90 Posts
Re: Rijndael decode problem
Jun 30, 2012 06:38 PM|LINK
Your code should work. Make sure you use the same key at both ends, while doing encryption and decryption.
You are passing a character string not numeric data. What do you meant by getting numeric value as output?
The mystic output you are showing might be output from Encrypt method. Are you saying, you wanted numeric output from encrypt method??
xRidx
Member
573 Points
235 Posts
Re: Rijndael decode problem
Jun 30, 2012 07:00 PM|LINK
Yes.
I wonna encrypt thats sample text: "It is my string" to output for example 12345673545.
And next using decode method and decode 12345673545 to string "It is my string".Its possible to do this?If yes how?I trying do this, but i dunno is it possible.
MahadTECH
Star
8976 Points
1659 Posts
Re: Rijndael decode problem
Jul 01, 2012 03:18 AM|LINK
Hello xRidx,
I have Answered it Before!
Check this Link
Good luck` Mark the Post Answered if it helped no matter you can mark multiple posts as answered!
Mahad Bin Mukhtar
Remember to Mark the replies as Answers
The easiest day was 'yesterday'.
MCP, MCSD
For .NET TECH Blog
buddalasunil...
Member
516 Points
90 Posts
Re: Rijndael decode problem
Jul 01, 2012 04:10 AM|LINK
Ok. If at all you want only numeric output from your encrypt method then you should go for different type of encryption.
But remember it's easy to break your output to actual input if you have only numbers in your output. It's more vulnerable and risky. May be you should look at the link provided by MahadTECH above(I'm pasting the link to the particular post below) where you'll get UTF8Encoding data which might be looking good for you.
http://forums.asp.net/post/5003892.aspx
xRidx
Member
573 Points
235 Posts
Re: Rijndael decode problem
Jul 01, 2012 03:10 PM|LINK
THX for answers.
I only wonna create encoded string without special chracters.Base64 output return characters "+ / =". I cant use ASCII too.
Do u know any encryption whitch can encrypt without special characters?
buddalasunil...
Member
516 Points
90 Posts
Re: Rijndael decode problem
Jul 01, 2012 03:41 PM|LINK
I am saying even if you use such one it's vulnerable and I don't see any significance of using it. Only dummies can't hack it. Experts can easily figure out your data hiding there. It's very simple to crack numeric data.
buddalasunil...
Member
516 Points
90 Posts
Re: Rijndael decode problem
Jul 01, 2012 04:36 PM|LINK
You can use polybius cipher which gives you numeric encrypted output that I have learned during my college days, which is very simple to use. Ofcourse there are lot more of algorithms you can look for. Here you can re arrange the characters order in the array to make it better.
public class PolyBius { private char[,] arr = {{'a','b','c','d','e'},{'f','g','h','i','j'},{'k','l','m','n','o'},{'p','q','r','s','t'}, {'u','v','w','x','y'}}; public PolyBius() { } public string Encrypt(string input) { StringBuilder sb2 = new StringBuilder(); foreach (char ch in input) { sb2 = sb2.Append(GetIndex(ch).ToString()); } return sb2.ToString(); } public string Decrypt(string encryptedData) { StringBuilder sb3 = new StringBuilder(); for (int i = 0; i < encryptedData.Length; i = i + 2) { String s3 = encryptedData.Substring(i, 2); int x = Int32.Parse(s3); sb3.Append(GetCharVal(x)); } return sb3.ToString(); } private int GetIndex(char c) { for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) if (arr[i, j] == c) return (10 * (i + 1) + (j + 1)); if (c == 'z') return 55; else return 0; } private char GetCharVal(int x) { int i = x / 10; int j = x % 10; return arr[i - 1, j - 1]; } }and you use it normally.
PolyBius cipher = new PolyBius(); string encryptedData = cipher.Encrypt("comeon"); string decryptedData = cipher.Decrypt(encryptedData);which gives you output of encrypted data as 133533153534
xRidx
Member
573 Points
235 Posts
Re: Rijndael decode problem
Jul 01, 2012 06:33 PM|LINK
Thanx a lot ,this will be enough for me. I know it easy to decode but will using as hibrid with Rijndael encrypting method -it will be great,and hard to decode anyway.It convert non numeric value type into numeric but every numeric value type which is in string will change as 0. I must create an exception which numeric values will be skip.