简体   繁体   English

使用JavaScript和PHP对信用卡进行编码

[英]Encode credit card using JavaScript and PHP

I don't have a SSL for my website, and I need to pass the credit card number from one page to another page. 我的网站没有SSL,我需要将信用卡号从一页传递到另一页。 The scenario is like this. 情况就是这样。

  1. I will check the credit card number (is that a valid number) 我将检查信用卡号(是一个有效的数字)
  2. Then i will post the value to another one page response.php 然后,我将值发布到另一个页面response.php
  3. The post value should be encrypted from index.php and I should be able to decrypt it in the next page (response.php) 帖子值应该从index.php中加密,我应该能够在下一页(response.php)中对其进行解密

     <script type="text/javascript"> function validate(payment_form){ var error=""; if(trim(payment_form.credit_card_number.value).length==0){ document.getElementById("credit_card_number_error").innerHTML="Enter Credit Card Number"; error+= false; }else{ if(IsNumeric(payment_form.credit_card_number.value)==false){ document.getElementById("credit_card_number_error").innerHTML="Enter numeric values"; error+= false; } else{ document.getElementById("credit_card_number_error").innerHTML=""; error+= ""; } } if(error==""){ return true; } else{ return false; } } function IsNumeric(strString) { var strValidChars = "0123456789.-"; var strChar; var blnResult = true; if (strString.length == 0) return false; for (i = 0; i < strString.length && blnResult == true; i++) { strChar = strString.charAt(i); if (strValidChars.indexOf(strChar) == -1) { blnResult = false; } } return blnResult; } </script> <form id='payment-form' name="payment_form" onSubmit="return validate(this)" action='response.php' method='POST'> <table> <tr><td>Credit Card Number</td><td><input type="text" placeholder="Credit Card Number" value="" id="credit_card_number" name="credit_card_number"></td><td><label class="error" id="credit_card_number_error"/></td> <tr><td></td><td><input type="submit" value="PAYMENT" class="login" id="payment" name="payment"></td></tr> </table> </form> 

To summarize all, I am using a form to post the credit card number and it needs to be encoded and I would be able to decode it in the next page. 总而言之,我正在使用一张表格来发布信用卡号,它需要进行编码,然后我才能在下一页中对其进行解码。 Is this possible. 这可能吗。

It is impossible to do what you want securely without SSL and "real" certificates, ie, signed by trusted certification authorities. 没有SSL和“真实”证书(即由受信任的证书颁发机构签名),就无法安全地做您想做的事情。

  • If you use symmetric encryption , the client must know the secret key, therefore the attacker can easily capture the key by simple eavesdropping. 如果使用对称加密 ,则客户端必须知道秘密密钥,因此攻击者可以通过简单的窃听轻松地捕获密钥。 With some more eavesdropping, he can capture the encrypted credit card number and decrypt it right away. 通过更多的窃听,他可以捕获加密的信用卡号并立即对其进行解密。
  • if you use asymmetric encryption , the attacker might be able to perform a man in the middle attack that you cannot even detect (ie, the attacker replaces the public key with one of his own, you encrypt the data to him, he decrypts it, reads the data, re-encrypts it with the real public key sent by the server; with SSL and "real" certificates, the browser would be able to tell the difference between the real public key and the attacker's one)! 如果您使用非对称加密 ,则攻击者可能会甚至无法检测到的中间攻击中执行一次操作(即,攻击者将自己的公钥替换为自己的公钥,然后将数据加密给他,然后解密,读取数据,然后使用服务器发送的真实公共密钥对其进行重新加密;使用SSL和“真实”证书,浏览器将能够分辨出真实公共密钥与攻击者的真实密钥之间的区别!

Please, please, please: don't try to be creative on security/cryptography unless you are a guru (in which case you would most probably be developing algorithms, and not making websites). 拜托,拜托,拜托:除非您是专家,否则请不要尝试在安全性/密码学上有所创新(在这种情况下,您很可能正在开发算法,而不是在创建网站)。 Odds are you will build something badly broken. 奇怪的是,您将构建一些严重损坏的东西。

The reason why SSL with "real" certificates makes this secure is because those certificates are signed by certification authorities , which will make your browser recognize them as valid. 带有“真实”证书的SSL可以确保此安全的原因是,这些证书是由证书颁发机构 签名的,这将使您的浏览器将其识别为有效证书 If they are not signed by a certification authority recognized by the browser, it will display a horrible screen telling the user that he is probably being attacked. 如果未由浏览器认可的证书颁发机构签名,它们将显示一个可怕的屏幕,告诉用户他可能正在受到攻击。 It is not feasible for an attacker to generate a valid signature for a certificate for your domain. 攻击者无法为您的域的证书生成有效的签名。

Go buy a certificate. 去买证书。 I've recently transferred my domain to a new registrar and they offered me an SSL certificate for $1.99 for 1 year (it is not wonderful -- the CA is not recognized by my Android Chrome; but well... $1.99...) 我最近将我的域名转移到了新的注册商,他们为我提供了为期1年的SSL证书,价格为$ 1.99(真是太好了-我的Android Chrome无法识别CA;但是... $ 1.99 ...)

Use local storage to save the CC number so that it doesn't need to be sent to the server. 使用本地存储来保存抄送号码,以便不需要将其发送到服务器。

localStorage.ccnum = num;

var num = localStorage.ccnum;

In case you are using SSL it will alweays be good. 如果您使用的是SSL,那肯定会很好。 But consider that you want to make and AJAX call to your own PHP/JSP page then you can do a trick. 但是考虑到您想对自己的PHP / JSP页面进行AJAX调用,那么您可以做个技巧。

Trick: 特技:

Option#1: Replace all numeric characters using simple String.replaceAll method with alphabets in your algorithem. 选项1:使用简单的String.replaceAll方法将所有数字字符替换为您的算法中的字母。 For example you replace all '1' by 'a', all '2' with 'b'. 例如,您将所有“ 1”替换为“ a”,将所有“ 2”替换为“ b”。 Or you can do it other way round. 或者,您也可以选择其他方式。 ie '1' replaced by 'z' , '2' replaced by 'y', etc. 即用“ z”代替“ 1”,用“ y”代替“ 2”,依此类推。

Option#2: You can use a random character generator to get unique 10 characters. 选项2:您可以使用随机字符生成器来获取唯一的10个字符。 Each for a separate number. 每个都有一个单独的数字。 Then in your ajax call you need to pass on that random generated string of characters (which length was 10). 然后在您的ajax调用中,您需要传递随机生成的字符串(长度为10)。 Then your PHP or JSP page will use the same algorithm to decrypt the creditcard number. 然后,您的PHP或JSP页面将使用相同的算法来解密信用卡号。

The method 2 is more secure as it will always be random. 方法2更安全,因为它始终是随机的。 This can be made more secure in a way that you make an ajax call first to ask your PHP/JSP page to generate the 10 alphabets (each for a number) and store that somewhere in DB or in session. 可以通过首先调用ajax来要求PHP / JSP页面生成10个字母(每个字母代表一个数字)并将其存储在DB或会话中的方式来提高安全性。 Then it returns the key or index to that stored 10 alphabets and in your AJAX call you will not be passing the actual encryption algorithem string. 然后,它将密钥或索引返回到存储的10个字母,并且在AJAX调用中,您将不会传递实际的加密算法字符串。 So no one will be able to understand what is happening. 因此,没有人能够了解正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM