Tag: des

用triple des加密完整对象

我需要加密一个完整的java对象。 我有一个我在互联网上看到的代码,它显示了如何加密和解密文本而不是java对象。 所以我很困惑这是否可以加密完整的java对象。 我正在使用的代码如下。 package security; import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * This class defines methods for encrypting and decrypting using the Triple DES * algorithm and for generating, reading and writing Triple DES keys. It also * defines a main() method that allows these […]

Java Triple DES加密,带有2个不同的密钥

我正在尝试使用具有两个不同密钥的三重DES来加密数据,因此给定两个密钥k1和k2,密码文本将是Ek1(Dk2(Ek1(明文)))其中E是加密和D解密。 我正在尝试使用Java中的DES算法来模拟这个。 这是代码: public static void main(String[] args) { SecretKey k1 = generateDESkey(); SecretKey k2 = generateDESkey(); String firstEncryption = desEncryption(“plaintext”, k1); String decryption = desDecryption(firstEncryption, k2); String secondEncryption = desEncryption(decryption, k1); } public static SecretKey generateDESkey() { KeyGenerator keyGen = null; try { keyGen = KeyGenerator.getInstance(“DES”); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(Test.class .getName()).log(Level.SEVERE, null, […]

perl CBC DES相当于java

我们正在将一些代码从perl迁移到java / scala,我们遇到了障碍。 我们试图弄清楚如何在Java / scala中执行此操作: use Crypt::CBC; $aesKey = “some key” $cipher = new Crypt::CBC($aesKey, “DES”); $encrypted = $cipher->encrypt(“hello world”); print $encrypted // prints: Salted__ ,% 8XL /1 & n; 쀍c $decrypted = $cipher->decrypt($encrypted); print $decrypted // prints: hello world 我在scala中尝试了一些东西但是并没有真正做到正确,例如: val secretKey = new SecretKeySpec(“some key”.getBytes(“UTF-8”), “DES”) val encipher = Cipher.getInstance(“DES/CBC/PKCS5Padding”); encipher.init(Cipher.ENCRYPT_MODE, secretKey) […]

如何在C中使用crypt库来进行DES加密? (setkey,encrypt,crypt等)

我需要在C中做一些简单的DES加密来与一些旧代码进行交互。 根据我的理解,你可以使用“crypt”库,使用函数setkey,encrypt,crypt等等。我一直在搞乱它并且无法正确使用它。 缺少setkey / encrypt手册页上的示例。 我想得到与我能用一些java代码得到的相同的输出(见下文)。 所以假设我在C中有两个字符数组。 char *message = “hellothe”; char *key = “iamakey0”; 有人可以举例说明如何使用setkey / encrypt加密这些并获得与java代码相同的结果吗? 我意识到你必须将消息和密钥放入一个64字节的数组中,其中每个字符代表一点,但其中一些也令人困惑。 显然你必须得到正确的位平价或其他东西吗? public static byte[] encryptDES(byte[] message, byte[] key) { byte[] encrypted = new byte[0]; try{ Cipher c = Cipher.getInstance(“DES”); c.init(Cipher.ENCRYPT_MODE,new SecretKeySpec(key,”DES”)); encrypted = c.doFinal(message); } catch (Exception e) { e.printStackTrace(); } return encrypted; }

在Python 2.7中复制Java的PBEWithMD5AndDES

如果不是立即显而易见,那么首先我要说的不是加密人。 我的任务是在Python 2.7中复制Java的PBEWithMD5AndDES(使用DES加密的MD5摘要)的行为。 我可以访问Python的加密工具包PyCrypto。 这是我试图复制其行为的Java代码: import java.security.spec.KeySpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.spec.PBEParameterSpec; import javax.crypto.Cipher; import javax.xml.bind.DatatypeConverter; public class EncryptInJava { public static void main(String[] args) { String encryptionPassword = “q1w2e3r4t5y6”; byte[] salt = { -128, 64, -32, 16, -8, 4, -2, 1 }; int iterations = 50; try { KeySpec […]

解密(使用PHP)Java加密(PBEWithMD5AndDES)

有人让我用PHP解密一个用以下Java类加密的字符串。 public class CryptoLibrary { private Cipher encryptCipher; private sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder(); public CryptoLibrary() throws SecurityException{ java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE()); char[] pass = “NNSHHETJKKSNKH”.toCharArray(); byte[] salt = { (byte) 0xa3, (byte) 0x21, (byte) 0x24, (byte) 0x2c, (byte) 0xf2, (byte) 0xd2, (byte) 0x3e, (byte) 0x19 }; init(pass, salt, iterations); } public void init(char[] pass, byte[] salt, […]

C#到Java DES加密

尝试创建java类,它将像下面的C#代码一样加密和解密。 下面是我的C#代码,我需要将其转换为Java。 有人可以帮我怎么做吗? 我已经这样做了将近2天但却找不到任何关于如何转换它的解决方案。 我是加密的新手。 注 – 似乎C#代码使用了ACME库。 但在我的java中,我不应该使用ACME jar文件。 public static string EncryptBase64(string key, string clearText) { if (key.Length > 8) key = key.Substring(0, 8); byte[] keyBytes = System.Text.Encoding.ASCII.GetBytes(key); byte[] clearBytes = GetClearTextBytes(clearText); // calculate the number of legitimate bytes in the last block byte lastByte = (byte)(8 – (clearBytes.Length – textEncoding.GetByteCount(clearText))); MemoryStream ms […]

从文件中读取加密数据

我正在阅读一篇关于使用私钥进行加密的IBM教程。 我编写了如下代码 import java.security.*; import javax.crypto.*; // encrypt and decrypt using the DES private key algorithm public class PrivateExample { public static void main (String[] args) throws Exception { String text=new String(); text=”THIS IS AN ENCRYPTION TEST”; byte[] plainText = text.getBytes(“UTF8”); // get a DES private key System.out.println( “\nStart generating DES key” ); KeyGenerator keyGen […]

将密钥转换为字节,如何将其转换回密钥?

我使用以下代码将密钥转换为字节 SecretKey key = KeyGenerator.getInstance(“DES”).generateKey(); byte[] bkey=key.getEncoded(); 现在如何从bkey获取密钥? 我试过了: SecretKeySpec secretkey = new SecretKeySpec(bkey,”DES”); SecretKeyFactory sfkey = SecretKeyFactory.getInstance(“DES”); SecretKey skey = sfkey.generateSecret(secretkey); 我收到以下错误: Error during Exception java.security.spec.InvalidKeySpecException: Inappropriate key specification

使用Java中的BigIntegers进行BitShifting

我正在使用BigIntegers在Java中实现DES加密。 我通过使用BigInteger.leftShift(int n)方法将Java二进制密钥与Java BigIntegers一起移位。 N(Kn)的关键取决于Kn-1的移位结果。 我得到的问题是,我在每个键生成后打印出结果,并且移位不是预期的输出。 密钥分为2 Cn和Dn(分别为左和右)。 我特意尝试这个:“要做左移,将每个位移到左边一个位置,除了第一个位,循环到块的末尾。” 根据转变,它似乎最终会对O进行攻击。 不知道如何纠正这个问题。 结果: c0:11110101010100110011000011110 d0:11110001111001100110101010100 c1:111101010101001100110000111100 d1:111100011110011001101010101000 c2:11110101010100110011000011110000 d2:11110001111001100110101010100000 c3:1111010101010011001100001111000000 d3:1111000111100110011010101010000000 c4:111101010101001100110000111100000000 d4:111100011110011001101010101000000000 c5:11110101010100110011000011110000000000 d5:11110001111001100110101010100000000000 c6:1111010101010011001100001111000000000000 d6:1111000111100110011010101010000000000000 c7:111101010101001100110000111100000000000000 d7:111100011110011001101010101000000000000000 c8:1111010101010011001100001111000000000000000 d8:1111000111100110011010101010000000000000000 c9:111101010101001100110000111100000000000000000 d9:111100011110011001101010101000000000000000000 c10:11110101010100110011000011110000000000000000000 d10:11110001111001100110101010100000000000000000000 c11:1111010101010011001100001111000000000000000000000 d11:1111000111100110011010101010000000000000000000000 c12:111101010101001100110000111100000000000000000000000 d12:111100011110011001101010101000000000000000000000000 c13:11110101010100110011000011110000000000000000000000000 d13:111100011110011001101010101000000000000000000000000000000 c14:1111010101010011001100001111000000000000000000000000000 d14:1111000111100110011010101010000000000000000000000000000 c15:11110101010100110011000011110000000000000000000000000000 d15:111100011110011001101010101000000000000000000000000000000000