Regular expression for base 58 private key? - bitcoin

I'm looking for a regex that will validate a base 58 bitcoin private key. I found this one for public addresses:
/^[13n][1-9A-Za-z][^OIl]{20,40}/
But I don't know what the requirements are for a private key.

var regex = /^[5KL][1-9A-HJ-NP-Za-km-z]{50,51}$/
In javascript.
All valid WIF private keys will match this, and the only extra stuff this will match is "WIF private key"-ish strings with invalid checksums.
Shortest possible WIF key (all 00 bytes, missing the compressed 01 byte) 51 length
5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAbuatmU
Longest possible WIF key (all ff bytes with the extra 01 compression byte) 52 length
L5oLkpV3aqBjhki6LmvChTCq73v9gyymzzMpBbhDLjDpKCuAXpsi

^5[HJK][0-9A-Za-z&&[^0OIl]]{49}

/^5[HJK][1-9A-Za-z][^OIl]{49}/
From https://en.bitcoin.it/wiki/Base58Check_encoding:
Such encodings will always yield a 51-character string that starts with '5', or more specifically, either '5H', '5J', or '5K'.

In python:
import re
re.search("^5[HJK][1-9A-Za-z][^OIl]{48}$", wif)

Related

VB Xor - double-encryption

I am coding in VB6, but I can rewrite it for VB.net if it would help.
I am using XOR to do some basic encryption: printa = printa + Chr((q Xor chCode))
I am converting the result back to characters. The problem comes when I double encrypt. I think the problem arises when a XOR operation resulting in a 0. Hence, Chr(0) = a null character. PS: the chCode can be anything 1 to 254. Also when I double encrypt the source text is often out side the range of printable characters.
Any help would be appreciated. I'll try to give more info if needed. Thx
Encryption is an operation that works on all bits of the message and the result is no longer within the printable area, as you state.
There is no inherent problem with handling such a string internally in VB though, since it stores the string length at the beginning of the string, rather than using null-termination like in C.
You should look at base64-encoding your encrypted message to get back to the printable range if that is what you need. What that does is basically using 4 bytes to represent 6 bits from 3 bytes each.

Decoding a coded variable

