hoc*_*bro 9 encryption rsa node.js ios
我正在尝试加密iOS端的东西并在我的node.js服务器上解密它.在服务器上,我正在使用库伪造.我能够加密某些东西并在node.js上解密它,这很有效.我像这样加密:const encryptedPassword = publicKey.encrypt(password, 'RAW');并解密如下:const password = privateKey.decrypt(encryptedPassword, 'RAW');.
现在,我想在我的iOS应用程序上加密,而不是在服务器中加密,但仍然使用相同的方式解密.我找到了这个库,swift-rsautils.https://github.com/btnguyen2k/swift-rsautils/blob/master/Swift-RSAUtils/RSAUtils.swift它调用了这个函数encryptWithRSAKey,这就是我正在使用的函数.由于它是原始加密,我试图传入填充SecPaddingNone.但是,不幸的是它不起作用,我无法在服务器上解密.错误消息的长度无效,并且base64数据的长度确实看起来要大得多.有谁知道如何解决这个问题?
这是我的iOS代码:
let dataString = text.dataUsingEncoding(NSUTF8StringEncoding)
let certificateLabel = "certificate"
let certificateRef = self.getCertificateFromKeyChain(certificateLabel)
let certificateData = self.getDataFromCertificate(certificateRef)
let cryptoImportExportManager = CryptoExportImportManager()
let publicKeyRef = cryptoImportExportManager.importPublicKeyReferenceFromDERCertificate(certificateData)
let encryptedData = self.encryptWithRSAKey(data, rsaKeyRef: publicKeyRef!, padding: SecPadding.None)
let base64EncryptedString = encryptedData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
Run Code Online (Sandbox Code Playgroud)
然后我将此base64加密字符串发送到服务器并尝试使用私钥解密.不幸的是,它不起作用.
这不是你确切问题的答案,因为我没有使用过那个特定的库,但我在 javascript 和 node.js 中玩过一些加密。
我能够实现 eccjs 库,它是使用非对称椭圆曲线支持构建的斯坦福 Javascript 加密库 (SJCL)。
在 Node.js 端:
var ecc = require('eccjs');
var cryptoKeys = ecc.generate(ecc.ENC_DEC); //crypto_keys.enc is the pubic key for encoding. crypto_keys.dec is the private key for decoding.
//send the public key to the client
app.get('/PublicKey', function(req, res){
res.setHeader('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.setHeader('Expires', '-1');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Content-type', 'text/plain');
res.send('var publicKey = ' + JSON.stringify(cryptoKeys.enc) + ';');
});
//authenticate a user name and a password (encrypted by client) against the domain controller
app.get('/Authenticate', function(req, res){
res.setHeader('Content-type', 'text/plain');
var url = "ldap://na-us-dc01.am.corp.airliquide.com";
var userPrincipalName = req.query.username + "@US-AIRLIQUIDE";
try
{
var cipherMessage = JSON.parse(req.query.encryptedPassword);
var password = ecc.decrypt(cryptoKeys.dec, cipherMessage);
//... Authentication goes here ...
}
catch(err)
{
console.log("Error with authentication: ",err);
res.send("Error with authentication: " + JSON.stringify(err,null,' '));
}
});
Run Code Online (Sandbox Code Playgroud)
在客户端中:
<script src="ecc.js"></script>
<script src="../PublicKey"></script> <!-- This returns the variable publicKey which has been set equal to the server's public key -->
<script>
function login() {
var plainTextPassword = document.getElementById('password').value;
var cipherTextPassword = ecc.encrypt(publicKey, plainTextPassword);
var username = document.getElementById('name').value;
console.log(ecc, publicKey, cipherTextPassword);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = (function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById('result').innerHTML = xhttp.responseText;
console.log("Response: " + xhttp.responseText);
}
}).bind(this);
xhttp.open("GET", "../Authenticate?username=" + username + "&encryptedPassword=" + JSON.stringify(cipherTextPassword), true);
xhttp.send();
}
</script>
Run Code Online (Sandbox Code Playgroud)
我确信这个解决方案并不完全安全,我最终没有使用它,而是实施了 HTTPS。然而,如果这是您的最终目标,这应该为您提供必要的部分来进行您自己的非对称加密。
| 归档时间: |
|
| 查看次数: |
642 次 |
| 最近记录: |