一区二区三区无码免费网站_国产日韩中文字幕在线_乱伦三级中文综合_欧美日韩中文字幕国产_国产色播av在线_翘臀美女XX00后进式片_正品蓝导航福利找AV导航_131少妇爱做高清免费视频_久久精品影视少妇_爆乳美女午夜视频精品

聰明屋視角

關(guān)注互聯(lián)網(wǎng),關(guān)注技術(shù)開發(fā),透析與分享移動互聯(lián)網(wǎng)行業(yè)最新動態(tài)

RSA加密解密

時(shí)間:2019-02-18 18:16:53    閱讀:59445次 分類:APP開發(fā)
舉個通俗易懂的例子,假設(shè)我們再登錄一個網(wǎng)站,發(fā)送賬號和密碼,請求被攔截了。 密碼沒加密,那攻擊者就直接拿到了密碼,這是最搓的。 密碼加密了,是不可逆加密,那攻擊者只需要模擬那個請求即可登錄。 密碼加密了...

舉個通俗易懂的例子,假設(shè)我們再登錄一個網(wǎng)站,發(fā)送賬號和密碼,請求被攔截了。

  • 密碼沒加密,那攻擊者就直接拿到了密碼,這是最搓的。
  • 密碼加密了,是不可逆加密,那攻擊者只需要模擬那個請求即可登錄。
  • 密碼加密了,是可逆加密,其中攜帶有時(shí)間等參數(shù),后臺可以根據(jù)時(shí)間等參數(shù)來判斷是否有效,但因?yàn)槭窃谇岸思用?,其加密方式也能在代碼中找到,找到加密方式就可以得出解密方式。

但是如果我們使用非對稱加密就可以避免以上問題。

非對稱加密算法需要兩個密鑰來進(jìn)行加密和解密,這兩個秘鑰是公開密鑰(public key,簡稱公鑰)和私有密鑰(private key,簡稱私鑰)。

工作過程如下,甲乙之間使用非對稱加密的方式完成了重要信息的安全傳輸。

非對稱加密工作過程簡要示意圖

  • 乙方生成一對密鑰(公鑰和私鑰)并將公鑰向其它方公開。
  • 得到該公鑰的甲方使用該密鑰對機(jī)密信息進(jìn)行加密后再發(fā)送給乙方。
  • 乙方再用自己保存的另一把專用密鑰(私鑰)對加密后的信息進(jìn)行解密。乙方只能用其專用密鑰(私鑰)解密由對應(yīng)的公鑰加密后的信息。

在傳輸過程中,即使攻擊者截獲了傳輸?shù)拿芪模⒌玫搅艘业墓€,也無法破解密文,因?yàn)橹挥幸业乃借€才能解密密文。
同樣,如果乙要回復(fù)加密信息給甲,那么需要甲先公布甲的公鑰給乙用于加密,甲自己保存甲的私鑰用于解密。

在非對稱加密中使用的主要算法有:RSA、Elgamal、背包算法、Rabin、D-H、ECC(橢圓曲線加密算法)等。

下面我們通過一個例子來說明如何用PHP來實(shí)現(xiàn)RSA的加密解密。

<?php class Rsa { public $privateKey = ''; public $publicKey = ''; public function __construct() {
        $resource = openssl_pkey_new();
        openssl_pkey_export($resource, $this->privateKey);
        $detail = openssl_pkey_get_details($resource); $this->publicKey = $detail['key'];
    } public function publicEncrypt($data, $publicKey) {
        openssl_public_encrypt($data, $encrypted, $publicKey); return $encrypted;
    } public function publicDecrypt($data, $publicKey) {
        openssl_public_decrypt($data, $decrypted, $publicKey); return $decrypted;
    } public function privateEncrypt($data, $privateKey) {
        openssl_private_encrypt($data, $encrypted, $privateKey); return $encrypted;
    } public function privateDecrypt($data, $privateKey) {
        openssl_private_decrypt($data, $decrypted, $privateKey); return $decrypted;
    }
}


