packageorg.owasp.mastestappimportandroid.content.Contextimportjavax.crypto.Cipherimportjavax.crypto.SecretKeyimportjavax.crypto.spec.SecretKeySpecimportandroid.util.Base64classMastgTest(privatevalcontext:Context){funmastgTest():String{// Bad: Use of a hardcoded key (from bytes) for encryptionvalkeyBytes=byteArrayOf(0x6C,0x61,0x6B,0x64,0x73,0x6C,0x6A,0x6B,0x61,0x6C,0x6B,0x6A,0x6C,0x6B,0x6C,0x73)// Example key bytesvalcipher=Cipher.getInstance("AES/GCM/NoPadding")valsecretKey=SecretKeySpec(keyBytes,"AES")cipher.init(Cipher.ENCRYPT_MODE,secretKey)// Bad: Hardcoded key directly in code (security risk)valbadSecretKeySpec=SecretKeySpec("my secret here".toByteArray(),"AES")// Returning resultsreturn"SUCCESS!!\n\nThe keys were generated and used successfully with the following details:\n\n"+"Hardcoded AES Encryption Key: ${Base64.encodeToString(keyBytes,Base64.DEFAULT)}\n"+"Hardcoded Key from string: ${Base64.encodeToString(badSecretKeySpec.encoded,Base64.DEFAULT)}\n"}}
packageorg.owasp.mastestapp;importandroid.content.Context;importandroid.util.Base64;importjavax.crypto.Cipher;importjavax.crypto.spec.SecretKeySpec;importkotlin.Metadata;importkotlin.jvm.internal.Intrinsics;importkotlin.text.Charsets;/* compiled from: MastgTest.kt */@Metadata(d1={"\u0000\u0018\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0000\b\u0007\u0018\u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J\u0006\u0010\u0005\u001a\u00020\u0006R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\u0007"},d2={"Lorg/owasp/mastestapp/MastgTest;","","context","Landroid/content/Context;","(Landroid/content/Context;)V","mastgTest","","app_debug"},k=1,mv={1,9,0},xi=48)/* loaded from: classes4.dex */publicfinalclassMastgTest{publicstaticfinalint$stable=8;privatefinalContextcontext;publicMastgTest(Contextcontext){Intrinsics.checkNotNullParameter(context,"context");this.context=context;}publicfinalStringmastgTest(){byte[]keyBytes={108,97,107,100,115,108,106,107,97,108,107,106,108,107,108,115};Ciphercipher=Cipher.getInstance("AES/GCM/NoPadding");SecretKeySpecsecretKey=newSecretKeySpec(keyBytes,"AES");cipher.init(1,secretKey);byte[]bytes="my secret here".getBytes(Charsets.UTF_8);Intrinsics.checkNotNullExpressionValue(bytes,"this as java.lang.String).getBytes(charset)");SecretKeySpecbadSecretKeySpec=newSecretKeySpec(bytes,"AES");return"SUCCESS!!\n\nThe keys were generated and used successfully with the following details:\n\nHardcoded AES Encryption Key: "+Base64.encodeToString(keyBytes,0)+"\nHardcoded Key from string: "+Base64.encodeToString(badSecretKeySpec.getEncoded(),0)+'\n';}}
rules:-id:mastg-android-hardcoded-crypto-keys-usageseverity:WARNINGlanguages:-javametadata:summary:Thisrulelooksforhardcodedkeysinuse.message:"[MASVS-CRYPTO-1] Hardcoded cryptographic keys found in use."pattern-either:-pattern:SecretKeySpec$_=newSecretKeySpec($KEY,$ALGO);-pattern:|-byte[]$KEY={...};...newSecretKeySpec($KEY,$ALGO);
The rule has identified one instance in the code file where hardcoded keys is used. The specified line numbers can be located in the reverse-engineered code for further investigation and remediation.