I use this function in order to encode an input:
Public Function encodeStrings(ByVal MyPass As String, ByVal MyUName As String) As String
Dim ENPass As String = Nothing
Dim password As String = (MyPass + MyUName)
Dim mhash As HashAlgorithm = New SHA1CryptoServiceProvider
Dim bytValue() As Byte = System.Text.Encoding.UTF8.GetBytes(password)
Dim bytHash() As Byte = mhash.ComputeHash(bytValue)
mhash.Clear()
ENPass = Convert.ToBase64String(bytHash)
Return ENPass
End Function
Now I need to decode this variable and get back the original string.
Does anyone know how to do that? I'm using VB.NET in my project.
No, there is no way to decode it. The SHA1CryptoServiceProvider implements a hashing algorithm. According to the MSDN:
Computes the SHA1 hash value for the input data...
SHA-1 is, by definition a hashing algorithm. According to the Wikipedia article:
In cryptography, SHA-1 is a cryptographic hash function designed by the United States National Security Agency and published by the United States NIST as a U.S. Federal Information Processing Standard. SHA-1 produces a 160-bit (20-byte) hash value. A SHA-1 hash value is typically expressed as a hexadecimal number, 40 digits long.
That is why it inherits from the HashAlgorithm base-class rather than the SymmetricAlgorithm base-class. Since it uses a hashing algorithm, that means that it is one-way encoded. The original string will always result in the same encrypted value, but there is no way to decrypt it to get back to the original value, since many different strings could result in that same encrypted value (although, with cryptographic hashing algorithms, such as SHA1, that is unfeasible).
If you think about it, that's the only thing that makes sense. If the SHA-1 algorithm always results in a hash code which is 20 bytes long, then that means you could encode a 1 gigabyte string and it would still only be 20 bytes long as an SHA-1 encrypted hash value. Surely you don't think that those 20 bytes contain all of the data that was originally stored in that 1 GB string. If that were the case, zipping files would be far more effective :)
Typically, when a cryptographic hashing algorithm is being used, it would be for something like storing a password. In that case, the issue is not how to decrypt the password, but rather, to encrypt the newly entered password and then compare the two encrypted values to see if they are the same. If the two encrypted values are the same, then you know that the user entered the right password. If the two encrypted values are not the same, then you know that it is incorrect (but you still don't know what the right password would be).
If you need a 2-way encryption algorithm, you need to use one which inherits from SymmetricAlgorithm, such as the AesCryptoServiceProvider class.

How Do I Convert a Byte Stream to a Text String?

I'm working on a licensing system for my application. I'd like to put all licensing information (licensee name, expiration date, and enabled features) into an object, encrypt that object with a private key, then represent the encrypted data as a single text string which I can send via email to my customers.
I've managed to get the encrypted data into a byte stream, but I don't know how to convert that byte stream into a text value -- something that contains no control characters or whitespace. Can anyone offer advice on how to do that? I've been researching the Encoding class, but I can't find a text-only encoding.
I'm using Net 2.0 -- mostly VB, but I can do C# also.
Use a Base64Encoder to convert it to a text string that can be decoded with a Base64Decoder. It is great for representing arbitary binary data in a text friendly manner, only upper and lower case A-Z and 0-9 digits.
BinHex is an example of one way to do that. It may not be exactly what you want -- for example, you might want to encode your data such that it's impossible to inadvertently spell words in your string, and you may or may not care about maximizing the density of information. But it's an example that may help you come up with your own encoding.
I've found Base32 useful for license keys before. There are some C# implementations linked from this answer. My own license code is based on this implementation, which avoids ambiguous characters to make it easier to retype the keys.

ssh-2 rsa body format?

Is there any documentation on what the bignums in the body are?
Like here is an example pubkey.
ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIBMgigwcBaB3sqogC/yVPANQJKjMwOvrdOKhBIVlI2eOIq6FEyzNI8cbRDbHo5muipun922bLoV+p/dPZ2/8wJ+zugLM8eCdsr+VbYZ3T5wb7bjd2swePOWtffnsXnHiP8aWyRj64vd/mRPR1gYErg8vb7vM5c4B2ANKHs/rP65Bw== rsa-key-20100514
It contains 3 bignums with the sizes 7, 1, 128. Now I am assuming the 128 is the key while the 7 is the shared. But what about the other one? Also the privkey contains a lot more bignums which I have no idea what they are.
Edit:
So apparently what I am looking for is the format of the private key blob. I have searched and all I have found are references to the name and not the actual format.
The number with size 1 is the "version". What you observe is the encoding of a structure which contains the key elements. The structure follows ASN.1 conventions and encoding rules. In the case of RSA, the contents of the public and private keys are specified by PKCS#1 (see annex C for the ASN.1 specification, and section 3 for the human readable text).

What's the suffix (type character) for "Byte" numeric constants in VB.NET?

Just out of curiosity:
I know I can tell the compiler if I want a value to be interpreted as a certain numeric type, e.g. as Integer (32 bit signed), this way appending an "I" (type character) to the constant value:
Private Function GetTheAnswerAsInteger() As Integer
Return 42I
End Function
There's also "S" for Short, "D" for Decimal, etc.
But what is the suffix for Byte? Hint: it's not the obvious one "B"...
There isn't one. If you need to distinguish between an integer and a byte (e.g. to call an appropriate overload) for a constant, you need to cast.
(The same is true in C#, by the way.)
MSDN provides confirmation:
Byte has no literal type character or
identifier type character.
There's also a list of type characters and literal suffixes.
So, we added binary literals in VB last fall and got similar feedback
from early testers. We did decide to add a suffix for byte for VB. We
settled on SB (for signed byte) and UB (for unsigned byte). The reason
it's not just B and SB is two-fold.
One, the B suffix is ambiguous if you're writing in hexadecimal (what
does 0xFFB mean?) and even if we had a solution for that, or another
character than 'B' ('Y' was considered, F# uses this) no one could
remember whether the default was signed or unsigned - .NET bytes are
unsigned by default so it would make sense to pick B and SB but all
the other suffixes are signed by default so it would be consistent
with other type suffixes to pick B and UB. In the end we went for
unambiguous SB and UB.
-- Anthony D. Green,
https://roslyn.codeplex.com/discussions/542111
It has been integrated to the upcoming VB.NET release, and this is the way it will work:
Public Const MyByte As Byte = 4UB;
Public Const MyByte2 As SByte = 4SB;
This answer does not really provide a suffix, but it's as close as it gets.
If you define an extension method as
Imports System.Runtime.CompilerServices
Module IntegerExtensions
<Extension()> _
Public Function B(ByVal iNumber As Integer) As Byte
Return Convert.ToByte(iNumber)
End Function
End Module
You can use it like this:
Private Function GetTheAnswerAsByte() As Byte
Return 42.B
End Function
There's no byte literal in .NET.