$rsa = new Rsa(); echo "公鑰:\n", $rsa->publicKey, "\n"; echo "私鑰:\n", $rsa->privateKey, "\n"; // 使用公鑰加密 $str = $rsa->publicEncrypt('hello', $rsa->publicKey); // 這里使用base64是為了不出現(xiàn)亂碼,默認(rèn)加密出來的值有亂碼 $str = base64_encode($str); echo "公鑰加密(base64處理過):\n", $str, "\n";
$str = base64_decode($str);
$pubstr = $rsa->publicDecrypt($str, $rsa->publicKey); echo "公鑰解密:\n", $pubstr, "\n";
$privstr = $rsa->privateDecrypt($str, $rsa->privateKey); echo "私鑰解密:\n", $privstr, "\n"; // 使用私鑰加密 $str = $rsa->privateEncrypt('world', $rsa->privateKey); // 這里使用base64是為了不出現(xiàn)亂碼,默認(rèn)加密出來的值有亂碼 $str = base64_encode($str); echo "私鑰加密(base64處理過):\n", $str, "\n";
$str = base64_decode($str);
$pubstr = $rsa->publicDecrypt($str, $rsa->publicKey); echo "公鑰解密:\n", $pubstr, "\n";
$privstr = $rsa->privateDecrypt($str, $rsa->privateKey); echo "私鑰解密:\n", $privstr, "\n";

大家執(zhí)行一下可以看到公鑰加密的數(shù)據(jù),只有私鑰能解密,反之亦然,私鑰加密的數(shù)據(jù)只有公鑰能解碼。

執(zhí)行結(jié)果如下:

這是PHP端的實(shí)現(xiàn),可用在第三方跟平臺之間的數(shù)據(jù)傳輸,但如果是前端傳到后端該怎么辦呢,搜索了一下,發(fā)現(xiàn)了這個庫jsencrypt。

使用jsencrypt加密解密的例子如下:

<!DOCTYPE html> <html> <head> <title>JSEncrypt Example</title> <base id="basetag" href="/jsencrypt-2.3.0/"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="jquery.js"></script> <script type="text/javascript" src="bin/jsencrypt.js"></script> </head> <body> <div class="container"> <script type="text/javascript"> // Call this code when the page is done loading. $(function() { // Run a quick encryption/decryption when they click. $('#testme').click(function() { // Encrypt with the public key... var encrypt = new JSEncrypt();
      encrypt.setPublicKey($('#pubkey').val()); var encrypted = encrypt.encrypt($('#input').val()); console.log(encrypted) // Decrypt with the private key... var decrypt = new JSEncrypt();
      decrypt.setPrivateKey($('#privkey').val()); var uncrypted = decrypt.decrypt(encrypted); console.log(uncrypted) // Now a simple check to see if the round-trip worked. if (uncrypted == $('#input').val()) {
        alert('It works!!!');
      } else {
        alert('Something went wrong....');
      }
    });
  }); </script> <label for="privkey">Private Key</label><br/> <textarea id="privkey" rows="15" cols="65">-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDkaTKbAERvJGE0
