Uninitialized Key Exception when using getK() in Elliptic Curve DSA on javacard - cryptography

When trying to use the getK() method on a key pair in JavaCard I get this exception code: CryptoException.UNINITIALIZED_KEY
Here is my code where I generate the key pair:
KeyPair key = new KeyPair(KeyPair.ALG_EC_FP, (short)256);
key.genKeyPair();
later on in the code I am trying to run
ECPublicKey eCPublicKey = (ECPublicKey) key.getPublic();
short hLeng = eCPublicKey.getK();
but this is when I get the exception thrown. Is there something else I need to do to init the key?

You need to have initialised the domain parameters a, b, g, k, r and the field. You can find a list of NIST recommended curves here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
getK() is throwing an exception because it has not been initialised.

Related

How to create openssh keys on iOS via Objective-C code

We can get openssh keys by order 'ssh-keygen...', or get openssl keys by order 'openssl genrsa...'. But can I do this in my iOS app via Objective-C code? Any help will be appreciated.
You'll need to use a library like OpenSSL to generate the keys. This isn't Objective-C, but you can interact with C libraries since Objective-C is a superset. You will need to read the documentation to fully understand how the library works, but this is a quick example:
RSA *myrsa;
unsigned long e = RSA_3;
BIO* out = NULL;
myrsa = RSA_generate_key(2048,e,NULL,NULL);
out=BIO_new(BIO_s_file());
if (myrsa == NULL) {
/* error handling here /*
}
You will want to write the key data out somewhere. Without a more specific example of what you are doing, this is the best I can offer.
For information on using OpenSSL libraries within your project, see: https://github.com/krzyzanowskim/OpenSSL

VB.net how to encrypt with private key using RSA

I keep trying to encrypt using my private RSA key, and decrypting with my public RSA key, but I can't decrypt without getting "Decrypt(): Key not valid for use in specified state."
Oh, and I realize that you usually encrypt with the public key - that's not what I'm trying to do. Please don't spam the thread with "learn how PKE works"
What I need this for is to be able to encrypt patches (about 200 bytes of base 64 text) on my computer, then have the program decrypt with the public key, proving I made the patch.
Can I get some help, or do you know of any libraries that would make this easier?
Decrypt function:
Public Shared Function Decrypt(ByVal Data() As Byte, ByVal Privatekey As String) As RSAResult
Try
Dim RSA As System.Security.Cryptography.RSACryptoServiceProvider = New System.Security.Cryptography.RSACryptoServiceProvider()
RSA.FromXmlString(Privatekey)
Dim Result As New RSAResult(RSADecrypt(Data, RSA.ExportParameters(True), False))
Return Result
Catch ex As Exception
Throw New Exception("Decrypt(): " & ex.Message, ex)
End Try
End Function
You can use RRSAParameters structure and RSACryptoServiceProvider's ImportParamaters method. This lets you create your own keys for encrypting with RSA.
For a definition of D, Dp, Dq, Exponent, InverseQ, Modulus, P, and Q, I suggest checking out the Wikipedia page on RSA for Cryptography. This covers how to generate a key, as well as what each parameter is for. Towards the bottom there is even a representation of the algorithm.

Getting key without iterating in YAML-cpp

Is there an easy way to get the key in a map using YAML-cpp without iterating? I see in the instructions that I can use the first() method of the YAML-cpp iterator class, but I don't need to actually iterate. I have a key that I can identify by indentation level, but what I need to do now is throw an exception if the key is not one of a known list. My code currently looks like this:
std::string key;
if (doc.FindValue("goodKey")
{
// do stuff
key = "goodKey";
} else if (doc.FindValue("alsoGoodKey") {
// do stuff
key = "alsoGoodKey";
} else throw (std::runtime_error("unrecognized key");
// made it this far, now let's continue parsing
doc[key]["otherThing"] >> otherThingVar;
// etc.
See, I need the key string to continue parsing because otherThing is under goodKey in the YAML file. This works fine, but I'd like to tell the user what the unrecognized key was. I don't know how to access it, though. I don't see any functions in the header file that give me that value. How do I get it?
There isn't necessarily a single "unrecognized key". For example, your YAML file might look like:
someKey: [blah]
someOtherKey: [blah]
or it might even be empty, e.g., {}. In the former case, which one do you want? In the latter case, there are no keys.
You're asking for how to "get the key in a map", but maps can have zero or more keys.
As a side note, you can simplify some of this using the actual result of FindValue, assuming you don't actually need the name of the key that you grabbed. For example:
const YAML::Node *pNode = doc.FindValue("goodKey");
if(!pNode)
pNode = doc.FindValue("alsoGoodKey");
if(!pNode)
throw std::runtime_error("unrecognized key");
(*pNode)["otherThing"] >> otherThingVar;
// etc

Generating a key

I am writing an encryption application that requires a 64 bit key. I am currently using the following code to automatically generate a key.
Function GenerateKey() As String
' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()
' Use the automatically generated key for encryption.
Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)
End Function
I am wanting the user to create their own key. Can I convert a user defined password (a string) into a 64 bit key that can be used?
The answer depends on how secure you want it to be, I'm no security expert so I wouldn't give advice on it.
I did see this though: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes.aspx It can be used to derives bytes from a string key and salt in the way Jodrell eluded to, and would be far better than rolling yor own.
The other constructor that might be suited after that stage is detailed here: http://msdn.microsoft.com/en-us/library/51cy2e75.aspx
I'm sure if you searched for that on the web you could find examples of how to use it.

How can I produce a valid Private Key (in any format) from the primes?

I have the necessary components to build the private/public RSA key: N, E, D, P, & Q. I can derive the other values for the CRT if needed. How can I use these parameters to produce a valid Private/Public key in DER, PEM, or PKCS#7? It doesn't matter which to me as I can convert between them. I'm looking for a tool, library, script, or program that supports assigning these values and writing out a valid file, so I don't have to read hundreds of pages on ASN.1. Every tool I've looked at doesn't expose this use case in the API.
If you are using Java, there is the RSAPrivateKeySpec class, which takes BigInteger arguments of modulus and exponent. There are subclasses which take instead of N the P and Q primes (for more efficiency). You can then use a KeyFactory to convert this object to a Key, and then to some EncodedKeySpec class like PKCS8EncodedKeySpec or X509EncodedKeySpec.
You should be able to use OpenSSL (the library).
BIGNUM is described here. There are many functions to create a BIGNUM out of your input (depending on format). BN_bin2bn is probably the simplest, but hexadecimal or decimal input is also supported.
RSA is a structure that contains BIGNUMs. It's described here. You can initialise it yourself (directly).
You can then write the RSA structure using one of the PEM functions (described here). For example, PEM_write_RSAPrivateKey writes a PEM file containing a private key (you can also encrypt it, if you want to).
I'm not aware of any tool that does all of the above. Also, ASN.1 isn't so bad (a public/private key is pretty much a SEQUENCE of INTEGERs).