Building an encryption/decryption engine in Scratch - need help with large numbers
Ce contenu n’est pas encore disponible dans votre langue.
💡 Struggling with advanced algorithms or cryptography in Scratch? 🚀 Get Expert Help
CryptoBuilder_Dev
Posted on July 22, 2025 • Advanced
🔐 Help with custom encryption engine - precision issues!
Hey everyone! I’m working on a pretty complex encryption/decryption system in Scratch and I’ve hit a roadblock. 🤔
Here’s what I’m trying to build:
- 🔢 Custom cipher that shifts characters by exponentially increasing amounts
- 📈 Each character gets shifted by 2^(position) to make it harder to crack
- 🔄 A decryption engine that reverses the process
- 🛡️ Goal is to make it nearly impossible to decipher without the algorithm
The Problem: When I encrypt longer sentences, Scratch starts rounding the large numbers due to precision limits. For example, 2^10 = 1024, so by the 11th character I’m already dealing with huge shifts!
The weird thing is, as long as the rounding happens consistently during both encryption AND decryption, it still works. But it’s frustrating and I’m worried about edge cases.
Has anyone dealt with large number precision in Scratch before? Any clever workarounds? 🤓
MathAlgorithm_Expert
Replied 3 hours later • ⭐ Best Answer
@CryptoBuilder_Dev Great project! You’ve identified the exact issue with exponential encryption schemes. 🎯
🔍 The Problem with Exponential Shifts
You’re absolutely right about the precision limits. Scratch uses floating-point arithmetic, which starts losing precision around 15-17 significant digits. With 2^n shifts, you hit this limit very quickly!
💡 Better Encryption Strategies
Here are some approaches that work better in Scratch:
🔧 Method 1: Modulo-Based Shifting
Instead of exponential growth, use modulo to keep numbers manageable:
// Encryption with controlled growth define encrypt character (char) at position (pos) set [ascii v] to (letter (pos) of [input text v]) set [shift amount v] to ((pos * pos) mod [1000]) set [encrypted ascii v] to ((ascii) + (shift amount)) set [encrypted char v] to (letter (encrypted ascii) of [ASCII table v])
🔄 Method 2: Cyclic Key System
Use a repeating pattern instead of exponential growth:
// Create a key pattern that repeats define setup encryption key delete all of [key pattern v] add [7] to [key pattern v] add [23] to [key pattern v] add [41] to [key pattern v] add [13] to [key pattern v] add [67] to [key pattern v] // Use the pattern cyclically define get shift for position (pos) set [key index v] to (((pos - 1) mod (length of [key pattern v])) + 1) set [shift v] to (item (key index) of [key pattern v])
🛡️ Method 3: Multi-Layer Encryption
Combine multiple simple techniques for better security:
// Layer 1: Caesar cipher with position-based shift define layer1 encrypt (text) set [result v] to [] repeat (length of (text)) set [char v] to (letter (counter) of (text)) set [shift v] to ((counter * 3) mod [26]) // Apply Caesar cipher logic here end // Layer 2: Reverse and substitute define layer2 encrypt (text) set [reversed v] to [] repeat (length of (text)) set [reversed v] to (join (letter (counter) of (text)) (reversed)) end // Apply character substitution
🎯 Recommended Solution
For your specific case, I’d suggest:
- Use modulo arithmetic: Keep shifts under 10,000
- Add a salt: Include the text length in your calculation
- Multiple passes: Encrypt the result 2-3 times with different methods
// Robust encryption function define encrypt text (input) set [salt v] to (length of (input)) set [result v] to (input) // Pass 1: Position-based shifting with modulo repeat (3) set [temp v] to [] repeat (length of (result)) set [char ascii v] to (unicode of (letter (counter) of (result))) set [shift v] to (((counter * (salt)) + (counter * counter)) mod [95]) set [new ascii v] to (((char ascii) + (shift)) mod [95]) set [temp v] to (join (temp) (unicode (new ascii) as letter)) end set [result v] to (temp) end
This approach maintains security while avoiding precision issues! 🔒
CryptoBuilder_Dev
Replied 1 hour later
@MathAlgorithm_Expert This is EXACTLY what I needed! 🙌
The modulo approach makes so much sense - I was so focused on making the numbers bigger that I forgot about keeping them manageable. The multi-layer encryption idea is brilliant too!
Going to implement the salt + multiple passes approach. Thanks for the detailed code examples! 🔥
SecurityCoder_Pro
Replied 2 hours later
Great discussion! 🔐 Just want to add that for educational purposes, this is awesome. For real security applications, remember that Scratch projects are visible to everyone, so the algorithm isn’t truly secret.
But for learning cryptography concepts and building cool demos, this approach is perfect! The modulo technique is actually used in real encryption algorithms too.
Pro tip: Try implementing a simple XOR cipher next - it’s another great learning exercise! 🎓
Vibelf_Community
Pinned Message • Moderator
🧮 Love Advanced Mathematics & Algorithms?
Excellent discussion about encryption and number precision! For those interested in advanced programming concepts, our community can help you master:
- 🔐 Cryptography and security algorithms
- 🧮 Advanced mathematics in programming
- ⚡ Algorithm optimization techniques
- 🔢 Working with large numbers and precision
📚 Related Discussions
- Implementing RSA encryption in Scratch
- Working with floating-point precision
- Advanced string manipulation techniques
Ready to dive deeper into computer science concepts? Get expert guidance from our advanced programming tutors!