jGsdPXUI1FpO1uDkBkuuQU4LRv0Quw9r8j+fVNkN9rZVXMV7MiGOgb80Z0k9zRxZ
5KWOqMnSlrpyO3WjhFpDJeSmqZ4wLMFwmxrr31AjabD5Nkf9dQ7RiEIuM49g27+M
3OFvdIPcLgCy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tbUDvWmL8WjYlrV5Toi0l8CgYAQ
Ujr37JGkAOzPzEQSA1FFvdpTm9G+U1T+JK6GI01DvbhPZC4nZnnAND/OTVqI4hCq
vNF4GTCV/Q+Lq3QBGG5RCh/Vf7TTBscD0PVGxoZ+RTozpaQ8rNoNP38EK7ru80gL
npK8qI+03nWxR+H3cin8l+N6X3GoOZyE+CMvb+XPwQKBgEeDjwTWxVhH/uksO3pw
MgbHjauD6AjuW9jc2a7ngFCWSQxQ3+xK1Spn6pbVdLPiBgInxCIl8d6S1yFU0Uan
iZHgy4fs1hdJRSuJ6qydqSwlS2C+gDpyY8ye0i+jq5VYYhKcpJCrCgaQGbleuaUd
ldp7v1FD8uyeemknGA35f6Id
-----END PRIVATE KEY-----</textarea><br/> <label for="pubkey">Public Key</label><br/> <textarea id="pubkey" rows="15" cols="65">-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5GkymwBEbyRhNIxrHT11
CNRaTtbg5AZLrkFOC0b9ELsPa/I/n1TZDfa2VVzFezIhjoG/NGdJPc0cWeSljqjJ
0pa6cjt1o4RaQyXkpqmeMCzBcJsa699QI2mw+TZH/XUO0YhCLjOPYNu/jNzhb3SD
3C4Asl55JfBcAXHgbN/EGwl2OTK8JWa9d8lmZ8o+n22xvLeGrQMyem549xWbr8pw
t0JzGlYumJ0yJQTlDh7arf47FEzglpMCqpGYfkWRdn8dyBZEqSQDjIE+aY0p3xCZ
CuROCSR6Gy6P3RTtCexr3vqm/5XyfVKZ2Xu/DdUwL2yYjWaDdtoIW7iAFjOaqiKj
swIDAQAB
-----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/> <input id="testme" type="button" value="Test Me!!!" /><br/> </div> </body> </html>

其中的公鑰和私鑰都是上面PHP代碼中生成的,這邊好像只有公鑰加密,私鑰解密。

拿到其中打印出的加密后的數(shù)據(jù),嘗試在PHP中進(jìn)行解密,可以看到也能解密成功。代碼如下:

<?php class Rsa { public $privateKey = '-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDkaTKbAERvJGE0
jGsdPXUI1FpO1uDkBkuuQU4LRv0Quw9r8j+fVNkN9rZVXMV7MiGOgb80Z0k9zRxZ
5KWOqMnSlrpyO3WjhFpDJeSmqZ4wLMFwmxrr31AjabD5Nkf9dQ7RiEIuM49g27+M
3OFvdIPcLgCyXnkl8FwBceBs38QbCXY5MrwlZr13yWZnyj6fbbG8t4atAzJ6bnj3
FZuvynC3QnMaVi6YnTIlBOUOHtqt/jsUTOCWkwKqkZh+RZF2fx3IFkSpJAOMgT5p
jSnfEJkK5E4JJHobLo/dFO0J7Gve+qb/lfJ9UpnZe78N1TAvbJiNZoN22ghbuIAW
M5qqIqOzAgMBAAECggEBAItUJFtqoVQOpADy+s/+UirNpjzbVJmjwXyNN3cnmW0g
PjsBrY+aqUCcUwLlMU2B+fg86w6jRokdWgL3t4m7Kkl8SkUuQgc5z/mP3tdPNkB6
vJDc/GIPeYnwidSrKOTSB/UGoiAesYJK6aCaiCV9tIWVxjUH7eyXnvf+qAChyrUW
PG/FirLyYmz1yRG99VKKE+iEIzemGSswU0DI0bwTFQ0MunLeJf0EdT20XppNwnl4
uoRgOBpMkW02vxDDWke2YIpk128KFRtPE3zF7W+Prb3ifMuQHSqDdqTgZA5+G3A9
D+lwczy95+0mIBpJ8rKQGjJ51ZT5WVMET9+Hb9+nUIECgYEA9NluRzIi9tZxwQa2
KdU8WAtWZZQZfG18mSFg8/QYrAGF2TyLAW0mEIe7nQXxPzm50HdpxTJCkUrXGm3u
hfPayx18H4oVVYRepSSfV+xe7wdogJWV6i5h/LaZsiTk1O8vF9Cwc3yUyVoMtKsg
yVcsONOzo/Kg/vwejQJb1C8dNvMCgYEA7tAewjA25vDmfiWZ0lrWKlwGQZ97+pMU
X+N12DWxL1Lv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vqm/5XyfVKZ2Xu/DdUwL2yYjWaDdtoIW7iAFjOaqiKj
swIDAQAB
-----END PUBLIC KEY-----'; public function __construct() {
    } public function publicEncrypt($data, $publicKey) {
        openssl_public_encrypt($data, $encrypted, $publicKey); return $encrypted;
    } public function publicDecrypt($data, $publicKey) {
        openssl_public_decrypt($data, $decrypted, $publicKey); return $decrypted;
    } public function privateEncrypt($data, $privateKey) {
        openssl_private_encrypt($data, $encrypted, $privateKey); return $encrypted;
    } public function privateDecrypt($data, $privateKey) {
        openssl_private_decrypt($data, $decrypted, $privateKey); return $decrypted;
    }
}

