Skip to content

Instantly share code, notes, and snippets.

Avatar

LaunchKey Documentation Examples lkdocs

View GitHub Profile
View Python PyCrypto: Generate RSA Keys Example.py
def generate_RSA(bits=2048):
'''
Generate an RSA keypair with an exponent of 65537 in PEM format
param: bits The key length in bits
Return private key and public key
'''
from Crypto.PublicKey import RSA
new_key = RSA.generate(bits, e=65537)
public_key = new_key.publickey().exportKey("PEM")
private_key = new_key.exportKey("PEM")
View Python PyCrypto: Verify Signature Example.py
def verify_sign(public_key_loc, signature, data):
'''
Verifies with a public key from whom the data came that it was indeed
signed by their private key
param: public_key_loc Path to public key
param: signature String signature to be verified
return: Boolean. True if the signature is valid; False otherwise.
'''
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
View Ruby OpenSSL: Verify Signature Example.rb
require 'openssl'
def verify_sign(key, signature, data)
# Verifies with a public key that the data was signed with their private key
pubkey = key.public_key
if pubkey.verify(OpenSSL::Digest::SHA256.new, signature, data)
puts 'the signature is valid'
else
puts 'the signature is invalid'
end
end
View Python PyCrypto: Sign Data Example.py
def sign_data(private_key_loc, data):
'''
param: private_key_loc Path to your private key
param: package Data to be signed
return: base64 encoded signature
'''
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from base64 import b64encode, b64decode
View Objective-C CommonCrypto: Generate RSA Keys Example.m
+(void)generateKeyPairWithPublicTag:(NSString *)publicTagString privateTag:(NSString *)privateTagString {
NSMutableDictionary *privateKeyAttr = [[NSMutableDictionary alloc] init];
NSMutableDictionary *publicKeyAttr = [[NSMutableDictionary alloc] init];
NSMutableDictionary *keyPairAttr = [[NSMutableDictionary alloc] init];
NSData *publicTag = [publicTagString dataUsingEncoding:NSUTF8StringEncoding];
NSData *privateTag = [privateTagString dataUsingEncoding:NSUTF8StringEncoding];
SecKeyRef publicKey = NULL;
SecKeyRef privateKey = NULL;
View Java Spongy Castle: Sign Data Example.java
private byte[] signWithPrivateKey(byte[] bytes, String privateKey) throws Exception {
Signature signature = Signature.getInstance("SHA256withRSA", "SC");
signature.initSign(Crypto.getRSAPrivateKeyFromString(privateKey));
signature.update(bytes);
return signature.sign();
}
View Java Spongy Castle: Encrypt Message Example.java
private byte[] encryptWithPublicKey(byte[] message, String publicKey) throws Exception {
String strippedKey = Crypto.stripPublicKeyHeaders(publicKey);
PublicKey apiPublicKey= Crypto.getRSAPublicKeyFromString(strippedKey);
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding", "SC");
rsaCipher.init(Cipher.ENCRYPT_MODE, apiPublicKey);
return rsaCipher.doFinal(message);
}
View Java Spongy Castle: Decrypt Package Example.java
public static byte[] decryptWithPrivateKey(byte[] message, String privateKey) throws Exception {
PrivateKey pKey = getRSAPrivateKeyFromString(privateKey);
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding", "SC");
rsaCipher.init(Cipher.DECRYPT_MODE, pKey);
return rsaCipher.doFinal(message);
}
View Python PyCrypto: Decrypt Package Example.py
def decrypt_RSA(private_key_loc, package):
'''
param: public_key_loc Path to your private key
param: package String to be decrypted
return decrypted string
'''
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from base64 import b64decode
key = open(private_key_loc, "r").read()
View Python PyCrypto: Encrypt Message Example.py
def encrypt_RSA(public_key_loc, message):
'''
param: public_key_loc Path to public key
param: message String to be encrypted
return base64 encoded encrypted string
'''
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
key = open(public_key_loc, "r").read()
rsakey = RSA.importKey(key)