I am able to retrieve the public key contents from a cert file using openssl by following the link http://fm4dd.com/openssl/certpubkey.htm
However, it prints the public key to stdout. I want to be able to store it in a string . Is there a way to get it into a char buffer or a string ?
The line PEM_write_bio_PUBKEY(outbio, pkey) prints to stdout.
Searched quite a bit, wasnt able to get what I wanted.
Any help will be appreciated, better if the code sample is provided as I'm running out of time.
Thanks in advance.
I found a solution finally !
X509 structure to human readable string
Just replaced with EVP_PKEY *key in X509_to_PEM() signature and used PEM_write_bio_PUBKEY(bio, key) instead of PEM_write_bio_X509(bio, cert).
Worked !
I ran into some issues writing a small test programme using the cryptoki library. I want (have) to get hold of the RSA private key (all its parameters). I thought about either generate keys and then extract the parameters or use already generated parameters to set the keys manually. So far, I don't get any of this working. Code is at the end of the post.
Extraction
I know that there is the C_GetAttributeValue() with which I can extract attributes such as the public exponent or the modulus. That works for both public and private key objects but I get CKR_ATTRIBUTE_SENSITIVE error for when I try to extract private parameters from the private key object. Is there a way to extract these attributes? Can/Do I have to set certain parameters when logging into a session or during initialising?
Setting Keys manually
My second approach was to read key material (generated with OPENSSL) from a file and use that for generating key objects with C_CreateObject(). The file contains all RSA parameters (n,e,d,p,q,dmp1,dmq1,iqmp). After reading, I convert them from ASCII to hex representation and store them in a CK_BYTE[]. So far so good. Now, when I pass all this to the C_CreateObject() in order to create a private key I receive a CKR_ATTRIBUTE_VALUE_INVALID error message. Creating a public key object the same way with the public parameters works. I verified that using C_GetAttributeValue() on the created public key object.
What am I missing for generating a private key object if this is even possible this way?
I suppose that C_GenerateKeyPair() always generates new keys no matter whether key material is provided or not, right?
C Code
This is what I try to create the private key object with:
CK_OBJECT_HANDLE hPrivateKeys[NUMKEYS];
CK_KEY_TYPE kType= CKK_RSA;
CK_OBJECT_CLASS kClass = CKO_PRIVATE_KEY;
CK_BYTE id[] = {123};
CK_UTF8CHAR label[] = "An RSA private key object";
// sn,sd,se, etc contain the length of the respective parameter
CK_ATTRIBUTE privateKeyTemplate[] = {
{CKA_CLASS, &kClass, sizeof(kClass)},
{CKA_KEY_TYPE, &kType, sizeof(kType)},
{CKA_TOKEN, &false, sizeof(false)},
{CKA_PRIVATE, &false, sizeof(false)},
{CKA_SENSITIVE, &false, sizeof(false)},
{CKA_EXTRACTABLE, &true, sizeof(true)},
{CKA_ID, id, sizeof(id)},
{CKA_SUBJECT, NULL_PTR, 0},
{CKA_DECRYPT, &true, sizeof(true)},
{CKA_SIGN, &true, sizeof(true)},
{CKA_LABEL, label, sizeof(label)-1},
{CKA_ID, id, sizeof(id)},
{CKA_MODULUS, modulus, sn},
{CKA_PUBLIC_EXPONENT, publicExponent, se},
{CKA_PRIVATE_EXPONENT, privateExponent, sd},
{CKA_PRIME_1, prime1, sp},
{CKA_PRIME_2, prime2, sq},
{CKA_EXPONENT_1, exponent1, sdmp1},
{CKA_EXPONENT_2, exponent2, sdmq1},
{CKA_COEFFICIENT, coefficient, siqmp}
};
CK_ATTRIBUTE publicKeyTemplate[] = {
{CKA_ENCRYPT, &true, sizeof(true)},
{CKA_VERIFY, &true, sizeof(true)},
{CKA_WRAP, &true, sizeof(true)},
{CKA_MODULUS_BITS, &modulusBits, sizeof(modulusBits)},
{CKA_PUBLIC_EXPONENT, publicExponent, se},
{CKA_MODULUS, modulus, sn}
};
rv = pFunctionList->C_CreateObject(hSession, privateKeyTemplate, NUM_ELEM(privateKeyTemplate), &hPrivateKeys[j]);
Your idea of generating a key pair and then reading it out is fine, you should however set the attribute CKA_SENSITIVE to false in the template of the private key. Note that it always depends on the token itself if such functionality is supported.
Usually when extracting private key information from a token you want to have it encrypted. Encryption of keys is called wrapping, and the possible extraction of sensitive information is managed by the CKA_EXTRACTABLE attribute.
After reading, I convert them from ASCII to hex representation and store them in a CK_BYTE[].
The PKCS#11 token interface specifies precisely how to encode / decode attributes. Just trying formats haphazardly is not going to give any results.
So I am programming with windows forms applications in Visual studios and I want to get the text from a text box and do stuff with it. It says that the text is a String ^. First of all what does the ^ mean? It also won't let me use that as a normal string so can i convert it to a normal string and vice-versa?
This is C++/CLI. A String^ is a handle to a managed System::String instance.
The best way to convert is to use the built in marshaling library:
#include <msclr/marshal_cppstd.h>
// given System::String^ managedString
std::string normalString = msclr::interop::marshal_as<std::string>(managedString);
I need to calculate the md5 of a file saved locally in an windows8 app created in
javascript / html.
I need the md5 to compare it with an online file and see if the two objects are really the same.
What function should I use?
Here's the code I use to hash a string in MD5 using Windows.Security.Cryptography namespace:
var inputBuffer, outputBuffer, toHash, hashed,
hashProvider = Windows.Security.Cryptography.Core.HashAlgorithmProvider.openAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.md5); // "open" an md5 hash provider
toHash = 'string'; // string to hash
inputBuffer = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(toHash, Windows.Security.Cryptography.BinaryStringEncoding.utf8); // convert string to binary
outputBuffer = hashProvider.hashData(inputBuffer); // hash the binary
hashed = Windows.Security.Cryptography.CryptographicBuffer.encodeToHexString(outputBuffer); // the hashed string
Now, all you need to do is to read the file in (see http://msdn.microsoft.com/en-us/library/windows/apps/hh464978.aspx). If reading the file into a buffer, then you wouldn't need the convertStringToBinary line.
The WinRT API provides SHA functionality in the Windows.Security.Cryptography.Core namespace, specifically through the static method HashAlgorithmProvider.openAlgorithm(Windows.Security.Cryptography.Certificates.HashAlgorithmNames.sha256).
This provides you with a HashAlgorithmProvider class that has methods like hashData.
Fin in this Link is all that's available in winjs in terms of cryptography.
And Here you can find an example on how to implement an MD5 calculator for a string, from there is a good start to make it work for a file.
Hope this helps.
I'm looking for one line code examples in various languages for getting a valid MD5 result (as a string, not a bytehash or what have you). For instance:
PHP:
$token = md5($var1 . $var2);
I found VB especially troublesome to do in one line.
C#:
string hash = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(input, "md5");
VB is virtually the same.
Here it is not using the System.Web namespace:
string hash = Convert.ToBase64String(new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)));
Or in readable form:
string hash =
Convert.ToBase64String
(new System.Security.Cryptography.MD5CryptoServiceProvider()
.ComputeHash
(System.Text.Encoding.UTF8.GetBytes
(input)
)
);
There is a kind of universality in how this is to be accomplished. Typically, one defines a routine called md5_in_one_line (or Md5InOneLine) once, and uses it all over the place, just as one would use a library routine.
So for example, once one defines Md5InOneLine in C#, it's an easy one-liner to get the right results.
Python
token = __import__('md5').new(var1 + var2).hexdigest()
or, if md5 is alrady imported:
token = md5.new(var1 + var2).hexdigest()
Thanks to Greg Hewgill
Aren't you really just asking "what languages have std. library support for MD5?" As Justice said, in any language that supports it, it'll just be a function call storing the result in a string variable. Even without built-in support, you could write that function in any language!
Just in case you need VBScript:
download the MD5 class from webdevbros and then with one line:
hash = (new MD5).hash("some value")
Does it really matter if you can do MD5 in one line. If it's that much trouble that you can't do it in VB in 1 line, then write your own function. Then, when you need to do MD5 in VB in one line, just call that function.
If doing it all in 1 line of code is all that important, here is 1 line of VB. that doesn't use the System.Web Namespace.
Dim MD5 As New System.Security.Cryptography.MD5CryptoServiceProvider() : Dim HashBytes() As Byte : Dim MD5Str As String = "" : HashBytes = MD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes("MyString")) : For i As Integer = 0 To HashBytes.Length - 1 : MD5Str &= HashBytes(i).ToString("x").PadLeft(2, "0") : Next
This will hash "MyString" and store the MD5 sum in MD5Str.
Coldfusion has a bunch of hashing algorithms, MD5 is the default.
cfset var md5hashresult = hash("string to hash")