c blowfish java,blowfish-golang加密与java解密

无法解密Java中使用Blowfish在Golang中加密的密码文本。

加密

import (

"testing"

"golang.org/x/crypto/blowfish"

"github.com/andreburgaud/crypt2go/ecb"

"github.com/andreburgaud/crypt2go/padding"

"fmt"

"encoding/base64"

)

func TestEncrypt(t *testing.T) {

bytes := []byte("cap")

key := []byte("1c157d26e2db9a96a556e7614e1fbe36")

encByte := encrypt(bytes, key)

enc := base64.StdEncoding.EncodeToString(encByte)

fmt.Printf("ENC - %s\n", enc)

}

func encrypt(pt, key []byte) []byte {

block, err := blowfish.NewCipher(key)

if err != nil {

panic(err.Error())

}

mode := ecb.NewECBEncrypter(block)

padder := padding.NewPkcs5Padding()

pt, err = padder.Pad(pt) // padd last block of plaintext if block size less than block cipher size

if err != nil {

panic(err.Error())

}

ct := make([]byte, len(pt))

mode.CryptBlocks(ct, pt)

return ct

}

// Output

// ENC - AP9atM49v8o=

解密

import lombok.SneakyThrows;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

import static java.util.Base64.getDecoder;

import static java.util.Base64.getEncoder;

public class UserAuthenticationFilter {

public static void main(String[] args) throws Exception {

String key = "1c157d26e2db9a96a556e7614e1fbe36";

System.out.println(decrypt(getDecoder().decode("AP9atM49v8o="), key));

// encryption and decryption verification

// String plainText = "cap";

// String cipher = encrypt(plainText, key);

// String decrypted = decrypt(getDecoder().decode(enc), key);

// assert decrypted.equals(plainText);

}

@SneakyThrows

public static String encrypt(String plainText, String key) {

byte[] myKeyByte = hexToBytes(key);

SecretKeySpec skeySpec = new SecretKeySpec(myKeyByte, "Blowfish");

Cipher ecipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");

ecipher.init(Cipher.ENCRYPT_MODE, skeySpec);

byte[] src = ecipher.doFinal(plainText.getBytes("ISO-8859-1"));

return getEncoder().encodeToString(src);

}

@SneakyThrows

public static String decrypt(byte[] cipherContent, String key) {

byte[] myKeyByte = hexToBytes(key);

SecretKeySpec skeySpec = new SecretKeySpec(myKeyByte, "Blowfish");

Cipher dcipher = Cipher.getInstance("Blowfish/ECB/NoPadding");

dcipher.init(2, skeySpec);

byte[] dcontent = dcipher.doFinal(cipherContent);

return (new String(dcontent, "ISO-8859-1")).trim();

}

private static byte[] hexToBytes(String str) {

if (str == null) {

return null;

} else if (str.length() < 2) {

return null;

} else {

int len = str.length() / 2;

byte[] buffer = new byte[len];

for(int i = 0; i < len; ++i) {

buffer[i] = (byte)Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);

}

return buffer;

}

}

}

// Output

// BY x³

根据输出,golang中的加密和java中的解密不会产生相同的纯文本。最初,认为问题可能与base64编码和解码中涉及的golang字节(0到255)和java字节(-128到127)有关。但是插入Java的解密代码,就可以正确地处理

value & 255

是的。

在果朗,解密同一个密码文本工作得很好。同时,java中的加密和解密也非常有效。但不是一个加密另一个解密。

我认为加密和解密逻辑是正确的。只是猜测可能有一些特定的语言???在将密码文本移植到其他语言进行解密时丢失。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值