有谁知道如何使用RSA公钥和私钥加密和解密字符串对象?
我使用KeyPair生成器在下面创建了私钥和公钥,但我现在想要使用公钥来加密数据,并使用私钥来解密它.
public class Keys {
private static KeyPairGenerator generator;
private static KeyPair keyPair;
private static PrivateKey mPrivateKey;
private static PublicKey mPublicKey;
private static SecureRandom secureRandom;
private static final String SHA1PRNG = "SHA1PRNG";
public static final String RSA = "RSA";
private Keys() throws NoSuchAlgorithmException {
generator = KeyPairGenerator.getInstance("RSA");
}
/**
* Generate private and public key pairs
*
* @throws NoSuchAlgorithmException
*/
private static void generateKeyPair() throws NoSuchAlgorithmException {
// create SecureRandom object used to generate key pairs
secureRandom = …Run Code Online (Sandbox Code Playgroud) 我一直在尝试加密一个项目的一些用户密码,但我似乎无法让它正常工作.我决定使用SHA-256算法,当我使用Sha2向MySQL引入密码时(例如,256)它为加密密码添加了两个零.在Java中,我使用它来散列程序中的文本,但无法获得相同的结果.
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest("ContrasenhaPassword".getBytes("UTF-8"));
StringBuilder hexString = new StringBuilder();
for (int i: hash) {
hexString.append(Integer.toHexString(0XFF & i));
}
String Hashed = new String(hexString);
System.out.println(hexString);
System.out.println(Hashed);
// Below, MySQL Output for SHA2('ContrasenhaPassword',256)
System.out.println("d17bf0da90f56b8fc627bac6523ffd284aa0d82c870e1a0428274de048f49d78");
System.out.println(Hashed.equals(hexString));
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
d17bf0da90f56b8fc627bac6523ffd284aa0d82c87e1a428274de048f49d78
d17bf0da90f56b8fc627bac6523ffd284aa0d82c87e1a428274de048f49d78
d17bf0da90f56b8fc627bac6523ffd284aa0d82c870e1a0428274de048f49d78
false
BUILD SUCCESSFUL (total time: 0 seconds)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
是否有解决以下问题的行业标准:
故事; 我有一个程序(=>游戏),它将状态(=> level,..)保存在xml文件中; 这个状态不应该是用户可编辑的; 我想阻止人们编写可修补我的xml和程序状态的补丁软件.
您想要保护您的xml文件.您可以通过加密来完成此操作.不过你怎么用钥匙?
因为有人可以随时进行逆向工程(打开你的dll)阅读密钥......?
public static string Encrypt (string toEncrypt)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("12345678901234567890123456789012");
// 256-AES key
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (toEncrypt);
RijndaelManaged rDel = new RijndaelManaged ();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
// http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
rDel.Padding = PaddingMode.PKCS7;
// better lang support
ICryptoTransform cTransform = rDel.CreateEncryptor ();
byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String (resultArray, 0, resultArray.Length);
}
public static string Decrypt (string toDecrypt)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("12345678901234567890123456789012");
// …Run Code Online (Sandbox Code Playgroud) 感谢Gaurav Mantri 在C#中回答我之前提出的Azure ACS设置问题.
但是能有人向我解释,如何以下线路保护?
if (!ClaimsPrincipal.Current.Identity.IsAuthenticated)
Run Code Online (Sandbox Code Playgroud)
ACS模式中的客户端很方便中间人,他可能无法登录到Facebook,例如,这被转发到ACS(我假设这部分是安全的),但现在ACS告诉客户回去关于失败的依赖方.
最后一部分是如何获得保障的?什么阻止客户端篡改ACS消息"失败"到"成功"?Asp.Net如何知道如何验证签名并解密消息?
就此而言,它用于加密/签署消息交换的密钥是什么?那在webfarm/Azure环境中如何工作?
当我的ASP.NET网站在Windows 7机器上运行时,它可以在另一台Windows 7机器上连接(以编程方式作为"客户端")到SSL加密服务("服务器").
但如果我的网站在生产框(Windows Server 2003)上,该服务的Windows日志显示:
从远程客户端应用程序收到TLS 1.0连接请求,但服务器不支持客户端应用程序支持的任何密码套件.SSL连接请求失败.
(服务是使用makecert.exe创建的自签名证书,但我看不出如何让makecert允许更多的密码套件......或者我在2003年的盒子上安装了什么......?这个:https:/ /serverfault.com/questions/166750对我不起作用,因为我没有使用CSR)
我目前正在使用Java中的密钥处理类,特别是使用KeyStore.我正在尝试使用AES实例生成SecretKey,然后使用setEntry()方法将其放在KeyStore中.
我已经包含了我的代码的相关部分:
// The KS Object
private KeyStore keyStore;
private KeyStore.SecretKeyEntry secretKeyEntry;
private KeyStore.ProtectionParameter protectionParameter;
private KeyGenerator keyGenerator;
private SecretKey secretKey, newSecretKey;
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256);
newSecretKey = keyGenerator.generateKey();
protectionParameter = new KeyStore.PasswordProtection(KEYSTORE_PASSWORD.toCharArray());
secretKeyEntry = new KeyStore.SecretKeyEntry(newSecretKey);
keyStore.setEntry(KEYSTORE_ALIAS, secretKeyEntry, protectionParameter);
Run Code Online (Sandbox Code Playgroud)
我使用的两个常量也定义为字符串.
我一直得到的异常是在我的setEntry()调用中:
java.security.KeyStoreException: Cannot store non-PrivateKeys
at sun.security.provider.JavaKeyStore.engineSetKeyEntry(Unknown Source)
at sun.security.provider.JavaKeyStore$JKS.engineSetKeyEntry(Unknown Source)
at java.security.KeyStoreSpi.engineSetEntry(Unknown Source)
at java.security.KeyStore.setEntry(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
我主要使用此文档http://docs.oracle.com/javase/7/docs/api/java/security/KeyStore.html作为参考,以及其他一些来源.
在此先感谢您的帮助.
任何人都可以帮助我在Mule 3.4中使用加密命名空间和模式位置.
我有这个配置XML
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
<encryption:config name="Encryption_PGP" defaultEncrypter="PGP_ENCRYPTER" doc:name="Encryption_PGP">
<encryption:pgp-encrypter-config publicKeyRingFileName="./mule.gpg" secretKeyRingFileName="./secring.gpg" secretAliasId="3879972755627455806" secretPassphrase="mule1234" principal="test1"/>
</encryption:config>
<flow name="sdsd">
<encryption:encrypt config-ref="Encryption_PGP" doc:name="Encryption" using="PGP_ENCRYPTER" input-ref="#[message.payload]">
<encryption:pgp-encrypter principal="fernando.martinez <fernando.martinez@mulesoft.com>" />
</encryption:encrypt>
</flow>
</mule>
Run Code Online (Sandbox Code Playgroud)
但是它给了我这个错误:元素"encryption:config"的前缀"encryption"没有绑定.
据我所知,在我的所有搜索和测试中,如果我设置自己的jabber服务器(使用ejabberd),我不能使用TLS与谷歌XMPP服务器进行s2s.
当我连接时,ejabberd日志记录:
I(<0.377.0>:ejabberd_s2s_out:365) : Connection established: XXXX.net -> gmail.com with TLS=false
Run Code Online (Sandbox Code Playgroud)
甚至可以使用谷歌XMPP服务器进行S2S TLS吗?
我不是加密技术,破解密码和安全性方面的专家.我需要在首选项中存储我将用于将客户端连接到SQL服务器的SQL服务器的密码和用户名.
我一直在读书,大部分时间都在这里,但大多数都超过2年,我们都知道安全世界正在快速变化.
根据我的阅读,我得出结论,大多数人建议使用SHA-XXX散列密码和/或建议使用jasypt加密和解密密码
所以我的问题是在首选项文件中保护密码的最安全方法是什么?我可以只使用带盐的SHA-512并将盐保存在首选项文件中吗?这样安全吗?
我对哈希算法知之甚少.
我需要在将文件转发到远程系统(有点像S3)之前用Java计算传入文件的哈希,这需要MD2/MD5/SHA-X中的文件哈希.出于安全原因,不会计算此哈希,而只是为了一致性校验和.
我可以使用Java标准库的DigestInputStream在转发文件时实时计算此哈希值,但是想知道最好使用哪种算法来避免使用DigestInputStream的性能问题?
我的一位前同事测试并告诉我们,与unix命令行或文件相比,计算hash实时可能非常昂贵.
关于过早优化的编辑: 我在一家公司工作,目的是帮助其他公司取消他们的文件.这意味着我们有一个处理来自其他公司的文件传输的批次.我们将来每天定位数百万份文档,实际上,此批次的执行时间对我们的业务非常敏感.
每天100万份文档的散列优化10毫秒是每天执行时间缩短3小时,这是非常巨大的.
encryption ×10
java ×5
asp.net ×2
security ×2
ssl ×2
acs ×1
aes ×1
azure ×1
c# ×1
comparison ×1
cryptography ×1
ejabberd ×1
google-talk ×1
hash ×1
key-pair ×1
keystore ×1
mule ×1
mysql ×1
sha256 ×1
xmpp ×1