I'm using Spongy Castle library to encode my users private key (PKCS8) into an ASN1 entity and afterwards as Base64 encoded string into a QR code.
One of my colleagues found out that it's possible to change some characters in the Base64 string without damaging the private key. Does the ASN1 format or PKCS8/DER format have some fault tolerance implemented?
//final String encoded = "MIGcAgEBB........lGEOPD2o+H59Qyl"; // original
final String encoded = "MIGcAgEBB........lGEOPD2oXXXXXXX"; // changed!
// decode Base64
final byte[] buffer = Base64.decode(encoded);
// decode ASN1
final ASN1Primitive primitive = ASN1Primitive.fromByteArray(buffer);
final ASN1Sequence asn1Sequence = ASN1Sequence.getInstance(primitive);
// read from ASN1
final BigInteger version = ASN1Integer.getInstance(asn1Sequence.getObjectAt(0)).getValue();
final byte[] keyBytes = DEROctetString.getInstance(asn1Sequence.getObjectAt(1)).getOctets();
// get private key from bytes
final PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes);
final PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);