This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'openssl' | |
| def sign_data(key, data) | |
| key.sign(OpenSSL::Digest::SHA256.new, data) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'openssl' | |
| def generate_RSA(bits=2048) | |
| # Generates an RSA keypair | |
| # @bits The key length in bits | |
| OpenSSL::PKey::RSA.new(bits) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| public function rsa_encrypt($key, $message) { | |
| $rsa = new Crypt_RSA(); | |
| $rsa->loadKey($key); | |
| $encrypted = base64_encode($rsa->encrypt($message)); | |
| return $encrypted; | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| public function rsa_decrypt($key, $package) { | |
| $rsa = new Crypt_RSA(); | |
| $rsa->loadKey($key); | |
| $decrypted = $rsa->decrypt(base64_decode($package)); | |
| return $decrypted; | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| public function rsa_decrypt($key, $package) { | |
| $rsa = new Crypt_RSA(); | |
| $rsa->setHash("sha256"); | |
| $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); | |
| $rsa->loadKey($key); | |
| $signature = base64_encode($rsa->sign(base64_decode($package))); | |
| return $signature; | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| public function rsa_verify_sign($key, $signature, $package) { | |
| $rsa = new Crypt_RSA(); | |
| $rsa->setHash("sha256"); | |
| $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); | |
| $rsa->loadKey($key); | |
| $verify = $rsa->verify(base64_decode($package), $signature); | |
| return $verify; | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| public function rsa_generate($bits=2048) { | |
| $rsa = new Crypt_RSA(); | |
| define('CRYPT_RSA_EXPONENT', 65537); | |
| $keypair = $rsa->createKey($bits); | |
| return $keypair; | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |