我有以下测试,它使用System.Security.Cryptograph.RSACryptoServiceProvider隔离了我遇到的问题.问题是r.Decrypt抛出异常"密钥不存在".如果我使用privateKeyXml进行加密和解密(而不是在解密时使用publicKeyXml),那么它按预期工作.当然我不想共享私钥,我需要能够使用公钥解密.有谁看到我在这里做错了什么?
[Fact]
public void BasicEncryptDecrypt()
{
var cspParameters = new CspParameters() { Flags = CspProviderFlags.CreateEphemeralKey | CspProviderFlags.NoPrompt };
string privateKeyXml = null;
string publicKeyXml = null;
using(var r = new RSACryptoServiceProvider(2048, cspParameters)){
r.PersistKeyInCsp = false;
privateKeyXml = r.ToXmlString(true);
publicKeyXml = r.ToXmlString(false);
}
byte[] encrypted = null;
string decrypted = null;
using (var r = new RSACryptoServiceProvider(2048, cspParameters))
{
r.FromXmlString(privateKeyXml);
encrypted = r.Encrypt(Encoding.UTF8.GetBytes("foobar"), false);
}
using (var r = new RSACryptoServiceProvider(2048, cspParameters))
{
r.FromXmlString(publicKeyXml);
decrypted = Encoding.UTF8.GetString(r.Decrypt(encrypted, false));
}
Assert.Equal("foobar", decrypted); …Run Code Online (Sandbox Code Playgroud) 这是加密方法:
public static byte[] Encrypt(byte[] plaintext, byte[] key)
{
using (var aes = Aes.Create())
{
aes.BlockSize = 128;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.None;
var iv = new byte[16];
for (int i = 0; i < iv.Length; i++)
iv[i] = 0;
aes.IV = iv;
var encryptor = aes.CreateEncryptor(key, aes.IV);
using(var target = new MemoryStream())
using (var cs = new CryptoStream(target, encryptor, CryptoStreamMode.Write))
{
using (var source = new StreamWriter(cs))
source.Write(plaintext);
return target.ToArray();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我是怎么称呼的:
var key = new …Run Code Online (Sandbox Code Playgroud) 我正在Windows 8.1上使用C#.NET4.0 VS2010开发Windows桌面应用程序.我使用.NET设置机制存储了一系列设置.它们具有用户范围,因此,在应用程序中设置时,它们将写入Users\username\AppData\Local\companyname\App.exe_URL_randomstuff\versionno\user.config.
这些设置包括我需要隐藏的一些用户注册信息.我的研究表明我应该能够使用RsaProtectedConfigurationProvider加密设置,但我发现的所有示例都与加密app.config而不是user.config有关(例如http://msdn.microsoft.com/en-us /library/system.configuration.rsaprotectedconfigurationprovider.aspx).
因此,我的问题是可以加密user.config,如果是这样,怎么办?我注意到当我实例化System.Configuration.Configuration对象时,我可以将ConfigurationUserLevel设置为PerUserRoamingAndLocal.当我通过调试器检查对象时,它似乎引用了正确的user.config文件,但是当我继续实例时,一个ConfigurationSection来保护它返回null.代码如下所示:
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal);
ConfigurationSection connStrings = config.AppSettings;
connStrings.SectionInformation.ProtectSection(provider);
Run Code Online (Sandbox Code Playgroud)
我认为config.AppSettings可能不正确,但我不知道该替换它.
任何建议都非常感谢.
我ArrayList使用这两种方法在sd卡中保存并加载包含可序列化对象的文件
保存方法
public static void saveUserList(ArrayList<User> userList) {
if (storageAvailable()) {
try {
createFolder();
FileOutputStream userList = new FileOutputStream(
baseDir + File.separator + baseAppDir + File.separator
+ fileName);
ObjectOutputStream oos = new ObjectOutputStream(
userList);
oos.writeObject(userList);
oos.close();
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
加载方法
public static ArrayList<User> loadUserList() {
if (storageAvailable()) {
ArrayList<User> userList = new ArrayList<User>();
try {
FileInputStream userList = new FileInputStream(baseDir
+ File.separator + baseAppDir + File.separator
+ fileName);
ObjectInputStream oos = new …Run Code Online (Sandbox Code Playgroud) 我使用AES加密字符串,但加密的字符串包含\n和\r结尾.
public class AESImpl {
private static String decryptedString;
private static String encryptedString;
public static void main(String[] args) throws NoSuchAlgorithmException, IOException, ClassNotFoundException {
String strToEncrypt = "This text has to be encrypted";
SecretKey secretKey = generateSecretKey();
String encryptStr = encrypt(strToEncrypt, secretKey);
System.out.println("Encrypted String : " + encryptStr + "It should not come in new line");
String decryptStr = decrypt(encryptStr, secretKey);
System.out.println("Decrypted String : " + decryptStr);
}
private static SecretKey generateSecretKey() throws NoSuchAlgorithmException, IOException {
KeyGenerator …Run Code Online (Sandbox Code Playgroud) 我试图将用C#编写的代码转换为目标c,我试过但无法得到相同的结果.任何帮助都会很明显.
internal static class Common {
static string encryptionKey = "0C61L2FSL2F3E7X8E9T1L2F3E4O5";
static byte[] salt = new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76};
internal static string Encrypt(string clearText)
{
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, salt);
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText …Run Code Online (Sandbox Code Playgroud) 如何使用加密密码解密jasypt.
package com.uk.mysqlmaven.jsf.test;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.jasypt.util.text.StrongTextEncryptor;
public class PasswordEncryptionDecryptionUsingJASYPT {
public static void main(String[] args) {
try {
String password = "password";
StrongPasswordEncryptor encryptor = new StrongPasswordEncryptor();
String encryptedPassword = encryptor.encryptPassword(password);
if (encryptor.checkPassword(password, encryptedPassword)) {
//correct
System.out.println("Encrypted: "+ encryptedPassword);
} else {
//bad again
System.out.println("Error: ");
}
StrongTextEncryptor textEncryptor = new StrongTextEncryptor();
textEncryptor.setPassword(encryptedPassword);
String decryptedPassword = textEncryptor.decrypt(encryptedPassword);
System.out.println("Decrypted: "+ decryptedPassword);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用jasypt解密密码时,控制台中会显示错误.
Encrypted: JIOYXNa1+3+QefY2S7sas7LmhyOuDQcG8TTsQoTkqj0OtobCvwAFHXxoTr7z6HuP
org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception. …Run Code Online (Sandbox Code Playgroud) 我按照本教程加密和解密android/java中的简单字符串:
/sf/ask/302364751/ 我做了一个Cryptography类:
public class Cryptography {
public static SecretKey generateKey() throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA");
digest.update("BhLKTyLoP YroUsRQT".getBytes());
return new SecretKeySpec(digest.digest(), 0, 16, "AES");
}
public static byte[] encrypt(String message, SecretKey key) throws NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException {
Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
aes.init(Cipher.ENCRYPT_MODE, key);
return aes.doFinal(message.getBytes());
}
public static String decrypt(byte[] cipherText, SecretKey key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
aes.init(Cipher.DECRYPT_MODE, key);
return new String(aes.doFinal(cipherText));
}
}
Run Code Online (Sandbox Code Playgroud)
我能够加密方法并给了我这个:
Encrypted username: [B@52aff408 …Run Code Online (Sandbox Code Playgroud) 我试图建立一个SRTP以RTP数据流转换器,我有得到的问题Master Key从WebRTC peerconnection我创造.
根据我的理解,通过DES交换,密钥是通过交换进行的SDP交换,并在a=crypto现场显示.因此,这种情况看起来非常简单(如果我错了,请纠正我),但最终无用,因为WebRTC标准化现在要求不应使用DES(Chrome现在只支持它,将来可能会删除).
对于DTLS,指纹字段中SDP是否有希望在未来交换中使用的证书的哈希值?[编辑:做了一些阅读后,我认为情况并非如此]我会想到的是指纹旁边的能力,通过交换中的DTLS数据包解析我应该能够抓住Master Key解码SRTP流,但我正在撞墙,因为我不知道在哪里看,甚至100%确定它是否是可能.
因此,简而言之,就是它甚至是可行的(并不进入下C++ API和创建我自己的实现的WebRTC)的解码SRTP是与创造的原料WebRTC PeerConnection中Chrome和FireFox(从收集到的信息嗅探可能通过分组SDP交换)?[编辑:令人沮丧,似乎访问密钥(也称为主密钥)的私有部分是不可能的......请大家指正,如果我错]