我知道以前曾以各种形式提出过这个问题.但是,我不是在寻找"使用https"的答案.我已经在使用HTTPS了,我并不担心有效载荷来回传输的敏感性.
但是,我正在处理的iPhone应用程序正在与我构建的REST API进行通信(我已经控制了应用程序和服务器 - 因此欢迎任何建议).
我使用OAuth2协议进行身份验证,这意味着我的"API密钥"是客户端ID和客户端密钥的组合,只需要传输即可获取access_token.之后,使用access_token包含请求主体的HMAC的头部(使用客户端秘密作为密钥)将所有请求发送到服务器.这种添加的唯一原因是有人无法使用JUST进行API请求access_token.
我正在谈论的API将在我发布应用程序时公开.所以我不一定担心别人能够对它进行API调用.
我关心的是:
我的猜测是,这个问题并没有真正的解决方案(除了使用UIWebView并制作一个美化的webapp),但我想我还是会在这里问一下.
如果应用程序需要使用客户端ID /客户端密码,您能想到保护客户端ID /客户端密钥的方法吗?
我有三件事:文件,签名文件和X509证书文件.cer.必须使用证书中的公钥和签名文件验证该文件.我想用Security.h/CommonCrypto来做.
到目前为止我尝试了什么:
// load all the files
NSData* fileData = [NSData dataWithContentsOfFile:(...)];
NSData* signatureData = [NSData dataWithContentsOfFile:(...)];
NSData* certificateData = [NSData dataWithContentsOfFile:(...)];
SecCertificateRef certificate = SecCertificateCreateWithData(NULL, CFBridgingRetain(certificateData)); // load the certificate
Run Code Online (Sandbox Code Playgroud)
证书加载就好了.它的名字可以使用
CFStringRef certificateDescription = SecCertificateCopySubjectSummary(certificate);
Run Code Online (Sandbox Code Playgroud)
哪个有效.由于iOS上似乎没有直接复制公钥的方法,我首先创建一个信任.
SecTrustRef trust;
OSStatus statusTrust = SecTrustCreateWithCertificates( certificate, secPolicy, &trust);
SecTrustResultType resultType;
OSStatus statusTrustEval = SecTrustEvaluate(trust, &resultType);
Run Code Online (Sandbox Code Playgroud)
这一切都可以正常使用errSecSuccess结果.
现在我尝试获取公钥.
SecKeyRef publicKey;
publicKey = SecTrustCopyPublicKey(trust);
size_t keysize = SecKeyGetBlockSize(publicKey);
Run Code Online (Sandbox Code Playgroud)
但是publicKey的内容
NSData* keyData = [NSData dataWithBytes:publicKey length:keysize];
Run Code Online (Sandbox Code Playgroud)
与打开.cer文件时看到的公钥不同.所以这是第一个问题.
然后我尝试验证签名,即使我知道公钥是错误的.填充是正确的.
OSStatus verficationResult = SecKeyRawVerify(publicKey, kSecPaddingPKCS1, [fileData bytes], …Run Code Online (Sandbox Code Playgroud) 我试图取出一个字符串中的所有标点符号和空格,我将使用Playfair Cipher加密.我无法弄清楚为什么这条线不起作用.
s = Regex.Replace(s, @"[^\w\s]", string.Empty);
Run Code Online (Sandbox Code Playgroud) 我们假设我使用此站点上的算法使用公钥 - 私钥加密和解密数据:
CodeProject上C#.NET中的公钥RSA加密
现在,让我们说有人使用我的公钥使用另一种算法加密他的数据并将其发送给我.使用不同的算法(如网站上的算法),我能够使用我的私钥解密信息吗?或者这是不可能的,因为算法不同?
我的观点是,假设使用的密钥是正确的,如果使用不同的加密算法,最终结果是否总是相同的?
是否有一些标准的方法来加密信息,使其可以在不同的机器上解密,也许是不同的编程语言?
将json数组编码为可在URL查询字符串中使用的字母数字字符串的最佳方法是什么?
我需要一些简单但不容易破解的东西.我已经阅读了所有加密和解密文档.
我需要加密一个json数组例如:{"firstName":"John","lastName":"Doe"}类似的东西a14iw58swd33s541dg2k58kv3s4gvkjsdf33s9f3,所以它可以在url查询字符串中使用http://www.example.com/?v=a14iw58swd33s541dg2k58kv3s4gvkjsdf33s9f3.
我稍后会解密这个服务器端.由于它是URL的一部分,我不能拥有类似的东西ȃZ Vì§n‹ØfjÒ šçæ¹ä¯
什么是一个简单而安全的方法呢?
是否有用于VB.NET的AES加密的标准库?我想用静态私钥加密字符串.
我用Google搜索并发现了很多变化.我真的不知道如何确定哪些算法是安全的.
如何使用PHP识别sha256哈希?另外,有没有办法确定字符串是否是sha256哈希,即使它是盐渍的?
我不需要知道哈希的真正价值(我知道这是不可能的),但我只需要验证字符串,所以如果字符串是sha256哈希,我可以以某种方式工作,如果它是另一种方式不是.
由于我对Java的不完全了解将这个加密代码转换为Python代码,我正在努力.两者应该具有完全相同的结果.非常感谢帮助.
Java函数
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String s = "testings";
Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
Key key = new SecretKeySpec("6#26FRL$ZWD".getBytes(), "Blowfish");
cipher.init(1, key);
byte[] enc_bytes = cipher.doFinal(s.getBytes());
System.out.println(enc_bytes);
}
}
Run Code Online (Sandbox Code Playgroud)
Python等价
def PKCS5Padding(string):
byteNum = len(string)
packingLength = 8 - byteNum % 8
if packingLength == 8:
return string
else:
appendage = chr(packingLength) * packingLength
return string + appendage
def PandoraEncrypt(string):
from Crypto.Cipher import Blowfish
key = …Run Code Online (Sandbox Code Playgroud) 我有以下代码,大约5%的时间抛出CryptographicException,我无法弄清楚为什么a)它不会一直失败b)为什么它完全失败:
// Initialize the new secure keys
var keyGenerator = KeyGenerator.Create();
var keyPair = keyGenerator.GenerateKeyPair();
this._privateKey = keyPair.ToEncryptedPrivateKeyString(privateKeySecret);
this._publicKey = keyPair.ToPublicKeyString();
// Initialize the certificate generation
var certificateGenerator = new X509V3CertificateGenerator();
var serialNo = BigInteger.ProbablePrime(128, new Random());
certificateGenerator.SetSerialNumber(serialNo);
certificateGenerator.SetSubjectDN(GetLicenseeDN());
certificateGenerator.SetIssuerDN(GetLicencerDN());
certificateGenerator.SetNotAfter(DateTime.Now.AddYears(100));
certificateGenerator.SetNotBefore(DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0)));
certificateGenerator.SetSignatureAlgorithm("SHA512withRSA");
certificateGenerator.SetPublicKey(keyPair.PublicKey);
var result = certificateGenerator.Generate(keyPair.PrivateKey);
this._clientCertificate = new X509Certificate2(DotNetUtilities.ToX509Certificate(result));
this._clientCertificate.PrivateKey = DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)keyPair.PrivateKey);
Run Code Online (Sandbox Code Playgroud)
堆栈看起来像:
System.Security.Cryptography.CryptographicException: Bad Data.
Result StackTrace:
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils._ImportKey(SafeProvHandle hCSP, Int32 keyNumber, CspProviderFlags flags, Object cspObject, SafeKeyHandle& hKey)
at …Run Code Online (Sandbox Code Playgroud) 我正在使用SagePay Forms,目前正在将他们的VB示例转换为c#.我取得了很好的进展,所以我的项目的加密部分工作正常(SagePay可以解密).
我遇到的问题是,当我尝试解密字符串时,它会变成垃圾.如果有人这样做之前我真的很感激我的解密代码的一些帮助.我已经包含了有效的加密代码,前两行是来自另一种方法的设置和调用.
我没有添加VB代码,但如果需要,我可以添加它.如果不需要,不想要一个巨大的帖子.
实用方法:
public string byteArrayToHexString(byte[] ba)
{
return BitConverter.ToString(ba).Replace("-", "");
}
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
Run Code Online (Sandbox Code Playgroud)
主加密方法,前几行是从较大的方法中提取的调用.
string crypt = "blahblahblah"
string EncryptAndEncode = "@" + byteArrayToHexString(aesEncrypt(crypt));
private byte[] aesEncrypt(string inputText)
{
RijndaelManaged AES = new RijndaelManaged();
//set the mode, padding and block size for the key
AES.Padding = PaddingMode.PKCS7;
AES.Mode = CipherMode.CBC;
AES.KeySize = 128;
AES.BlockSize = 128; …Run Code Online (Sandbox Code Playgroud)