I am trying to follow the instructions on deriving the WIF of a Hex bitcoin private key from - https://en.bitcoin.it/wiki/Wallet_import_format
However, when I try to hash the string (including 0x80 byte) I get different result than the expected.
I should have gotten 8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592.
Instead I receive e2e4146a36e9c455cf95a4f259f162c353cd419cc3fd0e69ae36d7d1b6cd2c09.
I read extensively on google and I understood that I should convert the string to binaries. I did, and then hashed the char array from those binaries to the same result.
Code is now working thanks to #Heinan Cabouly and #JaredPar
Here is the working code:
using System;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
namespace Base58Encode
{
internal class Program
{
public static string Str = "800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D";
public static byte[] Bytes;
public static void Main()
{
Bytes = StringToByteArray(Str);
SHA256Managed sha = new SHA256Managed();
string hashstr = String.Empty;
byte[] encrypt = sha.ComputeHash(Bytes);
foreach (byte b in encrypt)
{
hashstr += b.ToString("x2");
}
Console.WriteLine(hashstr);
//prints e2e4146a36e9c455cf95a4f259f162c353cd419cc3fd0e69ae36d7d1b6cd2c09
//instead of 8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592
Console.ReadLine();
}
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
}
}
This is how to hash a hex string SHA-256 in C#.
Thanks to all! Helped me out a lot!
As said here before me, the method you used for conversion isn't suitable. You can use this method (taken from stackoverflow by #JaredPar):
public static byte[] StringToByteArray(string hex) {
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
You call this function with your str (which represents the HEX string), and it will return the HEX representation.
From there, you can continue with your function as written.
Related
I am trying to generate 64 HEX digits to be used as a AES 256 key with no success.
Can somebody point out the mistakes and a better way to generate the same.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
namespace Test
{
public class Program
{
static System.Text.StringBuilder builder = new System.Text.StringBuilder();
public static void Main(string[] args)
{
String randomNumber = Convert.ToBase64String (GenerateRandomNumber(32));
Console.WriteLine(randomNumber);
string input = randomNumber;
char[] values = input.ToCharArray();
foreach (char letter in values)
{
// Get the integral value of the character.
int value = Convert.ToInt32(letter);
// Convert the decimal value to a hexadecimal value in string form.
string hexOutput = String.Format("{0:X}", value);
// Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
builder.Append(hexOutput);
}
Console.WriteLine(builder);
}
public static byte[] GenerateRandomNumber(int length)
{
using (var randomNumberGenerator = new RNGCryptoServiceProvider())
{
var randomNumber = new byte[length];
randomNumberGenerator.GetBytes(randomNumber);
return randomNumber;
}
}
}
}
Your biggest technical problem is that you used {0:X} when you meant {0:X2}. If the value is 10 the former produces "A" and the latter "0A". Since you've lost where all of the interior zeroes are your number isn't recoverable.
internal static string ByteArrayToHex(this byte[] bytes)
{
StringBuilder builder = new StringBuilder(bytes.Length * 2);
foreach (byte b in bytes)
{
builder.Append(b.ToString("X2"));
}
return builder.ToString();
}
(Code copied from https://github.com/dotnet/corefx/blob/7cad8486cbabbce0236bdf530e30db7036335524/src/Common/tests/System/Security/Cryptography/ByteUtils.cs#L37-L47)
But it's also pretty unclear why you're rerouting through Base64+ToCharArray+ToInt32. You're replacing values in the 0-255 range (bytes) with values in the [A-Za-z0-9/=+] rangeset, equivalent to 0-63 (Base64 and all); so you a) wouldn't have a very random key and b) it'll be too long.
I don't see why you need to convert it to a base64 string first. It could be as simple as this:
public class Program
{
public static void Main(string[] args)
{
var key = GenerateRandomNumber(32);
var hexEncodedKey = BitConverter.ToString(key).Replace("-", "");
Console.WriteLine(hexEncodedKey);
}
public static byte[] GenerateRandomNumber(int length)
{
using (var randomNumberGenerator = RandomNumberGenerator.Create())
{
var randomNumber = new byte[length];
randomNumberGenerator.GetBytes(randomNumber);
return randomNumber;
}
}
}
.NET framework already has a method Aes.GenerateKey() for generating symmetric keys, please look at this MSDN documentation: Aes class
Hi I am in need of using file handling,for that i used a method for converting a hexadecimal string into a byte array.
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
My problem is ,when i give a small hexadecimal string as a parameter to this function it will produce the right output,but when i used a large hexadecimal string as a parameter output is not that expected.
for your clear understanding -
I used a hexadecimal string which is being converted from a byte array of value [26246026],
when i convert that hex string into a byte array it should return a byte value as [26246026] - but its returning only the partial bytes ie.[262144].
i cant get the exact byte value from the hex string,how can i get that?
Please someone help me to get the expected output.
My input string for that method contains this hexadecimel string which is a 25mb size file-it should return a byte value of [26246026]---but its returning only the byte value of [262144].
when am using small hex string (min size file) its working fine,but when i work on big files i cant get the original file byte. please suggest me what to do.
my input parameter string content is as follow as asked in comment.
Its totally 524288 characters in length..
looks like this.
3026b2758e66cf11a6d900aa0062ce6c301600000000000008000000010240a4d0d207e3d21197f000a0c95ea850cc0000000000000004001c00530066004f0072006900670069006e0061006c00460050005300000003000400b49204001c0057004d004600530044004b00560065007200730069006f006e00000000001e00310031002e0030002e0036003000300031002e00370030003000300000001a0057004d004600530044004b004e006500650064006500640000000000160030002e0030002e0030002e00300030003000300000000c0049007300560042005200000002000400000000003326b2758e66cf11a6..........................................................................................................................................
d900aa0062ce6c54010000000000001e0000003a00da000000570dcb8b495848cea4609eca906bc24db442394f0ddac5eb0604fb99820bcc30ff0f1736eefd74cd4317a21a369e208c580dbb02f90e888f0a35901e08439ec6087c61d241bc3c476c24d311291a678596a98792a9000b68adf213906e0f00097c8d989e517ee532fcd6cb70e520ec9dd4fad8a1a37668bbd678bea11c1fcf2d187c4c4c6c09c3c2c53d3e64016cfebc34eace85d45a4c08cd78d05d3934e05b72ec194304848165a8c1a585c78423
/// <summary>
/// Parses a continuous hex stream from a string.
/// </summary>
public static byte[] ParseHexBytes(this string s)
{
if (s == null)
throw new ArgumentNullException("s");
if (s.Length == 0)
return new byte[0];
if (s.Length % 2 != 0)
throw new ArgumentException("Source length error", "s");
int length = s.Length >> 1;
byte[] result = new byte[length];
for (int i = 0; i < length; i++)
{
result[i] = Byte.Parse(s.Substring(i * 2, 2), NumberStyles.HexNumber);
}
return result;
}
I am trying to implement my own RSA encryption engine. Given these RSA algorithm values:
p = 61. // A prime number.
q = 53. // Also a prime number.
n = 3233. // p * q.
totient = 3120. // (p - 1) * (q - 1)
e = 991. // Co-prime to the totient (co-prime to 3120).
d = 1231. // d * e = 1219921, which is equal to the relation where 1 + k * totient = 1219921 when k = 391.
I am trying to write a method to encrypt each byte in a string and return back an encrypted string:
public string Encrypt(string m, Encoding encoding)
{
byte[] bytes = encoding.GetBytes(m);
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)BigInteger.ModPow(bytes[i], e, n);
}
string encryptedString = encoding.GetString(bytes);
Console.WriteLine("Encrypted {0} as {1}.", m, encryptedString);
return encryptedString;
}
The obvious issue here is that BigInteger.ModPow(bytes[i], e, n) may be too large to fit into a byte-space; it could result in values over 8 bits in size. How do you get around this issue while still being able to decrypt an encrypted string of bytes back into a regular string?
Update: Even encrypting from byte[] to byte[], you reach a case where encrypting that byte using the RSA algorithm goes beyond the size limit of a byte:
public byte[] Encrypt(string m, Encoding encoding)
{
byte[] bytes = encoding.GetBytes(m);
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)BigInteger.ModPow(bytes[i], e, n);
}
return bytes;
}
Update: My issue is that encryption would cause a greater number of bytes than the initial input string had:
public byte[] Encrypt(string m, Encoding encoding)
{
byte[] bytes = encoding.GetBytes(m);
byte[] returnBytes = new byte[0];
for (int i = 0; i < bytes.Length; i++)
{
byte[] result = BigInteger.ModPow(bytes[i], (BigInteger)e, n).ToByteArray();
int preSize = returnBytes.Length;
Array.Resize(ref returnBytes, returnBytes.Length + result.Length);
result.CopyTo(returnBytes, preSize);
}
return returnBytes;
}
public string Decrypt(byte[] c, Encoding encoding)
{
byte[] returnBytes = new byte[0];
for (int i = 0; i < c.Length; i++)
{
byte[] result = BigInteger.ModPow(c[i], d, n).ToByteArray();
int preSize = returnBytes.Length;
Array.Resize(ref returnBytes, returnBytes.Length + result.Length);
result.CopyTo(returnBytes, preSize);
}
string decryptedString = encoding.GetString(returnBytes);
return decryptedString;
}
If you ran this code like this:
byte[] encryptedBytes = engine.Encrypt("Hello, world.", Encoding.UTF8);
Console.WriteLine(engine.Decrypt(encryptedBytes, Encoding.UTF8));
The output would be this:
?♥D
?♥→☻►♦→☻►♦oD♦8? ?♠oj?♠→☻►♦;♂?♠♂♠?♠
Obviously, the output is not the original string because I can't just try decrypting each byte at a time, since sometimes two or more bytes of the cypher-text represent the value of one integer that I need to decrypt back to one byte of the original string...so I want to know what the standard mechanism for handling this is.
Your basic code for encrypting and decrypting each byte - the call to ModPow - is working, but you're going about the "splitting the message up and encrypting each piece" inappropriately.
To show that the ModPow part - i.e. the maths - is fine, here's code based on yours, which encrypts a string to a BigInteger[] and back:
using System;
using System.Linq;
using System.Numerics;
using System.Text;
class Test
{
const int p = 61;
const int q = 53;
const int n = 3233;
const int totient = 3120;
const int e = 991;
const int d = 1231;
static void Main()
{
var encrypted = Encrypt("Hello, world.", Encoding.UTF8);
var decrypted = Decrypt(encrypted, Encoding.UTF8);
Console.WriteLine(decrypted);
}
static BigInteger[] Encrypt(string text, Encoding encoding)
{
byte[] bytes = encoding.GetBytes(text);
return bytes.Select(b => BigInteger.ModPow(b, (BigInteger)e, n))
.ToArray();
}
static string Decrypt(BigInteger[] encrypted, Encoding encoding)
{
byte[] bytes = encrypted.Select(bi => (byte) BigInteger.ModPow(bi, d, n))
.ToArray();
return encoding.GetString(bytes);
}
}
Next you need to read more about how a byte[] is encrypted into another byte[] using RSA, including all the different padding schemes etc. There's a lot more to it than just calling ModPow on each byte.
But to reiterate, you should not be doing this to end up with a production RSA implementation. The chances of you doing that without any security flaws are very slim indeed. It's fine to do this for academic interest, to learn more about the principles of cryptography, but leave the real implementations to experts. (I'm far from an expert in this field - there's no way I'd start implementing my own encryption...)
Note: I updated this answer. Please scroll down to the update for how it should actually be implemented because this first way of doing it is not the correct way of doing RSA encryption.
One way I can think to do it is like this (but may not be compliant to standards), and also, note this does not pad:
public byte[] Encrypt(string m, Encoding encoding)
{
byte[] bytes = encoding.GetBytes(m);
byte[] returnBytes = new byte[0];
for (int i = 0; i < bytes.Length; i++)
{
byte[] result = BigInteger.ModPow(bytes[i], (BigInteger)e, n).ToByteArray();
int preSize = returnBytes.Length;
Array.Resize(ref returnBytes, returnBytes.Length + result.Length + 1);
(new byte[] { (byte)(result.Length) }).CopyTo(returnBytes, preSize);
result.CopyTo(returnBytes, preSize + 1);
}
return returnBytes;
}
public string Decrypt(byte[] c, Encoding encoding)
{
byte[] returnBytes = new byte[0];
for (int i = 0; i < c.Length; i++)
{
int dataLength = (int)c[i];
byte[] result = new byte[dataLength];
for (int j = 0; j < dataLength; j++)
{
i++;
result[j] = c[i];
}
BigInteger integer = new BigInteger(result);
byte[] integerResult = BigInteger.ModPow(integer, d, n).ToByteArray();
int preSize = returnBytes.Length;
Array.Resize(ref returnBytes, returnBytes.Length + integerResult.Length);
integerResult.CopyTo(returnBytes, preSize);
}
string decryptedString = encoding.GetString(returnBytes);
return decryptedString;
}
This has the potential of being cross-platform because you have the option of using a different datatype to represent e or n and pass it to a C# back-end service like that. Here is a test:
string stringToEncrypt = "Mary had a little lamb.";
Console.WriteLine("Encrypting the string: {0}", stringToEncrypt);
byte[] encryptedBytes = engine.Encrypt(stringToEncrypt, Encoding.UTF8);
Console.WriteLine("Encrypted text: {0}", Encoding.UTF8.GetString(encryptedBytes));
Console.WriteLine("Decrypted text: {0}", engine.Decrypt(encryptedBytes, Encoding.UTF8));
Output:
Encrypting the string: Mary had a little lamb.
Encrypted text: ☻6☻1♦☻j☻☻&♀☻g♦☻t☻☻1♦☻? ☻g♦☻1♦☻g♦☻?♥☻?☻☻7☺☻7☺☻?♥☻?♂☻g♦☻?♥☻1♦☻$☺☻
c ☻?☻
Decrypted text: Mary had a little lamb.
Update: Everything I said earlier is completely wrong in the implementation of RSA. Wrong, wrong, wrong! This is the correct way to do RSA encryption:
Convert your string to a BigInteger datatype.
Make sure your integer is smaller than the value of n that you've calculated for your algorithm, otherwise you won't be able to decypher it.
Encrypt the integer. RSA works on integer encryption only. This is clear.
Decrypt it from the encrypted integer.
I can't help but wonder that the BigInteger class was mostly created for cryptography.
As an example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace BytePadder
{
class Program
{
const int p = 61;
const int q = 53;
const int n = 3233;
const int totient = 3120;
const int e = 991;
const int d = 1231;
static void Main(string[] args)
{
// ---------------------- RSA Example I ----------------------
// Shows how an integer gets encrypted and decrypted.
BigInteger integer = 1000;
BigInteger encryptedInteger = Encrypt(integer);
Console.WriteLine("Encrypted Integer: {0}", encryptedInteger);
BigInteger decryptedInteger = Decrypt(encryptedInteger);
Console.WriteLine("Decrypted Integer: {0}", decryptedInteger);
// --------------------- RSA Example II ----------------------
// Shows how a string gets encrypted and decrypted.
string unencryptedString = "A";
BigInteger integer2 = new BigInteger(Encoding.UTF8.GetBytes(unencryptedString));
Console.WriteLine("String as Integer: {0}", integer2);
BigInteger encryptedInteger2 = Encrypt(integer2);
Console.WriteLine("String as Encrypted Integer: {0}", encryptedInteger2);
BigInteger decryptedInteger2 = Decrypt(encryptedInteger2);
Console.WriteLine("String as Decrypted Integer: {0}", decryptedInteger2);
string decryptedIntegerAsString = Encoding.UTF8.GetString(decryptedInteger2.ToByteArray());
Console.WriteLine("Decrypted Integer as String: {0}", decryptedIntegerAsString);
Console.ReadLine();
}
static BigInteger Encrypt(BigInteger integer)
{
if (integer < n)
{
return BigInteger.ModPow(integer, e, n);
}
throw new Exception("The integer must be less than the value of n in order to be decypherable!");
}
static BigInteger Decrypt(BigInteger integer)
{
return BigInteger.ModPow(integer, d, n);
}
}
}
Example output:
Encrypted Integer: 1989
Decrypted Integer: 1000
String as Integer: 65
String as Encrypted Integer: 1834
String as Decrypted Integer: 65
Decrypted Integer as String: A
If you are looking to use RSA encryption in C# then you should not be attempting to build your own. For starters the prime numbers you have chosen are probably to small. P and Q are supposed to be large prime numbers.
You should check out some other question/answers:
how to use RSA to encrypt files (huge data) in C#
RSA Encryption of large data in C#
And other references:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.encrypt(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx
This question already has answers here:
BitConverter.ToString() in reverse? [duplicate]
(7 answers)
Closed 8 years ago.
I have a function to calculate a SHA256 hash with a salt. The salt is then appended onto the hashed password and stored. The function for doing this looks like so:
public static string CalculateHash(string input)
{
var inputBuffer = new List<byte>(Encoding.Unicode.GetBytes(input));
var saltBytes = new byte[16];
using (var rnd = RandomNumberGenerator.Create())
{
rnd.GetBytes(saltBytes);
}
inputBuffer.AddRange(saltBytes);
byte[] hashedBytes;
using (var hasher = new SHA256Managed())
{
hashedBytes = hasher.ComputeHash(inputBuffer.ToArray());
}
var hash = BitConverter.ToString(hashedBytes).Replace("-", string.Empty);
var salt = BitConverter.ToString(saltBytes).Replace("-", string.Empty);
return string.Format("{0}:{1}", hash, salt);
}
It stores a string of 97 characters (including the ':') in length and seems to work well. I am struggling to strip the salt off of the hash when I retrieve it back. The issue I am having is converting the salt, in string form, back to a byte[16], which contains the original bytes. I assume once I have these original 16 bytes, I can append them to the user input, and hash the passwords to check for a match.
My current attempt is to split the hashed password and the salt at the colon delimiter and use a GetBytes function. It works like this:
public static bool ValidatePassword(string password, string hashWithSalt)
{
var split = hashWithSalt.Split(':');
var salt = split[1];
var saltBytes = GetBytes(salt);
}
private static byte[] GetBytes(string str)
{
var bytes = new byte[str.Length * sizeof(char)];
Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
But GetBytes is currently returning back byte[64], which is clearly not the original byte format. How can I go about fixing this?
Figured out my own solution. Just needed to change the GetBytes function to handle the hex conversion.
private static byte[] GetBytes(string str)
{
return Enumerable.Range(0, str.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(str.Substring(x, 2), 16))
.ToArray();
}
byte[] test = Form1.StrToByteArray("simpletext");
string encoded_text = BitConverter.ToString(test).Replace("-", "").ToLowerInvariant();
textBox1.Text = encoded_text;//73696d706c6574657874
as from this line "73696d706c6574657874" to get back "simpletext" ??
//StrToByteArray()
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
Do you absolutely have to use hex to start with? One slightly more efficient (and reversible with framework methods) option would be to use base 64:
string base64 = Convert.ToBase64String(test);
byte[] originalBytes = Convert.FromBase64String(base64);
string text = Encoding.ASCII.GetString(originalBytes);
I personally wouldn't suggest using ASCII as your encoding, however - UTF-8 will work the same way for ASCII characters, but allow all of Unicode to be encoded.
If you do have to use hex, you'll need a method to parse hex - I have an example here.
var input = "73696d706c6574657874";
var bytes = Enumerable
.Range(0, input.Length)
.Where(x => 0 == x % 2)
.Select(x => Convert.ToByte(input.Substring(x, 2), 16))
.ToArray();
Console.WriteLine(Encoding.ASCII.GetString(bytes));
should have done so
public static byte[] StrToByteArray(string str)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
//here im replace ASCIIEncoding to UTF8Encoding how said me Darin Dimitrov. respect man!!! very thx!!!!
and now i have result equal A-ґР^E—‹