【问题标题】:DESede-ECB Encryption-DecryptionDESede-ECB加密-解密
【发布时间】:2021-09-25 22:11:47
【问题描述】:

我正在尝试使用以下代码在 java 中加密和解密字符串:

public byte[] encrypt(String message) throws Exception {
    final MessageDigest md = MessageDigest.getInstance("md5");
    final byte[] digestOfPassword = md.digest("MYKEY12345"
            .getBytes("utf-8"));
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
        keyBytes[k++] = keyBytes[j++];
    }

    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
//        final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
    final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);

    final byte[] plainTextBytes = message.getBytes("utf-8");
    final byte[] cipherText = cipher.doFinal(plainTextBytes);
    // final String encodedCipherText = new sun.misc.BASE64Encoder()
    // .encode(cipherText);



    Log.d("base64", Base64.getEncoder().encodeToString(cipherText));

    return cipherText;
}


public String decrypt(byte[] message) throws Exception {
    final MessageDigest md = MessageDigest.getInstance("md5");
    final byte[] digestOfPassword = md.digest("MYKEY12345"
            .getBytes("utf-8"));
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
        keyBytes[k++] = keyBytes[j++];
    }

    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
//    final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
    final Cipher decipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key);

    // final byte[] encData = new
    // sun.misc.BASE64Decoder().decodeBuffer(message);
    final byte[] plainText = decipher.doFinal(message);

    return new String(plainText, "UTF-8");
}

用法:

byte[] codedtext = new Common().encrypt("HELLOWORLD!"); // Function to get the Encrption

输出:

 base64: Ya9zBTukyOmdOh5/5vCaGA== // encrypted string converted to base64
 Encrypted : [B@d41c149 

解密:

 String decodedtext = new Common().decrypt(codedtext); // To decrypt String

输出:

Decrypted : HELLOWORLD!  // from Encrypted string

现在,如果我使用相同的密钥和字符串在线获取加密密钥,我会得到不同的值。

Using this link 验证我的加密/解密。

我刚刚开始加密/解密,因此对于我在这里做错的任何事情,我们都非常感谢。

【问题讨论】:

  • 当我使用相同的密钥加密/解密相同的字符串时,我每次都会收到不同的值。
  • 是密文不同的问题吗?这有几个可能的原因:该网站几乎不会通过 MD5 实现您的密钥派生,可能您还选择了错误的算法和/或模式,可能该网站使用了不同的填充或可能只是错误(这在加密网站)。请注意,您的代码在几个方面是不安全的:损坏的 MD5、通过摘要派生的密钥、硬编码密码、ECB 模式。此外,TDES 很慢。顺便说一句,不断变化的密文表示带有 IV 的模式。
  • 这个Encrypted : [B@d41c149 表明您正试图通过调用该数组上的.toString() 方法打印出一个字节数组,由于字符串转换,显式或隐式。这并不像你认为的那样。要打印出二进制/任意字节序列,您应该首先对它们进行十六进制编码或 base64 编码,您已经完成了。
  • 您拥有的链接指向 ECB 模式下的单个 DES,而不是 DESede。

标签: java encryption des tripledes


【解决方案1】:

你需要

final byte[] plainText = decipher.doFinal(Base64.getDecoder().decode(message));

【讨论】:

  • 不,他没有。 OP 的代码打印 base64 到 System.out,但将原始(非base64)值存储在 codedtext 中。
  • @dave_thompson_085 我之所以这么说是因为它似乎他正在将加密代码的输出反馈到解密代码中(很自然的事情)。解密中的 Base64 也很自然地反映了加密例程
猜你喜欢
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-20
相关资源
最近更新 更多