Ruby 中的简单加密,无需外部 gem [英] Simple Encryption in Ruby without external gems

查看:22
本文介绍了Ruby 中的简单加密,无需外部 gem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对一些文本字符串进行简单的加密.我想创建优惠券代码并使它们看起来很酷,因此随后创建的代码应该看起来非常不同.(除了看起来很酷之外,猜出一个代码应该不容易.)但我希望能够再次解密它们.所以算法一定是可逆的.

我已经尝试了一些移动位的东西,所以它们看起来已经有点随机了.但是后面的两个代码(只有一点不同)当然看起来非常相似.

有什么建议吗?我想在不使用外部 gem 的情况下做到这一点.

菲利普

解决方案

解决方案有点从头开始,但基于此:

所以 a 的逆是 15790321.将所有这些放在一起:

A=17乙=37A_INV=15790321定义加密(x)(A*x+B)%(2**27)结尾def解密(y)((y-B)*A_INV)%(2**27)结尾

现在你可以:

irb(main):038:0>加密(4)=>105irb(main):039:0>解密(105)=>4

显然,我们希望优惠券代码看起来很酷.所以需要另外两件事:从 4000 左右开始序列,所以代码更长.还将它们转换为字母数字,这对于 Ruby 来说也很简单:

irb(main):050:0>解密(1ghx".to_i(36))=>4000irb(主):051:0>加密(4000).to_s(36)=>1ghx"

一个很好的附加属性是连续的数字足够不同以至于几乎不可能猜测.当然,我们假设用户不是加密分析师,如果有人确实猜到了一个有效数字,这不是世界末日:-)

irb(main):053:0>加密(4001).to_s(36)=>1吉"irb(main):054:0>解密(1gie".to_i(36))=>4001

让我们尝试通过从 1gie1gif 计数来天真地破解"它:

irb(main):059:0*decrypt("1gif".to_i(36))=>15794322

这完全超出了范围,反正只有 2000 张左右的优惠券 - 而不是 100 万张.:-) 另外,如果我没记错的话,可以对参数进行一些试验,因此后续数字看起来更混乱.

(为更长的代码选择更大的 n,反之亦然.Base 36 意味着每个字符需要 6 位(Math.log(36, 2)".所以 n=27 最多允许 5 个字符.)

I need a simple encryption for some text strings. I want to create coupon codes and make them look cool so subsequently created code should look very different. (And besides looking cool, it shouldn't be easy to guess a code.) But I want to be able to decrypt them again. So the algorithm must be reversible.

I alread tried some stuff with moving bits around so they look kind of random already. But two subsequent codes (just one bit different) of course look very similar.

Any suggestions? I would like to do that without using external gems.

Philip

解决方案

The solution is kind of from scratch but based on this: https://math.stackexchange.com/questions/9508/looking-for-a-bijective-discrete-function-that-behaves-as-chaotically-as-possib

The simplest way presented is using a * x + b (mod 2^n)

Obviously this is no real encryption and really only useful if you want to create sequential coupon codes without using much code.

So to implement this, you first need to pick a, b and n. (a must be odd) For example a=17, b=37 and n=27. Also we need to find "a^(-1)" on "mod 2^n". It's possible to do this on https://www.wolframalpha.com using the ExtendedGcd function:

So the inverse of a is therefore 15790321. Putting all this together:

A=17
B=37
A_INV=15790321

def encrypt(x)
  (A*x+B)%(2**27)
end

def decrypt(y)
  ((y-B)*A_INV)%(2**27)
end

And now you can do:

irb(main):038:0> encrypt(4)
=> 105
irb(main):039:0> decrypt(105)
=> 4

Obviously we want the coupon codes to look cool. So 2 extra things are needed: start the sequence at 4000 or so, so the codes are longer. Also convert them into something alpha-numeric, that's also an easy one with Ruby:

irb(main):050:0> decrypt("1ghx".to_i(36))
=> 4000
irb(main):051:0> encrypt(4000).to_s(36)
=> "1ghx"

One nice additional property is that consecutive numbers are different enough that guessing is practically impossible. Of course we assume that the users are not crypto analysts and if someone indeed guesses a valid number, it's not the end of the world: :-)

irb(main):053:0> encrypt(4001).to_s(36)
=> "1gie"
irb(main):054:0> decrypt("1gie".to_i(36))
=> 4001

Let's try to naively "hack" it by counting from 1gie to 1gif:

irb(main):059:0* decrypt("1gif".to_i(36))
=> 15794322

That's completely out of range, there are just 2000 or so coupons anyways - not a million. :-) Also if I remember correctly one can experiment a bit with the parameters, so subsequent numbers look more chaotic.

(Pick a larger n for longer codes and vice-versa. Base 36 means 6 bits are needed for each character ("Math.log(36, 2)"). So n=27 allows for up to 5 characters.)

这篇关于Ruby 中的简单加密,无需外部 gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