gpt4 book ai didi

java - AES 加密我正在尝试使用 AES 加密进行加密

转载 作者:太空宇宙 更新时间:2023-11-03 18:00:16 32 4
gpt4 key购买 nike

我只想将ruby代码迁移到Java

这是我的 ruby 代码

require 'openssl'
require 'base64'

key = '7c54367a45b37a192abc2cd7f45203042350406f8'
cipher = OpenSSL::Cipher::Cipher.new('aes-128-ecb')
cipher.encrypt()


cipher = OpenSSL::Cipher::Cipher.new('aes-256-ecb')
cipher.encrypt()
cipher.key = key

crypt = cipher.update('Rahul')
crypt << cipher.final()

puts (Base64.encode64(crypt))

这是我在 Java 中尝试的

String getDecodedString(String key,String encodedValue,SupportedEncryptionAlgorithm algoInfo) 
{
Cipher cipher = getCipherInstancenew(algoInfo, key,Cipher.DECRYPT_MODE);
try
{
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(encodedValue);

int ctLength = cipher.getOutputSize(dec.length);
byte[] plainText = new byte[cipher.getOutputSize(ctLength)];

int ptLength = cipher.update(dec, 0, ctLength, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);

return null;
}
catch (IllegalBlockSizeException e)
{
LoggerFactory.getLogger(EncryptionHelper.class).error("Security Alert",e);
}
catch (BadPaddingException e)
{
LoggerFactory.getLogger(EncryptionHelper.class).error("Security Alert",e);
}
return null;
}

public static byte[] stringToBytes(String s) {
byte[] b2 = new BigInteger(s, 36).toByteArray();
return Arrays.copyOfRange(b2, 1, b2.length);
}

public static Cipher getCipherInstancenew(SupportedEncryptionAlgorithm algoInfo,String keyString,int mode) throws IOException
{
byte[] decodedBytes;
Cipher cipher=null;
try
{
decodedBytes = getBase64FromHEX(keyString).getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(decodedBytes, "AES");
Security.addProvider(new BouncyCastleProvider());
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
cipher.init(mode, skeySpec );
}
catch (java.security.GeneralSecurityException e)
{
/*Strictly no logging as it is security class
* There seems to be some issue with the Keys so alert it */
//LoggerFactory.getLogger(EncryptionHelper.class).error("Security Alert",e);
throw new IOException("GetCipherInstance does not exsists");
}

return cipher;

}

public static String getBase64FromHEX(String input) {

byte barr[] = new byte[16];
int bcnt = 0;
for (int i = 0; i < 32; i += 2) {
char c1 = input.charAt(i);
char c2 = input.charAt(i + 1);
int i1 = intFromChar(c1);
int i2 = intFromChar(c2);

barr[bcnt] = 0;
barr[bcnt] |= (byte) ((i1 & 0x0F) << 4);
barr[bcnt] |= (byte) (i2 & 0x0F);
bcnt++;
}

BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(barr);
}

private static int intFromChar(char c) {
char[] carr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

char clower = Character.toLowerCase(c);
for (int i = 0; i < carr.length; i++) {
if (clower == carr[i]) {
return i;
}
}

return 0;
}

它适用于 32 字节的字符串但不适用于 Java 中的 41 字节但在 Ruby 中它适用于任何大于 32 字节的长度奇怪请帮助

最佳答案

下面的 Java 代码输出与 Ruby 代码完全相同的 base 64 编码结果并成功解密:

final Cipher encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
encryptCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec("7c54367a45b37a192abc2cd7f4520304".getBytes(), "AES"));
final byte[] encrypt = encryptCipher.doFinal("This is my text".getBytes());
System.out.println(new String(Base64.encode(encrypt)));

final Cipher decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
decryptCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec("7c54367a45b37a192abc2cd7f4520304".getBytes(), "AES"));
final byte[] decrypt = decryptCipher.doFinal(encrypt);
System.out.println(new String(decrypt));

Ruby OpenSSL API 显然只使用 key 的前 32 个字节,因为 key 的以下值返回与 41 字节版本相同的值:

key = '7c54367a45b37a192abc2cd7f4520304'

此外,我不确定为什么 cipher 在 Ruby 代码中被初始化两次,因为据我所知这不是必需的。

关于java - AES 加密我正在尝试使用 AES 加密进行加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6429246/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com