Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Created February 20, 2014 07:17
Show Gist options
  • Save KamilLelonek/9108495 to your computer and use it in GitHub Desktop.
Save KamilLelonek/9108495 to your computer and use it in GitHub Desktop.
Encryption between JS and Ruby
<!doctype html>
<html>
<head>
<title>JavaScript RSA Encryption</title>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="jsencrypt.min.js"></script>
<!-- https://github.com/travist/jsencrypt/tree/master/src -->
<script type="text/javascript">
$(function() {
$('#testme').click(function() {
var encrypt = new JSEncrypt();
encrypt.setPublicKey($('#pubkey').val());
var encrypted = encrypt.encrypt($('#input').val());
$('#result').val(encrypted);
});
});
</script>
</head>
<body>
<label for="pubkey">Public Key</label><br/>
<textarea id="pubkey" rows="15" cols="65">-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqoASU91zU5Li1LIEvBE1
eAociM8wFna4aT4y7inxvwvxML3dInVAr/jM/AOWOcrRIpJdH2R3ROLC4IX9Tui/
f65RwNLxNd/waCvl1pw3FCzERHTdL2ZBBhdpxuWkb/r07pfkDA9J4GRNDnYEsevB
FQw+tfK/e2S+kZh5VxH0SLjabaw7y+s9xYY4NXveSNd12dpbtI1xXa9EO4gVsyh8
gNtWtjt1Z7ypSxba+Dyxd9TMBtJ4Ho4rBjFrRijPggTWNCto0eVXKZ3NjBaDYUmW
drcBmx1tTjfcrUs+ZVi3KCp3ZTHyvcr3/A/mMZ6Id3e5og09U0lvjG3246ZzV2Jj
EwIDAQAB
-----END PUBLIC KEY-----</textarea><br/>
<label for="input">Text to encrypt:</label><br/>
<textarea id="input" name="input" type="text" rows=4 cols=70>This is a test!</textarea><br/>
<label for="result">Encrypted result:</label><br/>
<textarea id="result" name="result" type="text" rows=4 cols=70></textarea><br/>
<input id="testme" type="button" value="Test Me!!!" /><br/>
</body>
</html>
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYP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VwTRayrRQ5RYKlZal5p9DggwYv1ctXtK+BjmG48QNDIQoHoTeZLNbgH3jm8iHh
A28p/iDRJorKkDcy8/KuOR23Z/Z4iuQKHhT7vkeOXeBEx3+e4P0x7JvoqbE1H2GU
73P2YypSuPpjcptlhvvLwW+hwpO7rrVnvuETMawbzPAxmbQsYkYihJTiORDy0LCn
ycbrnGJdhvrLT9lhw6lywqTjeoPCYByBtHPPqjseNFi5Ei0IdarUFd/dM2lx4NrM
RjXzVbBs2GSdakNbzNlkyY7mJMVp9f554fx3i3Bpf6FuRY+N9chna2MKf115cME2
aMi4o5Aj4tmiWgQ0DCj4r16/t5a/w+f3lkpHpIWxRS0oiad+cX0a42ipl1XfAtFW
G5kjOm4ne0QFCh94njkzgmBLdn80Pf9t0ilYqIXe/Eq/sngXh+YiaZH/mljLEnbV
7Y/9j460xn5eHaetHxToi4reBz/3VY1FPe5ZmBuPTjPdrgtaXr0fvjTXElyTfkDT
MUyl9RIoVTgV5O6J6NZnwi05Sck9VQLJaoK3J5Ku4ax/gxUCk8aDOpWxr5cqMfXf
-----END RSA PRIVATE KEY-----
require 'openssl'
require 'base64'
message = '' # INSERT MESSAGE ENCRYPTED IN JS
password = 'ac7d45b46dd1dd03dbd20ddb03c98d9f'
key_file = File.read 'private.pem'
key = OpenSSL::PKey::RSA.new key_file, password
encrypted_massage = Base64.decode64 message
result = key.private_decrypt encrypted_massage
p result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment