gpt4 book ai didi

java - 如何修复 javax.crypto.IllegalBlockSizeException?

转载 作者:行者123 更新时间:2023-11-29 22:51:05 25 4
gpt4 key购买 nike

我已经使用 RSA 的公钥加密成功地加密了我的 AES key 。在使用 RSA 的私钥解密时,我得到:

javax.crypto.IllegalBlockSizeException

适用于我的加密逻辑是:

KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
SecretKey key = keygen.generateKey();
Log.d("keyS", encodeToString(key.getEncoded(), Base64.DEFAULT));
Calendar start = new GregorianCalendar();
Calendar stop = new GregorianCalendar();
stop.add(Calendar.YEAR,25);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(Objects.requireNonNull(getContext()))
.setKeySize(2048)
.setAlias(KEYSTORE_PROVIDER_ANDROID_KEYSTORE)
.setSubject(new X500Principal("CN="+KEYSTORE_PROVIDER_ANDROID_KEYSTORE))
.setSerialNumber(BigInteger.ZERO)
.setStartDate(start.getTime()).setEndDate(stop.getTime()).build();
KeyPairGenerator keyPairGenerator;
KeyPair kp;
keyPairGenerator = KeyPairGenerator.getInstance("RSA", KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
keyPairGenerator.initialize(spec);
kp = keyPairGenerator.generateKeyPair();
Cipher cipher = Cipher.getInstance("RSA/None/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, kp.getPublic());
String key64 = encodeToString(key.getEncoded(), Base64.DEFAULT);
editor.putString(SECRET,new String(cipher.doFinal(key64.getBytes()), StandardCharsets.UTF_8)).apply();
Log.d("key2",new String(cipher.doFinal(key64.getBytes()), StandardCharsets.UTF_8));

我目前使用的解密逻辑是:

final SharedPreferences settings = getSharedPreferences(SettingsActivity.PREF_SETTINGS, MODE_PRIVATE);
KeyStore keyStore = KeyStore.getInstance(KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
keyStore.load(null);
KeyStore.Entry entry = keyStore.getEntry(KEYSTORE_PROVIDER_ANDROID_KEYSTORE,null);
PrivateKey privateKey = ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();
Cipher cipher = Cipher.getInstance("RSA/None/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] tmp = Objects.requireNonNull(settings.getString(SECRET, null)).getBytes(StandardCharsets.UTF_8);
Log.d("key3",new String(tmp));
Log.d("key4", encodeToString(cipher.doFinal(tmp), Base64.DEFAULT) );

如何修复 cipher.doFinal() 抛出的错误?

最佳答案

找到我的解决方案。这是解决方案。感谢那些帮助我重新思考解决方案的人。加密代码:

KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
SecretKey key = keygen.generateKey();
Log.d("keyS", encodeToString(key.getEncoded(), Base64.DEFAULT));
Calendar start = new GregorianCalendar();
Calendar stop = new GregorianCalendar();
stop.add(Calendar.YEAR,25);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(Objects.requireNonNull(getContext()))
.setKeySize(2048)
.setAlias(KEYSTORE_PROVIDER_ANDROID_KEYSTORE)
.setSubject(new X500Principal("CN="+KEYSTORE_PROVIDER_ANDROID_KEYSTORE))
.setSerialNumber(BigInteger.ZERO)
.setStartDate(start.getTime()).setEndDate(stop.getTime()).build();
KeyPairGenerator keyPairGenerator;
KeyPair kp;
keyPairGenerator = KeyPairGenerator.getInstance("RSA", KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
keyPairGenerator.initialize(spec);
kp = keyPairGenerator.generateKeyPair();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, kp.getPublic());
editor.putString(SECRET,encodeToString(cipher.doFinal(key.getEncoded()), Base64.DEFAULT)).apply();
Log.d("key2",encodeToString(cipher.doFinal(key.getEncoded()), Base64.DEFAULT));

解密代码:

final SharedPreferences settings = getSharedPreferences(SettingsActivity.PREF_SETTINGS, MODE_PRIVATE);
KeyStore keyStore = KeyStore.getInstance(KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
keyStore.load(null);
KeyStore.Entry entry = keyStore.getEntry(KEYSTORE_PROVIDER_ANDROID_KEYSTORE,null);
PrivateKey privateKey = ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
String k64 = Objects.requireNonNull(settings.getString(SECRET, null));
byte[] tmp = decode(k64, Base64.DEFAULT);
Log.d("key3",k64);
MainActivity.key = new SecretKeySpec(cipher.doFinal(tmp),"AES");
Log.d("key4", encodeToString(MainActivity.key.getEncoded(),Base64.DEFAULT));

关于java - 如何修复 javax.crypto.IllegalBlockSizeException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57893633/

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