gpt4 book ai didi

java - AndroidKeyStore key 生成期间的 IllegalArgumentException(无法解析的日期)

转载 作者:太空宇宙 更新时间:2023-11-03 12:30:23 25 4
gpt4 key购买 nike

在使用 AndroidKeyStore 生成 RSA key 期间,我在我的应用程序中遇到了以下问题,而不是理解它可以很容易地从 Android SDK 在 BasicAndroidKeyStore 示例应用程序中复制。因此,如果您有 Locale.getDefault() == Locale.US,则此示例运行良好,但如果您将区域设置更改为,例如 "ar_EG",它会异常崩溃:

java.lang.IllegalArgumentException: invalid date string: Unparseable date: "af`cadaaedcaGMT+00:00" (at offset 0) at com.android.org.bouncycastle.asn1.DERUTCTime.(DERUTCTime.java:98) at com.android.org.bouncycastle.asn1.x509.Time.(Time.java:62) at com.android.org.bouncycastle.x509.X509V3CertificateGenerator.setNotBefore(X509V3CertificateGenerator.java:112) at android.security.AndroidKeyPairGenerator.generateKeyPair(AndroidKeyPairGenerator.java:127) at java.security.KeyPairGenerator$KeyPairGeneratorImpl.generateKeyPair(KeyPairGenerator.java:276) at com.example.android.basicandroidkeystore.BasicAndroidKeyStoreFragment.createKeys(BasicAndroidKeyStoreFragment.java:237)

因此,问题出在转换为字符串的 key 有效性时间,这是针对默认语言环境进行的。这是来自 ASN1UTCTime 的代码 fragment 类,它在 KeyPairGenerator.generateKeyPair() 的内部使用,方法调用如下:

public ASN1UTCTime(
String time)
{
this.time = Strings.toByteArray(time);
try
{
this.getDate();
}
catch (ParseException e)
{
throw new IllegalArgumentException("invalid date string: " + e.getMessage());
}
}

在调用此方法之前,日期对象被传递给以下 Time构造函数,它使用默认的系统语言环境:

public Time(
Date time)
{
SimpleTimeZone tz = new SimpleTimeZone(0, "Z");
SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss");
dateF.setTimeZone(tz);
String d = dateF.format(time) + "Z";
int year = Integer.parseInt(d.substring(0, 4));
if (year < 1950 || year > 2049)
{
this.time = new DERGeneralizedTime(d);
}
else
{
this.time = new DERUTCTime(d.substring(2));
}
}

这很奇怪,因为ASN1UTCTime类有另一个构造函数,似乎更适合国际工作:

/**
* Base constructor from a java.util.date and Locale - you may need to use this if the default locale
* doesn't use a Gregorian calender so that the GeneralizedTime produced is compatible with other ASN.1 implementations.
*
* @param time a date object representing the time of interest.
* @param locale an appropriate Locale for producing an ASN.1 UTCTime value.
*/
public ASN1UTCTime(
Date time,
Locale locale)
{
SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmss'Z'", locale);
dateF.setTimeZone(new SimpleTimeZone(0,"Z"));
this.time = Strings.toByteArray(dateF.format(time));
}

那么,什么是正确的修复或建议如何解决这个问题?

最佳答案

这是一个 known issue with AndroidKeyStore .

Android KeyStore 无法正确获取语言环境,这会导致设备语言环境从右到左出现故障。示例堆栈跟踪:

Caused by: java.lang.IllegalArgumentException: invalid date string: Unparseable date: "aga``eaeeb`eGMT+00:00" (at offset 0)
at com.android.org.bouncycastle.asn1.DERUTCTime.<init>(DERUTCTime.java:98)
at com.android.org.bouncycastle.asn1.x509.Time.<init>(Time.java:62)
at com.android.org.bouncycastle.x509.X509V3CertificateGenerator.setNotBefore(X509V3CertificateGenerator.java:112)
at android.security.AndroidKeyPairGenerator.generateKeyPair(AndroidKeyPairGenerator.java:128)
at java.security.KeyPairGenerator$KeyPairGeneratorImpl.generateKeyPair(KeyPairGenerator.java:275)

解决方法是在生成 key 对之前设置英语语言环境,然后再将其改回:

/**
* Generates RSA keys.
*/
private void generateRsaKeys(Context context, String rsaAlias) {
try {
// Set English locale as default (workaround)
Locale initialLocale = Locale.getDefault();
setLocale(Locale.ENGLISH);
// Generate the RSA key pairs
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 30); // 30 years
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
.setAlias(rsaAlias)
.setSubject(new X500Principal("CN=" + rsaAlias + ", O=Organization"))
.setSerialNumber(BigInteger.TEN)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA, ANDROID_KEY_STORE);
kpg.initialize(spec);
kpg.generateKeyPair();
// Reset default locale
setLocale(initialLocale);
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
Log.e(e, "generateRsaKeys: ");
}
}

/**
* Sets default locale.
*/
private void setLocale(Locale locale) {
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
config.locale = locale;
resources.updateConfiguration(config, resources.getDisplayMetrics());
}

关于java - AndroidKeyStore key 生成期间的 IllegalArgumentException(无法解析的日期),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35987510/

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