Last active
August 29, 2015 14:06
-
-
Save Katzy/c014dd000526a17154f5 to your computer and use it in GitHub Desktop.
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' | |
require 'Base64' | |
def encryption(data) | |
cipher = OpenSSL::Cipher::AES.new(128, :CBC) | |
cipher.encrypt | |
key = cipher.random_key | |
iv = cipher.random_iv | |
encrypted = cipher.update(data) + cipher.final | |
# encoded =Base64.encode64(encrypted).encode('utf-8') | |
# p encoded | |
p encrypted | |
# decoded=Base64.decode64(encrypted).encode('ascii-8bit') | |
decipher = OpenSSL::Cipher::AES.new(128, :CBC) | |
decipher.decrypt | |
# decipher.padding = 0 | |
decipher.key = key | |
decipher.iv = iv | |
plain = decipher.update(encrypted) + decipher.final | |
# p decoded | |
p plain | |
end | |
if __FILE__ == $0 | |
p encryption("This is crazy!") | |
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
put "/message/:id/encrypt" do | |
@message = Message.get(params[:id]) | |
wall_id = @message['wall_id'] | |
description = @message['description'] | |
# key = 'abcdefghabcdefghabcdefgh' | |
# iv = '1234567812345678' | |
cipher = OpenSSL::Cipher::AES.new(128, :CBC) | |
decipher = OpenSSL::Cipher::AES.new(128, :CBC) | |
key = cipher.random_key | |
iv = cipher.random_iv | |
if params[:secret] == SECRET_PASSWORD | |
cipher.encrypt | |
encrypted = cipher.update(description) + cipher.final | |
# encoded =Base64.encode64(encrypted).encode('utf-8') | |
@message.update(:description => encrypted) | |
show_params | |
p @message | |
p encrypted | |
redirect "/" | |
elsif params[:secret] == SECRET_PASSWORD_2 | |
encrypted = @message['description'] | |
# decoded=Base64.decode64(encrypted).encode('ascii-8bit') | |
decipher.decrypt | |
decipher.padding = 0 | |
decipher.key = key | |
decipher.iv = iv | |
plain = decipher.update(encrypted) + decipher.final | |
@message.update(:description => plain) | |
show_params | |
p plain | |
redirect "/" | |
else | |
"you did not provide the right password" | |
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
<p> | |
<h3><label>Title: <%= @wall.title %></h3> | |
</label> | |
<h3><label>Description: <%= @wall.description %></h3> | |
</label> | |
<h3><label>Author: <%= @wall.created_by %></h3> | |
</label> | |
<button><a href="/walls/<%= @wall.id %>/new_message">Write a Message on This Wall</a></button> | |
<% @messages.each do |message| %> | |
<p> Message id: <%= message.id %> </p> | |
<form method="POST" action="/message/<%= message.id %>/encrypt"> | |
<input type="hidden" name="_method" value="PUT"> | |
<p>Message: <%= message.description %></p><button>Encrypt or decrypt this message</button><input type="string" name="secret" placeholder="password"> | |
</form> | |
<br /> | |
<%= message.likes.to_s %> likes | |
<form method="POST" action="/walls/messages/<%= message.id %>/likes"> | |
<p><button>Like!</button></p> | |
</form> | |
<% end %> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment