Tag: 加密

安全/加密log4j文件

我有个问题 ; 安全要求我有java swing应用程序,它具有log4j生成的日志文件,用于跟踪错误时的支持问题。 我必须ecrypt / cypher / secure文件,以便客户端无法打开它们并查看它们(至少不是人类可读的方式),同时当支持技术团队获取这些文件时,他们将知道如何阅读( 解密 )它们。 我做了很多搜索,我尝试了我发现的最佳选项,即通过扩展SkeletonAppender构建自定义appender。 现在知道我有log4j工作得很好,如下面的配置,但我创建了新的类来加密它但我不能让它工作,即使简单的设置它不会创建文件,所以我可以继续在ecnryption部分。 任何帮助,链接都很好。 工作…版本 不工作……版本 简单的class级考试 http://www.javaworld.com/article/2075817/core-java/customized-appender-extending-org-apache-log4j-fileappender.html package com.MyAppender; import org.apache.log4j.spi.LoggingEvent; public class MyAppender extends org.apache.log4j.RollingFileAppender { private String file; private boolean initialized = false; private String baseFileName = null; // private static final Log log = LogFactory.getLog(MyAppender.class); /** * * write to ActivityLog […]

从Java中的已知参数创建RSA密钥

我正在努力实施Bing Cashback。 为了validation来自Bing的传入请求是有效的,他们提供签名。 签名是使用RSA加密的URL的160位SHA-1哈希。 Microsoft提供了RSA“公钥”,模数和指数,我应该用它来解密哈希。 有没有办法创建解密哈希所需的Java密钥对象,正如微软所说的那样? 我能找到的所有东西都会自动创建RSA密钥对,因为这就是RSA应该如何工作的。 如果可能的话,我真的很想使用Java对象,因为这显然比手动编码解决方案更可靠。 他们提供的示例代码是在.NET中,并使用.NET库函数来validation哈希。 特别是RSACryptoServiceProvider.VerifyHash()

RSA加密 – 解密:BadPaddingException:数据必须从零开始

我很遗憾地问你关于这么多问题的技巧。 我有一个关于RSA crypthography的问题。 我已经检查了有关此问题的其他主题,但我没有找到任何有用的答案。 我希望你能帮助我。 我想读取一个文件,将其包含,然后解密并将这些解密的字节放在一个新文件中。 我实际上可以: – 获取文件的字节 – 加密它 我有一个例外:javax.crypto.BadPaddingException:数据必须从零开始。 这是我的代码: package com.bodom.ghosty; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.io.*; import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.security.*; import java.security.spec.InvalidKeySpecException; import java.security.spec.RSAPrivateKeySpec; import java.security.spec.RSAPublicKeySpec; import java.util.Scanner; public class EncryptionUtil { private final PrivateKey privateKey; […]

Java AES密文文本大小

我正在使用一种非常标准的Java AES加密/解密方式。 byte[] key = hexStringToByteArray(“C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF”); byte[] message = hexStringToByteArray(“01A0A1A2A3A4A5A6A703020100060001”); SecretKeySpec secretKeySpec = new SecretKeySpec(key, “AES”); // Instantiate the cipher Cipher cipher = Cipher.getInstance(“AES”); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encrypted = cipher.doFinal(message); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] original = cipher.doFinal(encrypted); 如您所见,我使用的是128位密钥和128位消息。 我总是得到我期望的结果,但加密结果总是256位长。 第二个128位始终相同。 截断结果除外,如何确保密码只返回前128位,而不更改前128位? 我觉得我有点混淆块大小的定义。

Java中的RSA BadPaddingException – 在JRE中解密Android中的解密

我发现了一些其他的问题,但我没有找到解释是什么导致这个问题。 我在Java中编写了一个函数来加密数据,将其存储到一个文件中并解密它再次加载文件。 由于它是非对称加密,因此我有一个公钥和私钥,它直接存储在代码中以便进行测试。 问题似乎是加密本身的差异,因为该function适用​​于Android以及安装了Java RE 7的Windows机器上。 但是不可能使用相同的密钥和function来加密来自JRE7系统上的Android的数据,结果是“糟糕的填充执行”。 出于测试目的,我只使用一个数据块。 由于en / decrypt在每个系统中都有效,我确信问题不是关键。 最终版本将对二进制数据进行编码和解码 – 所以我只使用字节而不必担心字符串中的不同字符集。 在我理解了原因之后,我将使用pureRSA解码PHP中的数据。 import java.io.DataInputStream; import java.io.FileInputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; import android.util.Base64; import java.math.BigInteger; import java.security.InvalidAlgorithmParameterException; import javax.crypto.Cipher; import java.security.KeyFactory; import java.security.spec.RSAPublicKeySpec; import java.security.spec.PKCS8EncodedKeySpec; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.spec.InvalidKeySpecException; import javax.crypto.NoSuchPaddingException; import java.security.InvalidKeyException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.BadPaddingException; […]

InvalidKeySpecException:algid解析错误,而不是序列

我正在编写一个从控制台输入的程序 – 一个zip文件的名称,一个zip文件的名称包含从第一个zip生成的(de / en)加密文件和一个包含公钥的文件。 解密时我得到exception: Exception in thread “main” java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217) at java.security.KeyFactory.generatePrivate(KeyFactory.java:372) at com.Main.makePrivateKey(Main.java:148) at com.Main.decrypt(Main.java:40) at com.Main.main(Main.java:182) Caused by: java.security.InvalidKeyException: IOException : algid parse error, not a sequence at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:351) at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356) at sun.security.rsa.RSAPrivateCrtKeyImpl.(RSAPrivateCrtKeyImpl.java:91) at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75) at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316) at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213) … 4 […]

