hi guys! do you know of any algorithm that i can use to encrypt a string to a readable string so the result must be numeric (0-9) or Letters(Aa-Zz) or combination of the two. Thanks in advance!
Printable View
hi guys! do you know of any algorithm that i can use to encrypt a string to a readable string so the result must be numeric (0-9) or Letters(Aa-Zz) or combination of the two. Thanks in advance!
use this function:
using System.Security.Cryptography;
public static byte[] EncryptPassword(string userName, string password)
{
string tmpPassword = null;
tmpPassword = userName.ToLower() + password;
//Convert the password string into an Array of bytes.
UTF8Encoding textConverter = new UTF8Encoding();
byte[] passBytes = textConverter.GetBytes(tmpPassword);
return new MD5CryptoServiceProvider().ComputeHash(passBytes);
}
You are both speaking nonsense.
daimous, it would seem that you need "base 36" encoding. Which is neither sensibly readable, nor encrypted to any strong degree.
basti... hashing something drops its length. thus no encryption and no readibility.
This is an off the shelf example from MSDN that was in vb and didn't work. I did some tweaking. This isn't exactly how you should encrypt things really securely, but it will get the job done. You need to read up on this stuff to identify the flaws in the code, but it is a good example to get you started in the basics.
Everything should return in a string.
Code:using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace MyCrypto
{
/// <summary>
///
/// Class: Encrypt and Decrypt UTF strings
/// Methods: encryptStringToBytes_AES, decryptStringFromBytes_AES,
/// getMD5HashString, getHashBytes
///
/// Key Length:
/// 13/16/29 characters are needed for 128/152/256-bit
/// </summary>
public class Crypto
{
protected Byte[] IV = {51, 129, 19, 159, 132, 55, 236, 179,
89, 243, 244, 181, 17, 136, 39, 235};
/// <summary>
/// http://blog.brezovsky.net/en-text-2.html
/// Returns same value as MD5() in PHP
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public string getMD5HashString(string sInput)
{
Byte[] bs = this.getHashBytes(sInput);
StringBuilder s = new StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
return s.ToString();
}
public Byte[] getHashBytes(string sInput)
{
MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
byte[] bs = Encoding.UTF8.GetBytes(sInput);
bs = x.ComputeHash(bs);
return bs;
}
public string encryptStringToBytes_AES(string plainText, string sKey)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (sKey == null || sKey.Length <= 0)
throw new ArgumentNullException("Key");
Byte[] byteKey = getHashBytes(sKey);
// Declare the streams used
// to encrypt to an in memory
// array of bytes.
MemoryStream msEncrypt = null;
CryptoStream csEncrypt = null;
StreamWriter swEncrypt = null;
// Declare the RijndaelManaged object
// used to encrypt the data.
RijndaelManaged aesAlg = null;
// Declare the bytes used to hold the
// encrypted data.
//byte[] encrypted = null;
try
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = byteKey;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
msEncrypt = new MemoryStream();
csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
swEncrypt = new StreamWriter(csEncrypt);
//Write all data to the stream.
swEncrypt.Write(plainText);
}
finally
{
// Clean things up.
// Close the streams.
if (swEncrypt != null)
swEncrypt.Close();
if (csEncrypt != null)
csEncrypt.Close();
if (msEncrypt != null)
msEncrypt.Close();
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
// Return the encrypted bytes from the memory stream.
return Convert.ToBase64String(msEncrypt.ToArray());
}
public string decryptStringFromBytes_AES(string cipherText, string sKey)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (sKey == null || sKey.Length <= 0)
throw new ArgumentNullException("Key");
Byte[] cipherBytes = Convert.FromBase64String(cipherText);
Byte[] byteKey = getHashBytes(sKey);
// TDeclare the streams used
// to decrypt to an in memory
// array of bytes.
MemoryStream msDecrypt = null;
CryptoStream csDecrypt = null;
StreamReader srDecrypt = null;
// Declare the RijndaelManaged object
// used to decrypt the data.
RijndaelManaged aesAlg = null;
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
try
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = byteKey;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
msDecrypt = new MemoryStream(cipherBytes);
csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
srDecrypt = new StreamReader(csDecrypt);
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
finally
{
// Clean things up.
// Close the streams.
if (srDecrypt != null)
srDecrypt.Close();
if (csDecrypt != null)
csDecrypt.Close();
if (msDecrypt != null)
msDecrypt.Close();
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
return plaintext;
}
}//end class Crypto
}//end MyCrypto
You could create a look-up table and do letter replacement (simple to design and implement)
You could also go through and XOR by a set value (such as 32)
There's dozens of ways to make it unreadable. Why does the result have to be a readable string? Are you transmitting it?
Thanks for the input guys! Actually, im trying to generate a unique key base from the unique ID and that generated key will to use to something like activation code. Currently i have this solution.
First, generate a "unique" ID for a computer by getting some data about that computer like Drive C Serial, MAC Address, BIOS Serial, Memory Capacity
Second, Generate Unlock key/activation key from the generated IDCode:public string FuncGetSystemID()
{
string temp = string.Empty;
StringBuilder SystemID = new StringBuilder();
string DriveSerial = FuncGetDriveSerial();
string MacAddress = FuncGetMACAddress();
string BiosSerial = FuncGetBIOSSerial();
string PMemoryCapacity = FuncGetPhysicalMemoryCapacity();
int SysinfoLenght = DriveSerial.Length + MacAddress.Length + BiosSerial.Length + PMemoryCapacity.Length;
//replace 16 with Sysinfolenght if you want to use teh full string lenght
for (int i = 0; i < 20; i++)
{
if ((i % 4) == 0 && i != 0)
{
SystemID.Append('-');
}
int counter = i % 4;
switch (counter)
{
case 0://Drive Serial
{
if ((i / 4) < DriveSerial.Length)
SystemID.Append(DriveSerial.Substring((i / 4), 1));
else
SystemID.Append(Convert.ToChar(i), 1);
//SysinfoLenght++; uncomment is sysinfolenght will be use.
break;
}
case 1://MAC Address
{
if (((i - 1) / 4) < MacAddress.Length)
SystemID.Append(MacAddress.Substring(((i - 1) / 4), 1));
else
SystemID.Append(Convert.ToChar(i), 1);
break;
}
case 2://BIOSSerial
{
if (((i - 2) / 4) < BiosSerial.Length)
SystemID.Append(BiosSerial.Substring(((i - 2) / 4), 1));
else
SystemID.Append(Convert.ToChar(i), 1);
break;
}
case 3://Physical Memory Capacity
{
if (((i - 3) / 4) < PMemoryCapacity.Length)
SystemID.Append(PMemoryCapacity.Substring(((i - 3) / 4), 1));
else
SystemID.Append(Convert.ToChar(i), 1);
break;
}
}
}
return SystemID.ToString();
}
public string FuncGetDriveSerial()
{
string DriveCSerial = string.Empty;
ManagementObject mobj = new ManagementObject("win32_logicaldisk.deviceid=\"C:\"");
mobj.Get();
DriveCSerial = mobj["VolumeSerialNumber"].ToString();
mobj.Dispose();
return DriveCSerial;
}
public string FuncGetMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress = String.Empty;
foreach (ManagementObject mo in moc)
{
if (MACAddress == String.Empty) // only return MAC Address from first card
{
if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
}
mo.Dispose();
}
MACAddress = MACAddress.Replace(":", "");
return MACAddress;
}
public string FuncGetBIOSSerial()
{
string BIOSSerial = string.Empty;
ManagementObjectSearcher MgmtObjSearcher =
new ManagementObjectSearcher("root\\CIMV2","SELECT * FROM Win32_BIOS");
foreach (ManagementObject qryObj in MgmtObjSearcher.Get())
{
BIOSSerial = qryObj["SerialNumber"].ToString();
}
return BIOSSerial;
}
public string FuncGetPhysicalMemoryCapacity()
{
string PhysicalMemoryCapacity = string.Empty;
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2","SELECT * FROM Win32_PhysicalMemory");
foreach (ManagementObject queryObj in searcher.Get())
{
PhysicalMemoryCapacity = queryObj["Capacity"].ToString();
}
return PhysicalMemoryCapacity;
}
Comments, suggestion, critcism will be highly appreciate. Thanks!Code:public string FuncGetunlockKey(string SystemId)
{
string TmpUnlockKey = string.Empty;
string UnlockKey = string.Empty;
string TmpKey = string.Empty;
int counter = 1;
UTF8Encoding encoder = new UTF8Encoding();
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(encoder.GetBytes(SystemId));
foreach (byte b in result)
{
TmpUnlockKey += b.ToString();
}
foreach (char chr in TmpUnlockKey)
{
TmpKey += chr.ToString();
if (counter == 2)
{
int i;
int.TryParse(TmpKey,out i);
if ((i >= 48 && i <= 57) || (i >= 65 && i <= 90) || (i >= 97 && i <= 122))
{
UnlockKey += Convert.ToChar(i);
}
else
{
UnlockKey += TmpKey;
}
counter = 0;
TmpKey = "";
}
counter++;
}
return UnlockKey;
}
I read all the comments and I'm a little confused as to exactly what you want. But anyway, I don't know if an encryption like 3DES or Triple DES would help you. There's a complete example in the msdn. With these type of encryption algorithms, a string is excrypted and returned an array of bytes. I simply convert the array of bytes back to a string if the need arises.
Hope this helps - Jennifer.
I sometimes see some people attempt to help others in here and someone else comes along with harsh words either because their input was slighty incorrect or whatever... The point is that somebody tried. So all I would say is: I think you need to refine you communication skills!Quote:
Originally Posted by wossname
Jennifer.
But if it doesn't work, then it's not really any help then is it? IT just becomes noise that leads to further problems.
And all basti did was to take username and password, concat them, then hashed it. Hardly satisfying the requirements that it be "readable" (which doesn't make much sense to me either.)
-tg
In what way was it harsh? It was a statement of fact. Also those living in glass houses shouldn't throw stones etc...Quote:
Originally Posted by JenniferBabe
or use the toilet. It looks like des isn't what he wants. He's trying to come up with a way to generate registration keys (correct me if wrong)
nope that what I gathered after he posted his code. His problem is he doesn't know how to convery in proper terms what he wants.Quote:
Originally Posted by Lord Orwell
My problem is already solved with the help of you guys...Thanks very much for the ideas.
if you don't have an obfuscator, there's no use in encryption. Although .net applications cannot be opened in ollydbg, there are much easier and more efficient ways of disassembling such as reflector...using this allows someone to just about view your original source...so either use encryption and an obfuscator or use neither...
You right! Ive tested it my self with Reflector and i can easily view the code and worse even my function to generate a valid code which i dont want to heppen....I think i need to post this issue as new Thread since this is a different issue. Thanks alot gamesguru!
what is this Reflector? Is it a .net component?
Reflector is a .NET disassembler. It allows you to view, basically, the exact source used to compile that class library or application. It was a real shocker to me to find out that you could do that...there's many look alike programs too, but most are not free. There's also one in visual studios! If you can believe that...but reflector is my personal favorite...and obfuscators can't fully protect you, it will not stop the determined cracker...