$rsa = new Rsa(); // 使用公鑰加密的內(nèi)容 $str = 'IUMBGcLwJECdxUu3LMbeEhGQdoRjCLqFwfZQBO/Odh3tClbq76Tva7yYqTVxexXLmZ3uY8DrOk/XwcVVRr6g9rBnv/zxBxUShCdN0CwkoguvI+6Oju2aUBlM4FhUp+gmasa5YfqylEp1RpsVAp67GMGlxZvp0ekfhFXkjSqAguPd7dKq5YjftP12xOyuJHAzzg7U+eHxffxnneKqXkK7QrfQD6VrLpbYmayPSjMza/RbjXF+d85UeUZUaF25PZ7Y7kD4Yo7/hY/L6peeOkI5//tpl6U4QY9VsFsjAbIpNMsZuNjE/cZ57Kc5WScPsmy0o9wsp5DUEJmu+YYmr6adoA==';
$str = base64_decode($str);
$pubstr = $rsa->publicDecrypt($str, $rsa->publicKey); echo "公鑰解密:\n", $pubstr, "\n";
$privstr = $rsa->privateDecrypt($str, $rsa->privateKey); echo "私鑰解密:\n", $privstr, "\n";

解碼結(jié)果如下:


蕪湖市聰明屋智能科技有限公司(原中江網(wǎng)絡(luò)),成立于2005年,經(jīng)過10多年定制開發(fā)經(jīng)驗(yàn),積累了大量技術(shù)儲備和定制開發(fā)經(jīng)驗(yàn),率先創(chuàng)建安徽省內(nèi)自主研發(fā)的云計(jì)算平臺,具有大數(shù)據(jù)、高并發(fā)等高強(qiáng)度計(jì)算能力,為眾多政府、學(xué)校、公安部門、中小企業(yè)解決數(shù)據(jù)計(jì)算與管理難題。2013年公司內(nèi)部專門創(chuàng)建電商服務(wù)部,為企業(yè)提供全方位電商解決方案與配套服務(wù)。多次獲得國家、省市級領(lǐng)導(dǎo)接見,被國內(nèi)近20家電視臺、報(bào)紙媒體爭相報(bào)道。至今,聰明屋智能科技服務(wù)過上市公司、大型國企、各類私企超800家,為多家公司提供各類政務(wù)系統(tǒng)、app開發(fā)定制、微信小程序開發(fā)定制、智能家居、電商系統(tǒng)、連鎖收銀等技術(shù)解決方案服務(wù)。同時(shí),聰明屋智能科技在智能硬件方面、區(qū)塊鏈應(yīng)用方面持續(xù)投入關(guān)注及創(chuàng)新。

聰明屋智能科技