Python AES解密

我在Java中有以下代码片段,我希望在Python中进行复制。 public class AESDecryption { protected SecretKeySpec getPublicKey() { try { byte[] key = “MuidKeibimbtjph9”.getBytes(“UTF-8”); key = MessageDigest.getInstance(“SHA-256”).digest(key); key = Arrays.copyOf(key, 32); return new SecretKeySpec(key, “AES”); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } public String decrypt(byte[] data) { Cipher cipher = null; try { cipher = […]

Java和PHP之间的加密/解密字符串

我正在使用AES加密来加密和解密服务器端的php和Android应用程序(作为客户端)之间的字符串。 PHP中的加密字符串是: HaxRKnMxT24kCJWUXaVvqDHahzurJQK+sYA4lIHql/U= 在Java中它是: HaxRKnMxT24kCJWUXaVvqD/KMEkJTPTXEcCsHIYGX9TGtCNOHQcJyUURPk8qlgf3 我在PHP脚本中使用phpseclib来进行加密。 我在这里想念的是什么? 这里有相关的Java代码 SecretKeySpec skeySpec = new SecretKeySpec(pad16(pass), “AES”); Cipher c = Cipher.getInstance(“AES”); c.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] out = c.doFinal( input ) 这里的PHP代码: $aes = new Crypt_AES(); $aes->setKey(‘password’); $encrypted_encoded_text = base64_encode($aes->encrypt($plaintext));

使用Blowfish在Java中解密

你好, 我用Blowfish用Java加密和解密。 加密工作正常,但解密失败。 这是我的解密Java代码: String encryptedString = … ; String decryptedString = null; SecretKeySpec key = new SecretKeySpec(myKey.getBytes(), “Blowfish”); Cipher cipher; try { cipher = Cipher.getInstance(“Blowfish”); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decrypted = cipher.doFinal(encryptedString.getBytes()); decryptedString = new String(decrypted, Charset.forName(“UTF-8”)); } [ catch Exceptions … ] 我得到一个例外: Exception. javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting […]

在sharedPreferences中存储公钥,并检索它

我正在尝试使用RSA加密数据。 到目前为止一切都很好。 我可以生成Private-Public密钥,我可以成功加密和解密字符串。 现在我想在SharedPreference中存储公钥。 我可以把它存储为字符串。 我可以将其检索为字符串。 我需要将其转换为Key,以传递给密码。 不会发生从字符串到原始格式的转换。 这是我试过的 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(“RSA”); //generate key using RSA KeyPair keypair=keyPairGenerator.generateKeyPair(); //get generated key Cipher cipher =Cipher.getInstance(“RSA/ECB/PKCS1Padding”); SharedPreferences sharedPreferences=context.getSharedPreferences(“rsakey”, MODE_PRIVATE);//Initializing SharedPerference SharedPreferences.Editor editor=sharedPreferences.edit(); editor.putString(“public”,keypair.getPublic().toString()); editor.putString(“private”,keypair.getPrivate().toString()); editor.commit();//store key in sharedpreference final String sampletext=”abcde”; //getting stored key String publicKey = sharedPreferences.getString(“public”, null); String privateKey = sharedPreferences.getString(“private”, null); //publicKey must […]