Tag: 密码学

你能弄清楚为什么这个程序会触发IllegalStateException吗?

〜/ Cipher / nsdl / crypto中的所有文件都可以在这里找到用gcj编译的java文件,参见compile.sh nmint@nqmk-mint ~/Cipher/nsdl/crypto $ echo test | ./cryptTest encrypt deadbeefdeadbeefdeadbeefdeadbeef deadbeef Blowfish CBC > test null Exception in thread “main” java.lang.IllegalStateException: cipher is not for encrypting or decrypting at javax.crypto.Cipher.update(libgcj.so.81) at javax.crypto.CipherOutputStream.write(libgcj.so.81) at nsdl.crypto.BlockCrypt.encrypt(cryptTest) at nsdl.crypto.cryptTest.main(cryptTest) BlockCrypt.java: package nsdl.crypto; import java.io.*; import java.security.spec.*; import javax.crypto.*; import javax.crypto.spec.*; public class BlockCrypt […]

使用Java生成带有BouncyCastle的X509Certificate

这就是我现在生成的数字证书。 现在我能够生成一个密码保护私钥的数字证书。 public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); testKeyStore(); } public static void testKeyStore() throws Exception { try { String storeName = “d://suresh_test.cer”; java.security.KeyPairGenerator keyPairGenerator = KeyPairGenerator .getInstance(“RSA”); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPairGenerator.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); X509Certificate trustCert = createCertificate(“CN=CA”, “CN=CA”, publicKey, privateKey); java.security.cert.Certificate[] outChain = […]

加密/解密:HMAC标签在解密方法中不匹配

我正在尝试编码加密和解密消息的加密和解密方法。 在encrypt方法中,它将接受一个字符串。 它将读入公钥并用于RSA密码。 然后,将使用具有AES密钥和IV的AES密码对消息进行加密。 然后,通过使用HMAC密钥,使用密文加密的AES生成HMAC标签。 AES密钥和HMAC密钥连接在一起并由RSA Cipher加密。 该方法将返回一个JSONObject,其中包含:RSA密文,AES密文,AES IV和HMAC标记。 它们是转换为hex字符串的字节数组。 在decrypt方法中,它将接收将被解析的JSON对象。 它将读入将在RSA密码中使用的私钥。 RSA密码将用于解密连接密钥。 解密后,密钥将分为AES密钥和HMAC密钥。 然后,将在AES密文上生成新的HMAC标记。 比较加密标签和新标签。 如果它们相等,则解密AES密文并获取消息。 当我运行我的代码时,没有错误,但它没有解密,因为2个标签不匹配。 我不知道为什么。 公钥和私钥是从.pem文件转换而来的.der文件。 请帮帮我。 谢谢! import java.security.*; import java.security.spec.*; import javax.crypto.*; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.IvParameterSpec; import org.json.*; import javax.xml.bind.DatatypeConverter; public class CryptographicTools { /** * This method encrypts a message * @param […]

使用AES-128算法加密Java中的Excel文件和C#中的解密

我试图用Java加密excel文件,然后用C#解密,但解密文件显示垃圾字符。 我能够在Java中加密.txt和.docx文件,在C#中解密而没有任何问题。 请检查代码并告诉我,如果我做错了什么? 甚至让我知道是否需要添加任何东西。 Java代码加密xls文件 // file to be encrypted File file = new File(“D:\\SynchData\\output.xls”); FileInputStream inFile = new FileInputStream(file); FileOutputStream outFile = new FileOutputStream(“D:\\SynchData\\output.dec”); // password to encrypt the file String password = “MAKV2SPBNI99212”; // password, iv and salt should be transferred to the other end // in a secure manner // salt is […]

Java类中RSA加密的问题

public class MyEncrypt { public void saveToFile(String fileName, BigInteger mod, BigInteger exp) throws IOException { ObjectOutputStream oout = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fileName))); try { oout.writeObject(mod); oout.writeObject(exp); } catch (Exception e) { throw new IOException(“Unexpected error”, e); } finally { oout.close(); } } public static void main(String[] args) throws Exception { MyEncrypt myEncrypt = new MyEncrypt(); […]

使用AES / ECB / NoPadding进行加密/解密

以下是我的加密/解密方法: private String decrypt_data(String encData) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { String key = “bad8deadcafef00d”; SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), “AES”); Cipher cipher = Cipher.getInstance(“AES/ECB/NoPadding”); cipher.init(Cipher.DECRYPT_MODE, skeySpec); System.out.println(“Base64 decoded: “+Base64.decode(encData.getBytes()).length); byte[] original = cipher.doFinal(Base64.decode(encData.getBytes())); return new String(original).trim(); } private String encrypt_data(String data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { String key = “bad8deadcafef00d”; SecretKeySpec […]

Java中的AES-256-CBC

我正在尝试编写一个简单的Java程序,它将使用AES-256-CBC加密纯文本。 有课: import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AesCBC { private byte[] key; private byte[] iv; private static final String ALGORITHM=”AES”; public AesCBC(byte[] key, byte[] iv) { this.key = key; this.iv = iv; } public byte[] encrypt(byte[] plainText) throws Exception{ SecretKeySpec secretKey=new SecretKeySpec(key,ALGORITHM); IvParameterSpec ivParameterSpec=new IvParameterSpec(iv); Cipher cipher=Cipher.getInstance(“AES/CBC/PKCS5Padding”); cipher.init(Cipher.ENCRYPT_MODE,secretKey,ivParameterSpec); return cipher.doFinal(plainText); } public […]

已安装JCE Unlimited Strength但不支持AES 256

我已经为JAVA_HOME \ lib \ security安装了JCE Unlimited strength 但是,我仍然得到128 Cipher.getMaxAllowedKeyLength(“AES”) 。 我想知道我是否在错误的地方安装了JCE。 我在2个地方安装了Java。 C:\ Program Files \ Java \ jre7 C:\开发\的Java \ jdk1.6.0_21 谁能告诉我安装JCE无限力量的正确位置在哪里? 非常感谢您的帮助。 我的代码: KeyGenerator generator = KeyGenerator.getInstance(“AES”); generator.init(256); SecretKey secretKey = generator.generateKey(); byte[] raw= secretKey.getEncoded(); SecretKeySpec sskey= new SecretKeySpec(raw, “AES”); Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS5Padding”); if (mode == Cipher.ENCRYPT_MODE) { Cipher.getMaxAllowedKeyLength(“AES”)); cipher.init(Cipher.ENCRYPT_MODE, sskey); CipherInputStream […]

演示如何使用RSA公钥系统来交换实现机密性和完整性/身份validation的消息

我正在尝试演示使用RSA公钥系统来交换实现机密性和完整性/身份validation的消息。 我试图在客户端加密消息并将此信息发送到服务器端进行解密。 我遇到的问题是我的代码没有解密。 它给了我以下错误: javax.crypto.BadPaddingException: Data must start with zero at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:308) at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:255) at com.sun.crypto.provider.RSACipher.a(DashoA13*..) at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..) at javax.crypto.Cipher.doFinal(DashoA13*..) at PKServer.decryptMessage(PKServer.java:36) at PKServer.main(PKServer.java:69) 公钥客户端代码: import java.io.*; import java.net.*; import java.security.*; import javax.crypto.*; public class PKClient { public static final int kBufferSize = 8192; public static void main(String[] args) throws Exception { try { // […]

使用AES算法进行加密和解密

我正在为我的应用程序制作加密/解密模块。 我按照本教程 它没有给出任何错误,也没有显示输出。 日志文件 07-23 07:29:06.480: W/System.err(795): javax.crypto.BadPaddingException: pad block corrupted 07-23 07:29:06.629: W/System.err(795): at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:710) 07-23 07:29:06.629: W/System.err(795): at javax.crypto.Cipher.doFinal(Cipher.java:1111) 07-23 07:29:06.660: W/System.err(795): at com.example.generatesha384.AESHelper.decrypt(AESHelper.java:52) 07-23 07:29:06.690: W/System.err(795): at com.example.generatesha384.AESHelper.decrypt(AESHelper.java:25) 07-23 07:29:06.690: W/System.err(795): at com.example.generatesha384.MainActivity.onCreate(MainActivity.java:24) 07-23 07:29:06.700: W/System.err(795): at android.app.Activity.performCreate(Activity.java:5133) 07-23 07:29:06.730: W/System.err(795): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 07-23 07:29:06.730: W/System.err(795): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 07-23 07:29:06.770: W/System.err(795): at […]