Using SHA1 and RSA with java.security.Signature vs. MessageDigest and Cipher

OK, I’ve worked out what’s going on. Leonidas is right, it’s not just the hash that gets encrypted (in the case of the Cipher class method), it’s the ID of the hash algorithm concatenated with the digest: DigestInfo ::= SEQUENCE { digestAlgorithm AlgorithmIdentifier, digest OCTET STRING } Which is why the encryption by the Cipher … Read more

Calculate RSA key fingerprint

Run the following command to retrieve the SHA256 fingerprint of your SSH key (-l means “list” instead of create a new key, -f means “filename”): $ ssh-keygen -lf /path/to/ssh/key So for example, on my machine the command I ran was (using RSA public key): $ ssh-keygen -lf ~/.ssh/id_rsa.pub 2048 00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff /Users/username/.ssh/id_rsa.pub (RSA) To get the … Read more

Read RSA private key of format PKCS1 in JAVA

Java does not come with out-of-the-box support for PKCS1 keys. You can however use Bouncycastle PEMParser pemParser = new PEMParser(new FileReader(privateKeyFile)); JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(“BC”); Object object = pemParser.readObject(); KeyPair kp = converter.getKeyPair((PEMKeyPair) object); PrivateKey privateKey = kp.getPrivate();

RSA implementations in Objective C

I have tried RSA Encryption and Decryption for NSString. Here is the code: Add Security.Framework to your project bundle. ViewController.h code is as follows: #import <UIKit/UIKit.h> #import <Security/Security.h> @interface ViewController : UIViewController { SecKeyRef publicKey; SecKeyRef privateKey; NSData *publicTag; NSData *privateTag; } – (void)encryptWithPublicKey:(uint8_t *)plainBuffer cipherBuffer:(uint8_t *)cipherBuffer; – (void)decryptWithPrivateKey:(uint8_t *)cipherBuffer plainBuffer:(uint8_t *)plainBuffer; – (SecKeyRef)getPublicKeyRef; – … Read more

Verifying JWT signed with the RS256 algorithm using public key in C#

Thanks to jwilleke, I have got a solution. To verify the RS256 signature of a JWT, it is needed to use the RSAPKCS1SignatureDeformatter class and its VerifySignature method. Here is the exact code for my sample data: string tokenStr = “eyJraWQiOiIxZTlnZGs3IiwiYWxnIjoiUlMyNTYifQ.ewogImlzcyI6ICJodHRwOi8vc2VydmVyLmV4YW1wbGUuY29tIiwKICJzdWIiOiAiMjQ4Mjg5NzYxMDAxIiwKICJhdWQiOiAiczZCaGRSa3F0MyIsCiAibm9uY2UiOiAibi0wUzZfV3pBMk1qIiwKICJleHAiOiAxMzExMjgxOTcwLAogImlhdCI6IDEzMTEyODA5NzAsCiAiY19oYXNoIjogIkxEa3RLZG9RYWszUGswY25YeENsdEEiCn0.XW6uhdrkBgcGx6zVIrCiROpWURs-4goO1sKA4m9jhJIImiGg5muPUcNegx6sSv43c5DSn37sxCRrDZZm4ZPBKKgtYASMcE20SDgvYJdJS0cyuFw7Ijp_7WnIjcrl6B5cmoM6ylCvsLMwkoQAxVublMwH10oAxjzD6NEFsu9nipkszWhsPePf_rM4eMpkmCbTzume-fzZIi5VjdWGGEmzTg32h3jiex-r5WTHbj-u5HL7u_KP3rmbdYNzlzd1xWRYTUs4E8nOTgzAUwvwXkIQhOh5TPcSMBYy6X3E7-_gr9Ue6n4ND7hTFhtjYs3cjNKIA08qm5cpVYFMFMG6PkhzLQ”; string[] tokenParts = tokenStr.Split(‘.’); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters( new RSAParameters() { Modulus … Read more