Groovy DES加解密

import java.security.*
import javax.crypto.*
import javax.crypto.spec.*

class DESCodec {
static encode = { String target ->
def cipher = getCipher(Cipher.ENCRYPT_MODE)
return cipher.doFinal(target.bytes).encodeBase64()
}

static decode = { String target ->
def cipher = getCipher(Cipher.DECRYPT_MODE)
return new String(cipher.doFinal(target.decodeBase64()))
}

private static getCipher(mode) {
def keySpec = new DESKeySpec(getPassword())
def cipher = Cipher.getInstance("DES")
def keyFactory = SecretKeyFactory.getInstance("DES")
cipher.init(mode, keyFactory.generateSecret(keySpec))
return cipher
}

private static getPassword() { "secret12".getBytes("UTF-8") }

static void main(args) {
def strmi=encode("asdtiang")
//println decode("asdtiang")
//DESCodec.decode strmi
println decode("${strmi}")这个地主一定要这样写,由于理论不深入,具体为什么还不明白,写成strmi 是不正确的。
///或者写成strmi.toString()也行,不过还没实践过。

}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java库中可以使用Bouncy Castle库来实现SM4加解密算法。Bouncy Castle是一个流行的Java密码学库,提供了各种加密算法的实现。 要使用Bouncy Castle库实现SM4加解密,首先需要在项目中引入Bouncy Castle库的依赖。可以通过Maven或Gradle等构建工具添加以下依赖: Maven: ```xml <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.68</version> </dependency> ``` Gradle: ```groovy implementation 'org.bouncycastle:bcprov-jdk15on:1.68' ``` 接下来,可以使用以下代码示例来进行SM4加解密: ```java import org.bouncycastle.crypto.engines.SM4Engine; import org.bouncycastle.crypto.modes.CBCBlockCipher; import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.ParametersWithIV; import org.bouncycastle.util.encoders.Hex; import java.nio.charset.StandardCharsets; public class SM4EncryptionExample { public static void main(String[] args) throws Exception { // 密钥(16字节) byte[] key = Hex.decode("0123456789abcdeffedcba9876543210"); // 初始化向量(16字节) byte[] iv = Hex.decode("0123456789abcdef"); // 明文 String plaintext = "Hello, SM4!"; byte[] plaintextBytes = plaintext.getBytes(StandardCharsets.UTF_8); // 加密 PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new SM4Engine())); cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv)); byte[] ciphertextBytes = new byte[cipher.getOutputSize(plaintextBytes.length)]; int len = cipher.processBytes(plaintextBytes, 0, plaintextBytes.length, ciphertextBytes, 0); cipher.doFinal(ciphertextBytes, len); // 输出密文 String ciphertext = Hex.toHexString(ciphertextBytes); System.out.println("Ciphertext: " + ciphertext); // 解密 cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv)); byte[] decryptedBytes = new byte[cipher.getOutputSize(ciphertextBytes.length)]; len = cipher.processBytes(ciphertextBytes, 0, ciphertextBytes.length, decryptedBytes, 0); cipher.doFinal(decryptedBytes, len); // 输出解密后的明文 String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8); System.out.println("Decrypted Text: " + decryptedText); } } ``` 以上代码示例演示了如何使用Bouncy Castle库实现SM4加解密。首先定义了密钥和初始化向量,然后使用PaddedBufferedBlockCipher类进行加解密操作。加密时使用`cipher.init(true, ...)`,解密时使用`cipher.init(false, ...)`。最后输出了加密后的密文和解密后的明文。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值