-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrsadecryptionoaepsha1.html
151 lines (142 loc) · 7.61 KB
/
rsadecryptionoaepsha1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>RSA 2048 encryption with OAEP padding and SHA-1 hashing (decryption only)</title>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
h2 {font-size: 200%;}
p {font-size: 150%; }
button {height: 100%; font-size: 150%;}
textarea {font-size: 150%;}
input[type="text"] {font-size: 100%; }
input[type="button"] {font-size: 100%; }
</style>
</head>
<body>
<h1>RSA 2048 encryption with OAEP padding and SHA-1 hashing (decryption only)</h1>
<hr>
<p><b>Important note: this program is doing what it promises but the programming itself is of very poor quality and for demonstration purposes only. Never ever use this program as source for your own programs because there are a lot of conversions to get it run.</b>
<br><br>Get more information about this program on <a href="https://github.com/java-crypto/cross_platform_crypto/blob/main/docs/rsa_decryption_oaep_sha1_string.md" target="_blank">
my webpage <b>RSA string decryption with OAEP SHA1 padding</b></a><br>
<p>Insert your own Private Key (in PEM encoding):</p>
<textarea name="privatekey-value" id="privatekey-value" form= "myform" rows="10" cols="40">-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDwSZYlRn86zPi9e1RTZL7QzgE/36zjbeCMyOhf6o/WIKeVxFwVbG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Wsa9Y6d7qmxjEbmGkbGJBC+AioEYvFX9TaU8oQhvihgA6ZRNid58EKuZJBbe/3ek4/nR3A0oAVwZZMNGIH972P7cSZmb/uJXMOQKBgQCsFaQAL+4sN/TUxrkAkylqF+QJmEZ26l2nrzHZjMWROYNJcsn8/XkaEhD4vGSnazCu/B0vU6nMppmezF9Mhc112YSrw8QFK5GOc3NGNBoueqMYy1MG8Xcbm1aSMKVv8xbarh+BZQbxy6x61CpCfaT9hAoA6HaNdeoU6y05lBz1DQKBgAbYiIk56QZHeoZKiZxy4eicQS0sVKKRb24ZUd+04cNSTfeIuuXZrYJ48Jbr0fzjIM3EfHvLgh9rAZ+aHe/L84Ig17KiExe+qyYHjut/SC0wODDtzM/jtrpqyYa5JoEpPIaUSgPuTH/WhO3cDsx63PIW4/CddNs8mCSBOqTnoaxh
-----END PRIVATE KEY-----</textarea>
<section class="import-key spki">
<hr>
<p>Enter a message to decrypt (in Base64 encoding):</p>
<textarea name="ciphertextBase64" id="ciphertextBase64" rows="10" cols="40">JmTdIvJp4JueoqtCZ2xlm7DNKq9qytq3KIthaWzQkWOIf3o33kr69XH48O7NS1vdnKNr1+oidiGQJ3T5P8ukn9rYsHdTNwc8qBOp+ATusHL7LVtwCzdgUx6mWr7qe8YwPZZiAIg2RCOy17jaLvVCkoR+N5euk1+qH/eQyKbYA4/DNRXyJGxOb+7xb3gFuedJWvYcvf8eWJU2kFTVaHoFEqBdG2awOfVf0KaOS16z81qyHLIFiWmUePLyu4vtr1wx/G80Xq1Bkha0K2/IhDSn8J/s/uTxyORAfUDzVmyEdmXQFYEWri1TPcD7GDNV8GBaCWWgDccfcpfIZ3pzg/lKww==</textarea>
<hr>
<p><b>Instructions:</b><br>
1.: insert your own private key and press the "Import key" button<br>
2.: if the private key is valid the "Decrypt" button is enabled<br>
3.: enter the ciphertext message in Base64 encoding<br>
4.: press the "Decrypt button"<br>
5.: receive the decrypted plaintext</p><hr>
<input class="import-key-button" type="button" value="Import Key">
<input class="decrypt-button hidden" type="button" value="Decrypt" disabled>
</section>
<p>decrypted plaintext:</p>
<textarea name="decryptedtext" id="decryptedtext" rows="1" cols="50" readonly>the decrypted plaintext comes here...</textarea>
<hr><p><b>Technical note: this program uses the RSA encryption algorithm with a 2048 bit long key, OAEP padding and SHA-1 hash algorithm.</b></p><hr>
</body>
<SCRIPT LANGUAGE="JavaScript">
const bufToB64 = buf =>
btoa(Array.prototype.map.call(buf, ch => String.fromCharCode(ch)).join(""));
const bufToStr = str => new TextDecoder().decode(str);
const b64ToBuf = b64 => {
const binstr = atob(b64),
buf = new Uint8Array(binstr.length);
Array.prototype.forEach.call(binstr, (ch, i) => {
buf[i] = ch.charCodeAt(0);
});
return buf;
};
/*
The unwrapped encryption key.
*/
let privatekey;
const decryptButton = document.querySelector(".spki .decrypt-button");
/*
Convert a string into an ArrayBuffer
from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
*/
function str2ab(str) {
const buf = new ArrayBuffer(str.length);
const bufView = new Uint8Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
/*
Fetch the contents of the "message" textbox, and encode it
in a form we can use for the encrypt operation.
*/
function getMessageEncoding() {
const messageBox = document.querySelector("#plaintext");
const message = messageBox.value;
const enc = new TextEncoder();
return enc.encode(message);
}
/*
Import a PEM encoded RSA private key, to use for RSA-PSS signing.
Takes a string containing the PEM encoded key, and returns a Promise
that will resolve to a CryptoKey representing the private key.
*/
function importPrivateKey(pem) {
// fetch the part of the PEM string between header and footer
const pemHeader = "-----BEGIN PRIVATE KEY-----";
const pemFooter = "-----END PRIVATE KEY-----";
const pemContents = pem.substring(pemHeader.length, pem.length - pemFooter.length);
// base64 decode the string to get the binary data
const binaryDerString = window.atob(pemContents);
// convert from a binary string to an ArrayBuffer
const binaryDer = str2ab(binaryDerString);
return window.crypto.subtle.importKey(
"pkcs8",
binaryDer,
{
name: "RSA-OAEP",
hash: "SHA-1"
},
true,
["decrypt"]
);
}
async function decryptMessage() {
let ciphertext = b64ToBuf(document.getElementById("ciphertextBase64").value);
decryptedtext = await window.crypto.subtle.decrypt(
{
name: "RSA-OAEP"
},
privateKey,
ciphertext
);
document.getElementById("decryptedtext").value = bufToStr(decryptedtext);
}
function enableDecryptButton() {
decryptButton.classList.add('fade-in');
decryptButton.addEventListener('animationend', () => {
decryptButton.classList.remove('fade-in');
});
decryptButton.removeAttribute("disabled");
decryptButton.classList.remove("hidden");
}
/*
When the user clicks "Import Key"
- import the key
- enable the "Decrypt" button
*/
const importKeyButton = document.querySelector(".spki .import-key-button");
importKeyButton.addEventListener("click", async () => {
var pemEncodedKeyPriTextarea = document.getElementById("privatekey-value").value;
privateKey = await importPrivateKey(pemEncodedKeyPriTextarea);
enableDecryptButton();
});
decryptButton.addEventListener("click", decryptMessage);
</SCRIPT>
</html>