Tag: 密码学

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 = […]

MessageDigest的更新方法做什么以及BASE64Encoder的用途是什么?

以下是将加密用户字符串的代码: import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import sun.misc.BASE64Encoder; import java.io.*; class Encrypter { public synchronized String encrypt(String plainText) throws Exception { MessageDigest md = null; try { md = MessageDigest.getInstance(“SHA”); }catch(Exception exc) { throw new Exception(exc.getMessage()); } try { md.update(plainText.getBytes(“UTF-8”)); }catch(Exception exc) { throw new Exception(exc.getMessage()); } byte raw[] = md.digest(); String hash = […]

AES 256位加密 – java.security.InvalidAlgorithmParameterException:错误的IV长度:必须是16个字节长

以下是我的加密逻辑。 虽然我的IV是16字节长,但我仍然会收到无效IV长度的错误。 非常感谢任何帮助 @Override public String encrypt(String dataToEncrypt, String IV) throws Exception{ if(encryptionKey.length() < 10){ encryptionKey = generateEncryptionKey().toString(); } System.out.println("number of IV bytes is "+IV.length()+" "+IV); Cipher cipher = Cipher.getInstance(encrpytionAlgo); SecretKey key = new SecretKeySpec(encryptionKey.getBytes(Charset.forName("UTF-8")), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes(Charset.forName("UTF-8")))); byte[] encryptedTextBytes = cipher.doFinal(dataToEncrypt.getBytes(Charset.forName("UTF-8"))); return new Base64().encodeAsString(encryptedTextBytes); } IV和密钥生成逻辑 @Override public String generateRandomIV(){ Random random = […]

如何解密用Laravel中的Crypt加密的Java(Android)文本?

我需要解密从服务器收到的一些数据,并且制作API的程序员将我引导到此Encrypter类,以查看他用于加密的内容。 现在基于该类,我发现使用的算法是AES128 CBC,并且我收到的字符串是Base64编码的并且包含其他数据,而不仅仅是密文。 即,如果我收到以下字符串: eyJpdiI6InJsSzRlU3pDZTBBUVNwMzdXMjVcL0tBPT0iLCJ2YWx1ZSI6Ik5JOENsSVVWaWk2RGNhNlwvWjJNeG94UzVkclwvMGJOREQreWUyS1UzclRMND0iLCJtYWMiOiJhZTZkYjNkNGM2ZTliNmU0ZTc0MTRiNDBmMzFlZTJhNTczZWIxMjk4N2YwMjlhODA1NTIyMDEzODljNDY2OTk2In0 在base64解码后,我得到: {“iv”:”rlK4eSzCe0AQSp37W25\/KA==”,”value”:”NI8ClIUVii6Dca6\/Z2MxoxS5dr\/0bNDD+ye2KU3rTL4=”,”mac”:”ae6db3d4c6e9b6e4e7414b40f31ee2a573eb12987f029a80552201389c466996″} 基于Encrypter类的line 99 ( iv = base64_decode($payload[‘iv’]); ),我对iv和value执行了另一个base64解码,得到了长度为16的iv 。我作为参数传递给了function如下: public static String decrypt(String iv, String encryptedData) throws Exception { byte[] keyValue = “zy2dEd1pKG5i3WuWbvOBolFQR84AYbvN”.getBytes(); Key key = new SecretKeySpec(keyValue, “AES”); Cipher c = Cipher.getInstance(“AES/CBC/PKCS7Padding”); c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv.getBytes())); byte[] decordedValue = Base64.decode(encryptedData.getBytes(), Base64.DEFAULT); byte[] decValue = c.doFinal(decordedValue); return new String(decValue); […]

哈希和腌制价值观

我正在开发一个内部validation用户的小型Web应用程序。 一旦用户通过身份validation,我的Web应用程序就会将一些信息(如userID和Person的名称)传递给第三方Web应用程序。 第三方开发人员建议我们对值进行散列和加盐。 原谅我的无知,但究竟是什么意思呢? 我正在用Java编写应用程序。 所以我打算做的是将userID,Person的名称和一些Math.random()值作为Apache Commons Digest Utils SHA512的哈希值并将该哈希字符串与userID和person的名称一起传递。 这是标准做法吗? 我应该通过第三方盐也是正确的?

用Android实现Bouncy Castle密码算法

我如何使用Bouncy Castle提供程序来实现诸如Serpent和Twofish之类的算法,因为Sun的提供商根本不实现这些算法。 我知道当多个提供程序可以实现相同的算法时,您将获得最高级别提供程序的实现,该提供程序将成为Sun提供程序。 如果由于某种原因您想要使用特定提供程序的实现(可能因为您知道它更快),您可以在两个arg版本的getInstance()中指定提供程序。 就我而言,Sun提供商根本没有实现我感兴趣的算法。 我试图实施Serpent: public static final String FILE_EXTENSION = “.serpent”; public static final String PROVIDER = “BC”; // Bouncy Castle public static final String BLOCK_CIPHER = “Serpent”; public static final String TRANSFORMATION = “Serpent/CBC/PKCS7Padding”; public static final String KEY_ALGORITHM = “PBKDF2WithHmacSHA1”; public static final String PRNG_ALGORITHM = “SHA1PRNG”; public static final int […]

解密用Objective-C和Java加密的AES数据

我尝试解密最初用Java中的Objective-C加密的数据。 还有其他问题提到这一点,但它们真的很混乱,其中很多都没有解决,因此我会发布我自己的。 这是加密数据的代码: – (int) encryptWithKey: (NSString *) key { // ‘key’ should be 32 bytes for AES256, will be null-padded otherwise char * keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused) bzero( keyPtr, sizeof(keyPtr) ); // fill with zeroes (for padding) // fetch key data [key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF8StringEncoding]; // encrypts in-place, since […]

解密使用OpenSSL生成的“der”文件时出现exception:使用填充密码解密时,输入长度必须是8的倍数

首先,我使用OpenSSL生成一个私有RSA密钥文件,然后将其转换为加密的“der”文件: $ openssl pkcs8 -topk8 -inform PEM -outform DER -in private_key.pem -out private_key.der 接下来我尝试使用以下代码从Java解密此文件(在此阶段我已经使用位于此post底部的代码将文件读入byte[] key数组): public static byte[] decryptPrivateKey(byte[] key) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { PBEKeySpec passKeySpec = new PBEKeySpec(“p”.toCharArray()); //my password EncryptedPrivateKeyInfo encryptedKey = new EncryptedPrivateKeyInfo(key); System.out.println(encryptedKey.getAlgName()); //PBEWithMD5AndDES System.out.println(“key length: ” + key.length); //key length: 677 SecretKeyFactory keyFac = […]

java.security.spec.InvalidKeySpecException和java程序中不适当的密钥规范错误

作为项目实施的一部分,我做了:1。Generete DSA密钥2.使用AES加密私钥3.保存到文件4.打开文件并读取加密的私钥5.我试图转换读取值成主键格式 import java.security.spec.EncodedKeySpec; import java.security.spec.PKCS8EncodedKeySpec; import java.security.Security; import java.io.File; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.Signature; import java.security.spec.PKCS8EncodedKeySpec; import java.io.*; import java.security.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; public class Pgm { public static void main(String[] args) { try { KeyPairGenerator dsa = KeyPairGenerator.getInstance(“DSA”); SecureRandom random = […]